Selles õpetuses saate näidete abil teada Java eri tüüpi operaatorite kohta, nende süntaksist ja nende kasutamisest.
Operaatorid on sümbolid, mis teostavad muutujate ja väärtustega toiminguid. Näiteks +
on operaator, mida kasutatakse liitmiseks, samas kui *
ka operaator, mida kasutatakse korrutamiseks.
Java-operaatorid saab jagada viide tüüpi:
- Aritmeetikaoperaatorid
- Ülesandeoperaatorid
- Suheteoperaatorid
- Loogilised operaatorid
- Unary Operaatorid
- Bitipõhised operaatorid
1. Java aritmeetikaoperaatorid
Aritmeetilisi operaatoreid kasutatakse muutujate ja andmete aritmeetiliste toimingute tegemiseks. Näiteks,
a + b;
Siin kasutatakse +
operaatorit kahe muutuja a ja b lisamiseks. Samamoodi on Java-s mitmeid muid aritmeetilisi operaatoreid.
Operaator | Operatsioon |
+ | Lisamine |
- | Lahutamine |
* | Korrutamine |
/ | Jaotus |
% | Modulo operatsioon (järelejäänud osa pärast jagamist) |
Näide 1: Aritmeetikaoperaatorid
class Main ( public static void main(String() args) ( // declare variables int a = 12, b = 5; // addition operator System.out.println("a + b = " + (a + b)); // subtraction operator System.out.println("a - b = " + (a - b)); // multiplication operator System.out.println("a * b = " + (a * b)); // division operator System.out.println("a / b = " + (a / b)); // modulo operator System.out.println("a % b = " + (a % b)); ) )
Väljund
a + b = 17 a - b = 7 a * b = 60 a / b = 2 a% b = 2
Ülaltoodud näites oleme liitmis-, lahutamis- ja korrutustoimingute arvutamiseks kasutanud funktsioone +
, -
ja *
.
/ Jaoskonna operaator
Pange tähele a / b
meie programmi toimingut . /
Operaator on jagunemine operaator.
Kui kasutame jagamisoperaatorit kahe täisarvuga, on ka saadud jagatis täisarv. Ja kui üks operandidest on ujukomaarv, saame tulemuse ka ujukoma.
In Java, (9 / 2) is 4 (9.0 / 2) is 4.5 (9 / 2.0) is 4.5 (9.0 / 2.0) is 4.5
% Modulo operaator
Modulaoperaator %
arvutab ülejäänud osa. Kui a = 7
jagatakse b = 4
, on ülejäänud 3 .
Märkus . %
Operaatorit kasutatakse peamiselt täisarvudega.
2. Java määramise operaatorid
Muutujate väärtuste määramiseks kasutatakse Java-s määramisoperaatoreid. Näiteks,
int age; age = 5;
Siin =
on määramise operaator. See määrab väärtuse paremal vasakul olevale muutujale. See tähendab, et 5 määratakse muutuvale vanusele.
Vaatame veel mõnda Java-s saadaval olevat määramisoperaatorit.
Operaator | Näide | Samaväärne |
---|---|---|
= | a = b; | a = b; |
+= | a += b; | a = a + b; |
-= | a -= b; | a = a - b; |
*= | a *= b; | a = a * b; |
/= | a /= b; | a = a / b; |
%= | a %= b; | a = a % b; |
Näide 2: ülesandeoperaatorid
class Main ( public static void main(String() args) ( // create variables int a = 4; int var; // assign value using = var = a; System.out.println("var using =: " + var); // assign value using =+ var += a; System.out.println("var using +=: " + var); // assign value using =* var *= a; System.out.println("var using *=: " + var); ) )
Väljund
var, kasutades =: 4 var, kasutades + =: 8 var, kasutades * =: 32
3. Java suhtlusoperaatorid
Relatsioonioperaatoreid kasutatakse kahe operandi suhte kontrollimiseks. Näiteks,
// check is a is less than b a < b;
Siin on >
operaator suhteline operaator. See kontrollib, kas a on väiksem kui b või mitte.
See tagastab kas true
või false
.
Operaator | Kirjeldus | Näide |
---|---|---|
== | On võrdne | 3 == 5 tagastab vale |
!= | Pole võrdne | 3 != 5 tagastab tõene |
> | Suurem kui | 3> 5 tagastab vale |
< | Vähem kui | 3 < 5 tagastab tõene |
>= | Suurem või võrdne | 3>= 5 tagastab vale |
<= | Vähem kui võrdne | 3 <= 5 tagastab vale |
Näide 3: suhtekorraldajad
class Main ( public static void main(String() args) ( // create variables int a = 7, b = 11; // value of a and b System.out.println("a is " + a + " and b is " + b); // == operator System.out.println(a == b); // false // != operator System.out.println(a != b); // true //> operator System.out.println(a> b); // false // < operator System.out.println(a = operator System.out.println(a>= b); // false // <= operator System.out.println(a <= b); // true ) )
Märkus . Otsustusprotsessides ja tsüklites kasutatakse suhtekorraldajaid.
4. Java loogilised operaatorid
Loogiliste operaatorite abil kontrollitakse, kas avaldis on true
või false
. Neid kasutatakse otsuste tegemisel.
Operaator | Näide | Tähendus |
---|---|---|
&& (Loogiline JA) | AVALDIS1 && AVALDIS2 | true ainult siis, kui nii avaldis1 kui ka avaldis2 on true |
|| (Loogiline VÕI) | väljend1 || avaldis2 | true kui kas avaldis1 või avaldis2 on true |
! (Loogiline EI) | ! väljendus | true kui väljend on false ja vastupidi |
Näide 4: loogilised operaatorid
class Main ( public static void main(String() args) ( // && operator System.out.println((5> 3) && (8> 5)); // true System.out.println((5> 3) && (8 < 5)); // false // || operator System.out.println((5 5)); // true System.out.println((5> 3) || (8 < 5)); // true System.out.println((5 < 3) || (8 3)); // false ) )
Programmi töö
(5> 3) && (8> 5)
naaseb,true
sest mõlemad(5> 3)
ja(8> 5)
ontrue
.(5> 3) && (8 < 5)
naaseb,false
kuna avaldis(8 < 5)
onfalse
.(5> 3) || (8> 5)
naaseb,true
kuna avaldis(8> 5)
ontrue
.(5> 3) && (8> 5)
naaseb,true
kuna avaldis(5> 3)
ontrue
.(5> 3) && (8> 5)
naaseb,false
sest mõlemad(5 < 3)
ja(8 < 5)
onfalse
.!(5 == 3)
tagastab tõene, sest5 == 3
onfalse
.!(5> 3)
tagastab vale, kuna5> 3
ontrue
.
5. Java Unary operaatorid
Unary operators are used with only one operand. For example, ++
is a unary operator that increases the value of a variable by 1. That is, ++5
will return 6.
Different types of unary operators are:
Operator | Meaning |
---|---|
+ | Unary plus: not necessary to use since numbers are positive without using it |
- | Unary minus: inverts the sign of an expression |
++ | Increment operator: increments value by 1 |
-- | Decrement operator: decrements value by 1 |
! | Logical complement operator: inverts the value of a boolean |
Increment and Decrement Operators
Java also provides increment and decrement operators: ++
and --
respectively. ++
increases the value of the operand by 1, while --
decrease it by 1. For example,
int num = 5; // increase num by 1 ++num;
Here, the value of num gets increased to 6 from its initial value of 5.
Example 5: Increment and Decrement Operators
class Main ( public static void main(String() args) ( // declare variables int a = 12, b = 12; int result1, result2; // original value System.out.println("Value of a: " + a); // increment operator result1 = ++a; System.out.println("After increment: " + result1); System.out.println("Value of b: " + b); // decrement operator result2 = --b; System.out.println("After decrement: " + result2); ) )
Output
Value of a: 12 After increment: 13 Value of b: 12 After decrement: 11
In the above program, we have used the ++ and -- operator as prefixes (++a, --b). We can also use these operators as postfix (a++, b++).
There is a slight difference when these operators are used as prefix versus when they are used as a postfix.
To learn more about these operators, visit increment and decrement operators.
6. Java Bitwise Operators
Bitwise operators in Java are used to perform operations on individual bits. For example,
Bitwise complement Operation of 35 35 = 00100011 (In Binary) ~ 00100011 ________ 11011100 = 220 (In decimal)
Here, ~
is a bitwise operator. It inverts the value of each bit (0 to 1 and 1 to 0).
The various bitwise operators present in Java are:
Operator | Description |
---|---|
~ | Bitwise Complement |
<< | Left Shift |
>> | Right Shift |
>>> | Unsigned Right Shift |
& | Bitwise AND |
^ | Bitwise exclusive OR |
These operators are not generally used in Java. To learn more, visit Java Bitwise and Bit Shift Operators.
Other operators
Besides these operators, there are other additional operators in Java.
Java instanceof Operator
The instanceof
operator checks whether an object is an instanceof a particular class. For example,
class Main ( public static void main(String() args) ( String str = "Programiz"; boolean result; // checks if str is an instance of // the String class result = str instanceof String; System.out.println("Is str an object of String? " + result); ) )
Output
Is str an object of String? true
Here, str is an instance of the String
class. Hence, the instanceof
operator returns true
. To learn more, visit Java instanceof.
Java Ternary Operator
The ternary operator (conditional operator) is shorthand for the if-then-else
statement. For example,
variable = Expression ? expression1 : expression2
Here's how it works.
- If the
Expression
istrue
,expression1
is assigned to the variable. - If the
Expression
isfalse
,expression2
is assigned to the variable.
Let's see an example of a ternary operator.
class Java ( public static void main(String() args) ( int februaryDays = 29; String result; // ternary operator result = (februaryDays == 28) ? "Not a leap year" : "Leap year"; System.out.println(result); ) )
Output
Leap year
In the above example, we have used the ternary operator to check if the year is a leap year or not. To learn more, visit the Java ternary operator.
Nüüd, kui olete Java-operaatorite kohta teadlik, on aeg teada operaatorite hindamise järjekorda. Lisateabe saamiseks külastage Java Operator Preedence lehte.