Java Basic Operators - Operator Summary Table

 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 ii-- uses the current value of  as the value of the expression first, then subtracts 1 to i. It is equivalent to the following statements like this.

    Simple Code Below.
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
}
}
view raw Postfix.java hosted with ❤ by GitHub
    
💀 prefix  operators .  ++1 , --1
  Prefix increment operator has like this semantics.
    Ex:     ++i , ++8 , --i , --8

    Simple Code Below.
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
}
}
view raw prefix.java hosted with ❤ by GitHub


Arithmetic Operator 's.      +  -  *  /  %

See This is Simple Code Below.

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
}
}
view raw Additive.java hosted with ❤ by GitHub
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
}
}
view raw Additive_1.java hosted with ❤ by GitHub


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 :


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");
}
}
}
view raw Relational.java hosted with ❤ by GitHub




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, ReturningBoolean value.
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");
}
}
}
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");
}
}
}
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");
}
}
}
view raw Boolean_or.java hosted with ❤ by GitHub
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: <<, >>, >>> 

Shift left

a << n

Shift all bits in a left n times, filling with 0 from the right.

Shift right with sign bit

a >> n

Shift all bits in a right n times, filling with the sign bit from the left.

Shift right with zero fill    

a >>> n

Shift all bits in a right n times, filling with 0 from the left.

In the above example :

              24 is  = 32

              25 is  =64

              26 is  =128

See the below code :

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 :

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);
}
}


Post a Comment

Previous Post Next Post

Featured Post