- A symbol that performs a specific kind of operation on one, two, or three operands, and produces a result,
- Operator based on the number of operands they operate on
- unary:
a++;, - binary:
a = b;, - ternary:
mx = a > b?a:b;,
- unary:
- Operator has
precedencejust like other languages,
-
Compile time errorwill be shown if right side is not assignment compatible to left side. Ex:float f = 1.19F; int i = 15; i = f; // compile time error. i = (int)f; // ok
-
num = 25;- Assigns 25 to
num, num = 25produces 25. Sonum1 = num2 = 25is validnum = 25is called an expression (no semicolon),num = 25;is called a statement.
- Assigns 25 to
-
Declaration,Initialization,Assignmentint count; // Declaration count = 0; // assignment int value = 5; // initialization
- Type of the result of
b @ cis evaluated as,double > float > long > int. It means that,- Suppose
cis of double andbis of long, thenb @ cwill be double. Got it?
- Ex:
But,
byte b1; b1 = 5; // ok --------------------(a)
byte b1; byte b2 = 2; // ok byte b3 = 3; // ok b1 = b2 + b3; // error -----------(b) b1 = 2 + 3; // ok -------------(c)
(a)is ok, because5is a constant which is within the-128 to 127,(b)is giving error, becauseb2+b3evaluate tointandintcan't be assigned to abytedirectly,(b)follows data type of expression,(c)is ok because2,3arecompiled time constantand2+3is calculated at compile time and works likeb1=5.
- like
CorC++
- Ex:
byte b1 = 10; byte b2 = +5; b1 = b2; // Ok. byte to byte assignment b1 = +b2 // error. bcause +b2 convert it into int. Remember Expression data type
- Unary minus(
-) is same like Unary plus(+),
+=,-+,*=,/=,%=,- These operators are
fasterandefficient,
-
++a:- First incremented, then
- Other operation,
-
a++:- Other one operation, then
- increment, then
- others operation,
-
Ex-1:
int i = 100; int j = ++i + 15;
i = 101,j = 116. Rememberpre incrementis incremented first, -
Ex-2:
int i = 100; int j = i++ + 15;
Steps be like:
j = 100 + 15; // i = 100 j = 100 + 15; // i = 101 j = 115; // i = 101 -
Ex-3:
int i = 15; i = i++;
Steps be like:
i = 15; // i = 15 i = 15; // i = 16 i = 15; // ans
--a,a--- Similar to increment rules.
-
==,!=,>,>=,<,<=, -
These are
binaryoperator, -
For
primitiveoperands, it returns true if the both operands represent thesame value, -
For
referenceoperands, it returns true if the both operandsrefer to the same objectin memory, -
Alert while using these operators with reference type,
-
Ex:
Kuetian st1 = new Kuetian(75,"CSE","Pantho"); Kuetian st2 = new Kuetian(57,"CSE","Saeed"); Kuetian st3 = new Kuetian(57,"CSE","Saeed"); System.out.println(st1 == st2); // false, because referring different object System.out.println(st2 == st3); // false, because referring different object st1 = st3; System.out.println(st1==st3); // true, since same object
-
Let's get confused a little
Integer num1 = 100; Integer num2 = 100; System.out.println(num1==num2); // true Integer num3 = 10000; Integer num4 = 10000; System.out.println(num3==num4); // false
Just remember this
num1,num2,num3,num4are object of classInteger. Rest will be discussed later in different section. -
So, for reference data type, always use
obj1.equlas(obj2)instead of==.Integer num3 = 10000; Integer num4 = 10000; System.out.println(num3.equals(num4)); // true
- Logical AND Operator (
&), - Short-Circuit AND Operator (
&&), - Logical OR Operator (
|), - Logical Short-Circuit OR Operator (
||), - Logical XOR Operator (
^), - Compound Boolean Logical Assignment Operators:
&=,|=,^= Logicalandshort circuitoperator do same thing, only difference is short-circuit operator doesn't execute other condition if not needed,- Ex:
int i = 24; int j = 48; int k = 57; int count = 0; boolean outputFromLogical = (i<48) || (j == 48 ) || (++count < 1000); --------(a) System.out.println(outputFromLogical + " -> " + count); // true -> 0 boolean outputFromShortCircuit = (i<48) | (j == 48 ) | (++count < 1000); -----(b) System.out.println(outputFromShortCircuit + " -> " + count); // true -> 1 boolean outputAnd = (i == j) && (i++ ==k); --------(c) System.out.println(outputAnd+" "+i); // false 24 boolean outputShortAnd = (i == j) & (i++ ==k); ----(d) System.out.println(outputShortAnd+" "+i); // false 25
- For
(a), it is not needed to check2ndand3rdcondition. Because1stistrue, output will be true sinceORoperation, - For
(b), it will execute all conditions, surely. so count increases here, - Same for
(c)&(d),
- For
- Ex:
// boolean-expression ? true-expression : false-expression int a = 5; int b = 55; int mx = a>b ? a : b;
- Try to avoid this operator,
- Write code in such a way that, precedence doesn't affect calculation,
- Operators are:
++,--,+, -,~ Bitwise complement,!,(type) Cast,*, /, %,+, -,+ String concatenation,<< Left shift,>> Signed right shift,>>> Unsigned right shift,<,<=,>,>=,instanceof Type comparison,==,!=,& Bitwise AND,& Logical AND,^ Bitwise XOR,^ Logical XOR,| Bitwise OR,| Logical OR,&& Logical short-circuit AND,|| Logical short-circuit OR,?:,= Assignment,- (
+=, -=, *=, /=, %=, <<=, >>=, >>>=, &=, |=,^=),
- Perform operation on
bit level. Works with onlyintegers, - Operators are:
& Bitwise AND,| Bitwise OR,^ Bitwise XOR,~ Bitwise complement (1’s complement),<< Left shift,>> Signed right shift,>>> Unsigned right shift,&=, !=, ^=, <<=, >>=, >>>= Compound assignment,