Operators and Statement in java

Quick Reference :

  • The result of Division is always an Integer, if you need decimal you need to cast it into float
  • BODMAS – Brachet of Division, Multiplication, Addition, Subtraction

Order of Operator Precedence

OperatorSymbols
Post-unary operatorsexpression++, expression–
Pre-unary operators++expression, –expression
Other unary operators+, -, ~, !
Multiplication/Division/Modulus*, /, %
Addition/Subtraction+, –
Shift Operators<<, >>, >>>
Relational operators<, >, <=, >=, instanceof
Equal to/not equal to==, !=
Bitwise operators&, ^, |
Short-circuit logical operators&&, ||
Ternary operatorsboolean expression ? expression1 : expres-
sion2
Assignment operators= , += , -= , *= , /= , %= , &= , ^= , != , <<= , >>= , >>>=
  1. Unary Operator (expression++, expression–,++expression, –expression,+, -, ~(bitwise complement operator), !)
public class UnaryOperatorExample {
	public static void main(String[] args) {
		int a=10;  
		int b=10;  
		int c=-5;
		int d=12;
		System.out.println(a++ + ++a);//10+12=22  
		System.out.println(b++ + b++);//10+11=21
		System.out.println(~c);//4
		System.out.println(~d);//-13
		a=10;
		b=10;
		System.out.println(-a-++b+~c---~c);//-10-11+5-6
	}
}

2. Arithmatic Operator (*, /, %,+, -)

The multiplicative operators ( * , / , % ) have a higher order of precedence than the additive operators ( + , – ).

That means when you see an expression such as this:
int x = 2 * 5 + 3 * 4 – 8;
you first evaluate the 2 * 5 and 3 * 4 , which reduces the expression to the following:
int x = 10 + 12 – 8;

Then, you evaluate the remaining terms in left-to-right order, resulting in a value of x of
14 .

public class ArithmaticOperatpr {
	public static void main(String[] args) {
		int x = 2 * 5 + 3 * 4 - 8;
		System.out.println(x);
	}
}

3. Shift Operators is also a bitwise operator (<<(left shift) >>(right shift) >>>(right shift filled with zero))

Simple Example with Explanation

public class App {
    public static void main(String[] args) {
    	System.out.println( 8 << 2); //left shift operator
//    	i.e 8 = 1 0 0 0 and we are add 0 in last two bits then it becomes 1 0 0 0 0 0 which in decimal form is 32
    	System.out.println( 8 >> 2); //right shift operator 
//    	i.e 8 = 1 0 0 0 and we are removing last two bits then it becomes 1 which in decimal form is 1
    }
}
public class ShiftOperator {
	public static void main(String[] args) {
		{
			{
				int value = 100,shift =2;
				System.out.println((value<<shift) + " = "+ getDirectionShift(value,shift,"left"));//100*(2^2)
				System.out.println((value>>shift) + " = "+ getDirectionShift(value,shift,"right"));//100*(2^2)
			}
			{
				int value = 100,shift =33;
				System.out.println((value<<shift) + " = "+ getDirectionShift(value,shift,"left"));
				System.out.println((value>>shift) + " = "+ getDirectionShift(value,shift,"right"));
				//for 33 which is = 100001 but only last 5 values are considered for shift i.e 00001 = 1
			}
			{
				int value = 1000,shift =-12345;
				System.out.println((value<<shift) + " = "+ getDirectionShift(value,shift,"left"));
				System.out.println((value>>shift) + " = "+ getDirectionShift(value,shift,"right"));
				//for 12345 which is = 1100111111000111 but only last 5 values are considered for shift i.e 00111 = 7
			}
			
		}
	}
	public static int getDirectionShift(int x, int y,String direction) {
		if(direction.equalsIgnoreCase("left")) {
			return (int) (x*(Math.pow(2,getLastFiveBinary(y))));
		}else {
			return (int) (x/(Math.pow(2,getLastFiveBinary(y))));
		}
	}
	public static double getLastFiveBinary(int x) {
		String binary = Integer.toBinaryString(x);
		if (binary.length() > 5) {
			binary = binary.substring(binary.length()-4);
		} 
		return Integer.parseInt(binary,2);
	}
}

4. Bitwise Operator ( &, |, ^)

public class BitwiseOperator {
	public static void main(String[] args) {
		int a = 25; // 1 1 0 0 1
		int b = 15; // 0 1 1 1 1
		
		int c = a &amp; b; // 0 1 0 0 1   i.e 9
		int d = a | b; //  1 1 1 1 1    i.e 31
		int e = a ^ b; //x-or 1 0 1 1 0   i.e  22
		System.out.println(c);
		System.out.println(d);
		System.out.println(e);
	}
}

Continue and break statement

public class BreakNContinue {
	public static void main(String[] args) {
		for(int i=0;i<10;i++) {
			if(i==3)
				continue;
			if(i==7)
				break;
			System.out.println(i);
		}
		
		OUTER_LOOP: for(int j=0;j<3;j++) {
			INNER_LOOP: for(int i=0;i<10;i++) {
				if(i==3)
					continue OUTER_LOOP;
				System.out.println(j+ " " + i);
			}
		}
	}
}
Allows Optional labelsAllows break statementAllows continue statement
ifYes*NoNo
whileYesYesYes
do whileYesYesYes
forYesYesYes
switchYesYesNo
  • * Labels are allowed for any block statement, including those that are preceded with an if-then statement.

Concatenation (+ operator)

The + operator can be used in two ways within the same line of code, Rules of using + operator :

  • If both operands are numeric, + means numeric addition.
  • If either operand is a String , + means concatenation.
  • The expression is evaluated left to right.
public class ConcatOperator {
	public static void main(String[] args) {
		System.out.println(1+2); //3
		System.out.println("a"+"b"); //ab
		System.out.println("a"+"b"+3); //ab3
		System.out.println(1+2+"c"); //3c
	}
}

Q & A :

  1. Example : Guess the output
	public static void main(String[] args) {
		for(int i=0;i<10;) {
			i=i++;
			System.out.println(i);
		}
	}

2. Example : Guess the output

	public static void main(String[] args) {
		final char a = 'A', d = 'D';
		char grade = 'B';
		switch(grade) {
		case a:
		case 'C': System.out.print("good");
		case 'B': System.out.print("great");
		case d:
		case 'F': System.out.print("not good");
		}
	}

Leave a Comment