Hi There , Now talk about Java Basic Operators. See the below Table for Operator Summary
Postfix operators |
[] . (parameters) 1++ 1-- |
Unary prefix operators |
++1, --1, +1, -1, ~ ! |
Unary prefix creation and cast |
new (type) |
Multiplicative |
* / % |
Additive |
+ - |
Shift |
<< >> >>> |
Relational |
< <= > >= |
Equality |
== != |
Bitwise/logical AND |
& |
Bitwise/logical XOR |
^ |
Bitwise/logical OR |
| |
Conditional AND |
&& |
Conditional OR |
|| |
Conditional |
?: |
Assignment |
= += -= *= /= %= <<= >>= >>>= &= ^= | |
Increment(++) and Decrement(--) Operators
Now we Test Postfix & prefix operators . 1++ ,++1
💀 Postfix operators . 1++ ,1--
Postfix increment operator has like this semantics.
Ex: i++ , 10++ , i--, 10--
i++ uses the current value of i as the value of the expression first, then adds 1 to i. i-- uses the current value of i as the value of the expression first, then subtracts 1 to i. It is equivalent to the following statements like this.
Simple Code Below.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Postfix { | |
public static void main(String args[]){ | |
int i = 5 ; | |
System.out.println(i++); // Output is 6 but its incresed after print | |
System.out.println(i--); // Output is 4 but its Decrement after print | |
} | |
} |
💀 prefix operators . ++1 , --1
Prefix increment operator has like this semantics.
Ex: ++i , ++8 , --i , --8
Simple Code Below.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class prefix { | |
public static void main(String args[]){ | |
int i = 5 ; | |
System.out.println(++i); // Output is 6 | |
System.out.println(--i); // Output is 4 | |
} | |
} |
Arithmetic Operator 's. + - * / %
See This is Simple Code Below.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Additive { | |
public static void main(String args[]){ | |
System.out.println(4 + 2 ); // Output : 6 | |
System.out.println(4 - 2 ); // Output : 2 | |
System.out.println( 4 * 2 ); // Output : 8 | |
System.out.println(4 / 2 ); // Output : 2 | |
System.out.println(3 % 2 ); // Output : 1 3/2 = 1 Output the division remainder | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Additive_1 { | |
public static void main(String args[]){ | |
int a = 4; | |
int b = 2; | |
System.out.println( a+ b ); // Output : 6 | |
System.out.println( a - b ); // Output : 2 | |
System.out.println( a * b ); // Output : 8 | |
System.out.println( a / b ); // Output : 2 | |
System.out.println( a % b ); // Output : 1 a % b = o Output the division remainder | |
} | |
} |
Relational & Equality Operator 's. <, <=, >, >= ,== , !=
Now we see How to use Relational Operator in code. Relational Operator Use How to match or what Relation in two variable or values . like below eg :
A Match B , A Bigger Than B , A Less Than B .
See This code :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Relational { | |
public static void main(String[] args) { | |
int a = 25 ; | |
int b = 25 ; | |
// <, <=, >, >= ,== , != | |
if( a < b ){ | |
//true | |
System.out.println("yes"); | |
}else{ | |
//false | |
System.out.println("No"); | |
} | |
if( a > b ){ | |
//true | |
System.out.println("yes"); | |
}else{ | |
//false | |
System.out.println("No"); | |
} | |
if( a<= b ){ | |
//true | |
System.out.println("yes"); | |
}else{ | |
//false | |
System.out.println("No"); | |
} | |
if(a>=b ){ | |
//true | |
System.out.println("yes"); | |
}else{ | |
//false | |
System.out.println("No"); | |
} | |
if(a==b ){ | |
//true | |
System.out.println("yes"); | |
}else{ | |
//false | |
System.out.println("No"); | |
} | |
if(a!=b ){ | |
//true | |
System.out.println("yes"); | |
}else{ | |
//false | |
System.out.println("No"); | |
} | |
} | |
} |
Boolean Logical Operator 's.
Boolean logical operators include the unary operator ! (logical complement) .
The binary operators
& (logical AND),
| (logical inclusive OR),
^ (logical exclusive XOR,
~ ( logical complement).
Boolean logical operators can be applied
to Boolean operands, Returning a Boolean value.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Boolean_and { | |
public static void main(String args[]){ | |
int a = 5 ; | |
int b = 4 ; | |
if( a & b ){ | |
//true | |
System.out.println("yes"); | |
}else{ | |
//false | |
System.out.println("No"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Boolean_com { | |
public static void main(String args[]){ | |
int a = 5 ; | |
int b = 4 ; | |
if( a ~ b ){ | |
//true | |
System.out.println("yes"); | |
}else{ | |
//false | |
System.out.println("No"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Boolean_or { | |
public static void main(String args[]){ | |
int a = 5 ; | |
int b = 4 ; | |
if( a | b ){ | |
//true | |
System.out.println("yes"); | |
}else{ | |
//false | |
System.out.println("No"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Boolean_xor { | |
public static void main(String args[]){ | |
int a = 5 ; | |
int b = 4 ; | |
if( a & b ){ | |
//true | |
System.out.println("yes"); | |
}else{ | |
//false | |
System.out.println("No"); | |
} | |
} | |
} |
How to use java Shift Operators: <<, >>, >>>
In the above example :
24 is = 32
25 is =64
26 is =128
See the below code :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Shift_Operators { | |
public static void main(String args[]){ | |
int i = 2; | |
int result = i << 4; // 2 * 2* 2* 2 *2 32 | |
System.out.println(result); | |
int a = 32; | |
int result_a = a << 4; // 32 >>>>>> 2 * 2* 2* 2 *2 = 2 | |
System.out.println(result_a); | |
} | |
} |
The Conditional Operator : ?
condition ? expression_1 : expression_2
Condition is false output expression_2
boolean Ans = false;
int i = Ans ? 1 : 0; // 0
Condition is true output expression_1
boolean Ans = true;
int i = Ans ? 1 : 0; // 1
See the below code :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Conditional_opr { | |
public static void main(String args[]){ | |
boolean Ans = false; | |
int i = Ans ? 1 : 0; // 0 | |
System.out.println(i); | |
//----------------------------------------- | |
boolean Ans1 = true; | |
String a = Ans1 ? "yes" : "no"; // 1 | |
System.out.println(a); | |
} | |
} |
Tags:
Java