Thursday 28 December 2017

1.5 Operators

1. Short Hand Operators += , -=, *=,  /=

int a=4;
a+=1; //a=a+1 short hand operator example
System.out.println(a);



2. Binary Literals 
To print binary to decimal we can use "0b" or "0B"
int n= 0b1001;
int n= 0B1001;
underscore is introduced in java 7

3. Pre-increment and Post-increment

int i=5;
i=i++;  Here i++ means
temp=i and increment
i=temp and increment
value of i will be 5

int i=5;
int j=i++;
here i++ means
temp=i and increment
j=temp and increment
value of j will be 5 and i will be 6. 


4. Bitwise AND &  , OR |

int a=25;
int b=15;

int c=a & b;
a=25 1 1 0 0 1
b=15 0 1 1 1 1
c=9   0 1 0 0 1

int d= a | b;
a=25 1 1 0 0 1
b=15 0 1 1 1 1
c=31 1 1 1 1 1

5. LeftShift << and RightShift >> operator

int a=8;
int b= a << 2;

b=32 it will add two more zeros at the end
a=8  1 0 0 0 left shift 1 0 0 0 0 0 which is the binary for 32

int a=25;
int b= b >> 2;

b=6 it will delete the last two digits
a=25 1 1 0 0 1 right shift 0 0  1 1 0 which is the binary for 6

6. Boolean (True or False)

boolean b=true;
System.out.println(b); // true
b=!b;
System.out.println(b); // false

No comments:

Post a Comment