Operators in Java

Operators in JAVA

Operator are the symbols that are used to manipulate the data items. Different types of operators are available in java. Some of them are discussed below.

  • Arithmetic Operators
  • Increment or decrement operators
  • Relational Operators
  • Logical Operators
  • Assignment Operators
  • Conditional Operator

Precedence of operators

Precedence of operators means when several operations occur in an expression, which one will be evaluated first? Next what? It means the priority. So, each part is evaluated and resolved in a previously designed order called operator precedence. Usually we use the BODMAS rule in normal calculation. But here it is something like that. This affects how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator.

Example

Now, let us evaluate the statements below

class student

{

public static void main(String[] args)

{
int x=15-3*2;

System.out.println(x);

}

}

now can you find what is the value of x?

will it be 24 ? (After deduction of 3 from 15 we got 12 and then this is multiplied by 2)

will it be 9 ? (After multiplication of 3 and 2 we got 6 and deduct same from 15)

here 3 is sharing both “*” and “-“. So when two operators share a common operand, the operator with the highest precedence is operated first. In java “*” has higher precedence than “–“ that is why it will be operated first. And thus result will be 9.

Another example

Class student

{

public static void main(String[] args)

{

 int a = 10, b = 5, c = 1, result;

 result = a-++c-++b;

 System.out.println(result);

}

}

If it is executed the output will be 2

Let us see how?

The operator precedence of prefix “++” is higher than  “-“ operator.

So the expression a-++c-++b; is equivalent to result = a-(++c)-(++b);

So, when dealing with multiple operators and operands in an single expression, you can use parentheses for clarity.

Arithmetic Operators

Arithmetic operators are called binary operator. They require one operator and two operands (on which operator works)

Details about arithmetic operators are given below with different programming examples

+             – addition

             – subtraction

*             – multiplication

/              – division

%            – modulo (this is used to keep the reminder)                                        

Programming examples of arithmetic operators

þ Addition example    

Write a java program to add two numbers

public class student

{

        public static void main(String []args)

           {

               int  x=12;      

               int y= 20;

               int z;

               z= x+y;

             System.out.println(x);

             System.out.println(y);

             System.out.println(“Result is “+z);

          }

}

Output

12

20

Result is 32

þ Subtraction example

Write a java program to subtract two numbers

public class student

{

        public static void main(String []args)

           {

               int  x=34;      

               int y= 19;

               int z;

               z= x-y;

             System.out.println(x);

             System.out.println(y);

             System.out.println(“Result is “+z);

          }

}

Output

34

19

Result is 15

þ Multiplication example

Write a java program to multiply two numbers

public class student

{

        public static void main(String []args)

           {

               int x=34;      

               int y= 5;

               int z;

               z= x*y;

                System.out.println(x);

                System.out.println(y);

                System.out.println(“Result is “+z);

          }

}

Output

34

5

Result is 170

þ Division example

Write a java program to divide two numbers

public class student

{

        public static void main(String []args)

           {

               int  x=12;      

               int y= 20;

               int z;

               z= y/x;

             System.out.println(y);

             System.out.println(x);

             System.out.println(“Result is “+z);

          }

}

Output

20

12

Result is 1

þ Modulo example

Write a java program to divide two numbers and keep the reminder

public class student

{

        public static void main(String []args)

           {

               int  x=12;      

               int y= 20;

               int z;

               z= y%x;

             System.out.println(y);

             System.out.println(x);                                                                     

             System.out.println(“Result is “+z);

          }

}

Output

20

12

Result is 8

Increment or decrement operators

Increment or decrement operators are called unary operators. They require one operator and one operator. ++ Means to increase the value of the variable by 1 and – – means to decrease the value of the variable by 1

Details about increment or decrement operators are given below with different programming examples

++           – increment

             – decrement

Programming examples of increment and decrement operators

þ Increment example

public class student

{

        public static void main(String []args)

           {

               int  x=12;     

               x++;

               System.out.println(“Result is “+x);

          }

}

Output

Result is 13

Another increment example (Post increment)

public class student

{

        public static void main(String []args)

           {

               int  x=12;     

               int y;

               y=x++;

               System.out.println(“Result is “+x);

               System.out.println(“Result is “+y);

          }

}

Output

Result is 13

Result is 12

Another increment example (Pre increment)

public class student

{

        public static void main(String []args)

           {

               int  x=12;     

               int y;

               y=++x;

               System.out.println(“Result is “+x);

               System.out.println(“Result is “+y);

          }

}

Output

Result is 13

Result is 13

þ Decrement example

public class student

{

        public static void main(String []args)

           {

               int  x=12;     

               x–;

               System.out.println(“Result is “+x);

          }

}

Output

Result is 11

Another Decrement example (Post decrement)

public class student

{

        public static void main(String []args)

           {

               int  x=12;     

               int y;

               y=x–;

               System.out.println(“Result is “+x);

               System.out.println(“Result is “+y);

          }

}

Output

Result is 11

Result is 12

Another Decrement example (Pre decrement)

public class student

{

        public static void main(String []args)

           {

               int  x=12;     

               int y;

               y=–x;

               System.out.println(“Result is “+x);

               System.out.println(“Result is “+y);

          }

}

Output

Result is 11

Result is 11

NB – When increment and decrement operators are assigning its value to other variable or printing its value written as above program segments :- in case of post increment first it assigns its value then increment itself by 1. And in case of pre increment first in increases its value by 1 and then it assigns value. Same thing is applicable for decrement operators.

