Related post

Operators in Java | Zhullyblog

 

Java operators


Operators In Java

An operator is a symbol or character used to perform an operation. Take a good look at this 

 A  +  B 

 " + " Is the operator used here.



Types of Operator in Java


 • Arithmetic Operator

 •  Assignment Operator

 • Auto - increment and decrement operator

 • Comparison Operator

 • Logical Operator

 • Bitwise operator

 • Ternary operator



 Arithmetic Operator


When we talk of arithmetic operator, we are talking about the general basic operator we use regularly to add , subtract , divide and multiply.

The operators are represented by + , - , * , / , % 

Let's take a Java example on this.




public class sample {

    public static void main(String[] args) {

    

        int number1 = 10, number2 = 5, result;

    

        // Using addition operator

        result = number1 + number2;

        System.out.println("number1 + number2 = " + result);

    

        // Using subtraction operator

        result = number1 - number2;

        System.out.println("number1 - number2 = " + result);

    

        // Using multiplication operator

        result = number1 * number2;

        System.out.println("number1 * number2 = " + result);


        // Using division operator

        result = number1 / number2;

        System.out.println("number1 / number2 = " + result);

    

        // Using remainder operator

        result = number1 % number2;

        System.out.println("number1 % number2 = " + result);

    }


}


Screenshot

Java arithmetic operator


Results




 Assignment Operator


Assignment operator are slightly different from the general arithmetic operator. They are += , -= , *= , /= , %= and = .

   X += Y

The assignment operator above us simply saying that x = x + y

Hope your u grab that, now let's go straight to the example.


public class sample {

public static void main(String args[]) {

      int num1 = 20;

      int num2 = 40;


      num2 = num1;

      System.out.println("= Result: "+num2);


      num2 += num1;

      System.out.println("+= Result: "+num2);

      

      num2 -= num1;

      System.out.println("-= Result: "+num2);

      

      num2 *= num1;

      System.out.println("*= Result: "+num2);

      

      num2 /= num1;

      System.out.println("/= Result: "+num2);

      

      num2 %= num1;

      System.out.println("%= Result: "+num2);

   }

}




Screenshot


Results





Auto Increment and decrement operator.


As the name implies, the operators job is to either auto-increase or auto-decrease the values given.

  If x = 5 and y = 8 and x is set to auto-increase and y is set to auto-decrease, then the value of x will be 6 and y will be 7.

This is simply x = x + 1 and y = y -1

Here is how to code in Java



//by Zhullyblog

public class sample {

public static void main(String args[]){

      int num1=5;

      int num2=10;

      //auto increment

      num1++;

      //auto decrement

      num2--;

      System.out.println("num1++ is: "+num1);

      System.out.println("num2-- is: "+num2);

   }

}


Screenshot

Operator in java

Results



Comparison Operator


Comparison Operator is used to compare two variables.

Let's take an instance in Java


//by Zhullyblog

public class sample {

public static void main(String args[]) {

      int num1 = 5;

      int num2 = 10;

      //check if num1 and num2 are equal

      if (num1==num2) {

 System.out.println("num1 and num2 are equal");

      }

      else{

 System.out.println("num1 and num2 are not equal");

      }

      //check if num1 and num2 are not equal

      if( num1 != num2 ){

 System.out.println("num1 and num2 are not equal");

      }

      else{

 System.out.println("num1 and num2 are equal");

      }

      //check if num1 is greater than num2

      if( num1 > num2 ){

 System.out.println("num1 is greater than num2");

      }

      else{

 System.out.println("num1 is not greater than num2");

      }

     //check whether num1 is less than num2

     if( num1 < num2 ){

 System.out.println("num1 is less than num2");

      }

      else{

 System.out.println("num1 is not less than num2");

      }

      //check whether num1 is greater than or equal to num2

      if( num1 >= num2 ){

 System.out.println("num1 is greater than or equal to num2");

      }

      else{

 System.out.println("num1 is less than num2");

      }

      //check whether num1 is less than or equal to num2


      if( num1 <= num2){

 System.out.println("num1 is less than or equal to num2");

      }

      else{

 System.out.println("num1 is greater than num2");

      }

   }

}




Screenshot



Results


Let's move

Comments