How to Java programming- Java operators.

Java operators are symbols that are used to perform operations. There are many types of operators in java such as unary operator, arithmetic operator, a relational operator, shift operator, bitwise operator, ternary operator and assignment operator.

 

1.Unary operator.

++ (Increment) Increases the value of operand by 1.
— (Decrement) Decreases the value of operand by 1.

 

Unary operators Example
	package FirstPackage; 
	public class MyFirstClass {
		public static void main(String[] args){
			// T000 Auto-generated method stub 
			// Unary Operators 
			int x = 10; 
			int y = 20; 
			x = ++x;
                        y = --y;
			System.out.println(x);
			System.out.println(y); 
		}
	}

 

Output
	11
	19

2.Arithmetic Operators.

+ (Addition) Adds values on either side of the operator.
– (Subtraction) Subtracts right-hand operand from left-hand operand.
* (Multiplication) Multiplies values on either side of the operator.
/ (Division) Divides left-hand operand by right-hand operand.
% (Modulus) Divides left-hand operand by right-hand operand and returns the remainder.

 

Arithmetic operators Example
	public class MyFirstClass{
		public static void main(String[] args){
			 // TODO Auto-generated method stub 
			 // Arithmetic Operators 
			 int x = 10; 
			 int y = 20; 
			 int z = x + y; 
			 System.out.println(z); 
			 z= y-x; 
			 System.out.println(z); 
			 z=x*y; S
			 ystem.out.println(z); 
			 z=y/x; 
			 System.out.println(z); 
			 z=y%x; 
			 System.out.println(z); 
		}
	}
Output
	30
	10
	200
	2
	0

 

3.Relational Operators.

== (equal to) Checks if the values of two operands are equal or not, if yes then condition becomes true.
!= (not equal to) Checks if the values of two operands are equal or not, if values are not equal then condition becomes true.
> (greater than) Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.
< (less than) Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.
>= (greater than or equal to) Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.
<= (less than or equal to) Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.

Relational operators Example
	package FIRstPackage;
	public class MyFirstClass {
		public static void main(String[] args) {
			// TODO Auto-generated method stub 
			// Relational Operators 
			int x = 10; 
			int y = 20; 
			if(x == y){
				System.out.println("X and Y are equal");
			}else{
				System.out.println("X and Y are not equal"); 
			}	
		}	
	}
Output
	X and Y are not equal

 

4.Shift operators.

<< (left shift) Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand.
>> (right shift) Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand.
>>> (zero fill right shift) Shift right zero fill operator. The left operands value is moved right by the number of bits specified by the right operand and shifted values are filled up with zeros.

 

5.Bitwise operators.

& (bitwise and) Binary AND Operator copies a bit to the result if it exists in both operands.
| (bitwise or) Binary OR Operator copies a bit if it exists in either operand.
^ (bitwise XOR) Binary XOR Operator copies the bit if it is set in one operand but not both.
~ (bitwise compliment) Binary One’s Complement Operator is unary and has the effect of ‘flipping’ bits.
&& (logical and) Called Logical AND operator. If both the operands are non-zero, then the condition becomes true.
|| (logical or) Called Logical OR Operator. If any of the two operands are non-zero, then the condition becomes true.
! (logical not) Called Logical NOT Operator. Use to reverses the logical state of its operand.

 

Bitwise operators Example
	public class MyFirstClass {
		public static void main(String[] args) { 
			// TODO Auto-generated method stub 
			// Bitwise Operators 
			int x = 10; 
			int y = 20; 
			int z = x & y; 
			/*0000 1010 + =>10 
                         *0001 0100   =>20  
                         *0000 0000   =>0 */ 
			System.out.println(z); 
                }
       }
Output
	0

6.Ternary operators.

? : (Conditional) This operator consists of three operands and is used to evaluate Boolean expressions. The goal of the operator is to decide, which value should be assigned to the variable.

 

7.Assignment operators.

= (Simple assignment operator). Assigns values from right side operands to left side operand.
+= (Add AND assignment operator). It adds right operand to the left operand and assigns the result to left operand.
-= (Subtract AND assignment operator). It subtracts right operand from the left operand and assigns the result to left operand.
*= (Multiply AND assignment operator.) It multiplies right operand with the left operand and assigns the result to left operand.
/= (Divide AND assignment operator.) It divides left operand with the right operand and assign the result to left operand.
%= (Modulus AND assignment operator.) It takes modulus using two operands and assigns the result to left operand.
<<= (Left shift AND assignment operator.)
>>= (Bitwise AND assignment operator.)
&= (Right shift AND assignment operator.)
^= (bitwise exclusive OR and assignment operator.)
|= (bitwise inclusive OR and assignment operator.)

Anwar Yakkiparamban

Anwar Yakkiparamban is the founder of Lauyou Learning. Prior to Lauyou learning, Anwar worked at ARD Engineering & Development, Qatar. He holds bachelor degree in Electronics and Communication Engineering from Govt. Engineering College Idukki.

You may also like...