In the above example y=x++; is called post increment. First value of x will be assigned to y and then the value of x will be increased by 1. y=+++; is called pre increment. First value of x is will be increased by 1 and then the result will be assigned to y. same thing is applicable for decrement operators.

Relational Operators

Relational operators are also called binary operators. They require one operator and two operands. They return Boolean value either true or false.

Details about relational operators are given below with different programming examples

<             – less than

>             – greater than

<=           – less than or equal to

>=           – greater than or equal to

==           – equal to (checks for equality)

!=            – not equal to

Programming examples of relational operators

þ Less than example

public class student

{

        public static void main(String []args)

           {

               int  x=12;

               int y=30;

               boolean z;

               z=x<y;

               System.out.println(“Result is “+z);

          }

}

Output

Result is true

þ Greater than example

public class student

{

        public static void main(String []args)

           {

               int  x=12;

               int y=30;

               boolean z;

               z=x>y;

               System.out.println(“Result is “+z);

          }

}

Output

Result is false

þ Equal to example

public class student

{

        public static void main(String []args)

           {

               int  x=30;

               int y=30;

               boolean z;

               z=x==y;

               System.out.println(“Result is “+z);

          }

}

Output

Result is true

þ Not equal to example

public class student

{

        public static void main(String []args)

           {

               int  x=12;

               int y=30;

               boolean z;

               z=x!=y;

               System.out.println(“Result is “+z);

          }

}

Output

Result is true

public class student

{

        public static void main(String []args)

           {

               int  x=12;

               int y=12;

               boolean z;

               z=x!=y;

               System.out.println(“Result is “+z);

          }

}

Output

Result is false

Logical Operators

Logical operators are also called binary operators. They require one operator and two operands. They return Boolean value either true of false

Details about logical operators are given below with different programming examples

&&         – logical and

||           – logical or

!              -logical not

Programming examples of logical operators

þ Logical “and” example

public class student

{

        public static void main(String []args)

           {

               boolean  x=true;

               boolean  y=false;

               boolean z;

               z=x &&y;

               System.out.println(“Result is “+z);

          }

}

Output

Result is false

þ Logical or example

public class student

{

        public static void main(String []args)

           {

               boolean  x=true;

               boolean y=false;

               boolean z;

               z=x || y;

               System.out.println(“Result is “+z);

          }

}

Output

Result is true

þ Logical not example

public class student

{

        public static void main(String []args)

           {

               boolean  x=true;

               boolean y=false;

               boolean z;

               z=x&&!y;

               System.out.println(“Result is “+z);

          }

}

Output

Result is true

Assignment Operators

These operators are also called binary operator. They require one operator and two operands. It assigns the right hand value to left hand value.

Details about assignment operators are given below with different programming examples

Simple assignment

 =            – equal to

Programming examples of assignment operators

þ Equal to

public class student

{

        public static void main(String []args)

           {

               int  x=12,y;     

               y=x;

               System.out.println(“Result is “+x);

               System.out.println(“Result is “+y);

          }

}

Output

Result is 12

Result is 12

Compound assignment

+=           -add and assign

 -=           -subtract and assign

*=           -multiply and assign

/=           -divide and assign

%=          -modulo and assign

Programming examples of compound assignment operators

þ Add and assign example

public class student

{

        public static void main(String []args)

           {

               int  x=12;     

               x+=10;       //x=x+10

               System.out.println(“Result is “+x);

          }

}

Output

Result is 22

þ Subtract and assign example

public class student

{

        public static void main(String []args)

           {

               int  x=34;     

               x-=5;         //x=x-5

               System.out.println(“Result is “+x);

          }

}

Output

Result is 29

þ Multiply and assign example

public class student

{

        public static void main(String []args)

           {

               int  x=12;     

               x*=10;

               System.out.println(“Result is “+x);

          }

}

Output

Result is 120

þ Divide and assign example

public class student

{

        public static void main(String []args)

           {

               int  x=12;      

               x/=10;

               System.out.println(“Result is “+x);

          }

}

Output

Result is 1

þ Modulo and assign example

public class student

{

        public static void main(String []args)

           {

               int  x=12;     

               x%=10;

               System.out.println(“Result is “+x);

          }

}

Output

Result is 2

Conditional Operator    

This operator is known as ternary operators as it requires three operands. This also works on Boolean value.

Details about conditional operator is given below with different programming examples

(? 🙂        – conditional operator

Programming examples of conditional operator

public class student

{

        public static void main(String []args)

           {

               int  x=10;

               int y=2;

               int  z;

               z=(x>y)? x : y;

               System.out.println(“Result is “+z);

          }

}

Output

Result is 10

Another Example of conditional operator

public class student

{

        public static void main(String []args)

           {

               int  x=10;

               int y=20;

               String  z;

               z=(x>y)? x+”is greater” : y+”is greater”;

               System.out.println(“Result is “+z);

          }

}                      

Output

Result is 20is greater

Type Casting

Before explaining about type casting let us try a program in corejava that is given below. Type this program and see the output.

import java.util.Scanner; 

class student

 public static void main(String args[])

  { 

   int a=20,b=9,c;

   c=a/b;

   System.out.println(c); 

 } 

}

Definitely the output will be 2. Because integer variable keeps only integer data.

Again let us modify the above program and declare c as float and see the output.

Definitely the output will be 2.0. Because float variable keeps only float data but getting integer from the expression.

Again let us modify the above program and we will do the type casting and we will write following code

c=(float)a/b;

in this case definitely the output will be 2.2222223

From the above explanation we got that assigning a value of one type to a variable of another type is known as Type Casting.

*****

Leave a Comment

Scroll to Top