Basic Java – 9 || Performing multiple operations using Java Operators.

          Java Operators used to perform data manipulation while programming. Although there are multiple Class and Methods present in the Java library to perform such operations. We have some java operators which are used to perform basic calculation and operations.

Operation Types

  1. Arithmetic Operations: To perform the basic calculation on numeric values.
  2. Relational Operations: To perform the comparison between two values.
  3. Logical Operations: To perform the action based on multiple conditions.
  4. Assignment Operations: To assign a value to a variable.
  5. Ternary Operations: Single statement to check any condition and perform the action.
  6. Bitwise Operations: To perform operations on bit values.
Java Operators
Java Operators
Related Links:

OOPs Concept:

Java Question And Answer:

Code Example of Arithmetic Operators

public class Arithmetic {
	public static void main(String[] args) 
	{
		int a=20;
		int b=30;
		int c=7;
		
		System.out.println("Arithmetic Operations on variables a and b");
		System.out.println(a+b); // Returns sum of two values.
		System.out.println(b-a); // Returns difference between two values.
		System.out.println(a*b); // Returns multiplication of two values.
		System.out.println(a/c); // Divide two values and returns Remainder.
		System.out.println(a%c); // Divide two values and returns Quotient.
}
}
Output:
Arithmetic Operations on variables a and b
50
10
600
2
6

Along with the above basic operation, there are two more arithmetic operators present:

  1. Increment(++): Post Increment and Pre Increment
  2. Decrement(–): Post Decrement and Pre Decrement

      Combination of two consecutive addition(+) symbols increases a value by 1. On the other hand, two consecutive negative(-) symbols decrease value by 1.

public class Arithmetic {
	public static void main(String[] args) 
	{
		int a=20;
		int b=30;
			
		// ++ increment
		// -- decrement
		System.out.println("\nPre Increment and Decrement Operations");
		System.out.println(++a);
		System.out.println(- -b);
		
		System.out.println("\nPost Increment and Decrement Operations");
		System.out.println(a++);
		System.out.println(b- -);
	}
}

Output:
Pre Increment and Decrement Operations
21
29
Post Increment and Decrement Operations
21
29

            Always remember in Pre Increment/Decrement value gets change before the current line termination or end of the statement. In the above example, the value of a get increased by 1 value and get printed on the console.

          On the other hand, in Post Increment/Decrement value gets changed only after the execution of current line get finished. And hence value doesn’t get the change in last two statements.

Code Example of Relational Operators

public class Relational {
	public static void main(String[] args) 
	{
		int a=10;
		int b=20;
		int c=10;
		
		if(a<b)
		{
			System.out.println("Value of a is less than b.");
		}
		
		if(a==c)
		{
			System.out.println("Value of a is equal to c.");
		}
		// Same way we can use other relational operators.
	}
}

Code Example of Logical Operators

public class Logical {
	public static void main(String[] args) {
		int a=10;
		int b=20;
		
		// Result of both conditions should be true
		if((a!=0) && (a<b))
		{
			System.out.println("&& - Value of a is less than b and not equal to 0.");
		}
		
		// Result of at least one condition(s) should be true.
		if((a==0) || (a<=10))
		{
			System.out.println("|| - Value of a  is not equal to 0 but less than 10.");
		}
		
		// ! operator reverses the result of condition.
		// If result is True ! converts the same in False.
		// If result is False ! converts the same in True.
		if(!(a==12))
		{
			System.out.println("! - Value of a is not equal to 12, still result of condition get reversed.");
		}
	}
}
Output:
&& - Value of a is less than b and not equal to 0.
|| - Value of a  is not equal to 0 but less than 10.
! - Value of a is not equal to 12, still result of condition get reversed.

Code Example of Assignment Operators

public class Assignment {
	public static void main(String[] args) {
		// Assign value to a variable
		int a=10;
		
		System.out.println("Value of a: "+a);
		
		// Following statement is same as 'a=a*5'
		// First it multiplies value of "a with 5"
		// Then it assign new value to variable "a"
		a*=5;
		System.out.println("Value of a after increment: "+a);
		
		// The same way we can use other Arithmetic Operators.
	}
}
Output:
Value of a: 10
Value of a after increment: 50

Code Example of Ternary Operator

Ternary Operator takes 3 arguments, first, it checks the condition, the second argument gets executed if the condition is true and third argument get executed if the condition is false.

public class Ternary {
	
	public static void main(String[] args)
	{
		int a=10;
		int b=20;
		
		// First argument verify condition.
		// Second statement execute when condition is True.
		// Third statement execute when condition is False.
		String result=(a>b)?"True- value of a is greater than b.":"False- value of a is less than b.";
		
		System.out.println(result);	
	}
}
Output:
False- value of a is less than b.

Code Example of Bitwise Operators

public class Bitwise {
	public static void main(String[] args) 
	{
		int a=10;
		int b=15;
		
		// Performed operations on bits of numeric values.
		System.out.println("Numeric value of a  :"+a);
		System.out.println("Numeric value of b  :"+b);
		
		System.out.println("\nBinary value of a   :"+Integer.toBinaryString(a));
		System.out.println("Binary value of b   :"+Integer.toBinaryString(b));
		
		System.out.println("\nBinary value of a&b :"+Integer.toBinaryString(a&b));
		System.out.println("Binary value of a|b :"+Integer.toBinaryString(a|b));
		System.out.println("Binary value of a^b :"+Integer.toBinaryString(a^b));
		System.out.println("Binary value of ~a  :"+Integer.toBinaryString(~a));
		
		System.out.println("\n'Bitwise AND' of 10 and 15 is ="+(a&b));
		System.out.println("'Bitwise OR' of 10 and 15 is  ="+(a|b));
		System.out.println("'Bitwise XOR' of 10 and 15 is ="+(a^b));
		System.out.println("'Bitwise Complement' of 10 is ="+(~a));
	}
}
Output:
Numeric value of a  :10
Numeric value of b  :15
Binary value of a   :1010
Binary value of b   :1111
Binary value of a&b :1010
Binary value of a|b :1111
Binary value of a^b :101
Binary value of ~a  :11111111111111111111111111110101
'Bitwise AND' of 10 and 15 is =10
'Bitwise OR' of 10 and 15 is  =15
'Bitwise XOR' of 10 and 15 is =5
'Bitwise Complement' of 10 is =-11

2811total visits,2visits today

Leave a Reply

Your email address will not be published. Required fields are marked *