There are various operators defined in Java language to carry out different operations with the Variables in Java. If you know any of the programming language, most of the operators in Java are self explanatory. Operators defined in Java and their precedence level are shown in the below table.

[caption id="attachment_451" align="aligncenter" width="451"] Source https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html[/caption]

Following example shows the usage of most of the above operators in Java.

 

package info.FiveBalloons.Examples;

public class OperatorsExample {
	
	public static void main(String args[]){
		
		//Arithmetics Operations
		int result = 20 + 10;
		System.out.println("20 + 10 = "+ result);
		
		result = 20 - 10;
		System.out.println("20 - 10 ="+ result);
		
		result = 20 * 10;
		System.out.println("20 * 10 ="+ result);
		
		result = 20 / 10;
		System.out.println("20 / 10 ="+ result);
		
		result = 20 % 8;
		System.out.println("20 % 8 ="+ result);
		
		//Assignment Operations
		result += 20;
		System.out.println("Result = Result + 20 "+ result);
		
		result -= 20;
		System.out.println("Result = Result - 20 "+ result);
		
		result *= 20;
		System.out.println("Result = Result * 20 "+ result);
		
		result /= 20;
		System.out.println("Result = Result / 20 "+ result);
		
		result %= 20;
		System.out.println("Result = Result % 20 "+ result);
		
		//Unary Operations
		
		//First execution then increment
		System.out.println("Result = Result + 1 "+result++);
		
		//First execution then decrement
		System.out.println("Result = Result - 1 "+result--);

		//First increment then print
		System.out.println("Result = Result + 1 "+(++result));
		//First decrement then print
		System.out.println("Result = Result - 1 "+(--result));
		
		
		//Relational and Equality Operators
		if(result > 10){
			System.out.println("Result is greater than 100");
		}
		if(result < 10){
			System.out.println("Result is less than 100");
		}
		if(result == 10){
			System.out.println("Result is equal to 10");
		}
		if(result != 10){
			System.out.println("Result is not equal to 10");
		}
	}
	

}

Please refer to the oracle documentation for the summary of operators. https://docs.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html

Comments