Showing posts with label Operators. Show all posts
Showing posts with label Operators. Show all posts

Jul 5, 2020

Operators in JAVA | Java Language | Coding Winds

OPERATORS

 

Operators are anything which we use to operate our variables, and play with them. So in java we have basically 5 types of category of operators and then we have some special ones. So the five basic types of operators are

i) Arithmetic

ii)Relational

iii)Logical

iv)Assignment

v) Bitwise operators

 

Arithmetic Operators

 

Arithmetic operators are the most basic operators we see in any programming language, these are used to do basic math operations in a code. There are basically five arithmetic operators in java, +, -, *, / , %. 

+      plus operator

-       minus operator

*      multiplication operator

/      division operator

%    modulo operator

 

The modulo operator is used to fetch out the remainder between any two integers.

 

Relational operators

 

Relational operators in java are the ones who relate or compare two or more variables in the code. There are six relational operators in java, they are <, >, <=, >=, ==  and != .

 

<     is smaller than

>     is greater than

==   equals to

>=   greater than equal to

<=   smaller than equal to

!=    not equal to

 

Logical Operators

 

There are three logical operators in java

 

i) LOGICAL AND OPERATOR (&&)

 

In this operator if the two conditions between which the AND operator  is u true , only then our result will be true.

Ex:

package solution;

 

public class Operators {

 

     public static void main(String[] args) {

      int x =8;

     

      if(x%2==0 && x>0) {

      System.out.println("The integer is positive and even");

      }

   

       }

}

//Output: The integer is positive and even

 

ii) LOGICAL OR OPERATOR (||)

 

In this operator the either of the two condition between which the OR operator is used must be true.

Ex:

public class Operators {

 

     public static void main(String[] args) {

      int x =8;

     

      if(x%2==0 || x<0) {

      System.out.println("Not sure the number is positive or not, but it's definitely divisible by 2");

      }

   

       }

}

//Output: Not sure the number is positive or not, but it's definitely divisible by 2

 

iii) LOGICAL NOT OPERATOR (!)

 

Logical NOT operators are used when we want to perform a certain action when a specific condition is not true (or false).

Ex:

public class Coding_Winds {

 

     public static void main(String[] args) {

         // TODO Auto-generated method stub

      int x =7;

     

      if(!(x%2==0)) {

      System.out.println("Your number is odd");

      }

   

      

     }

 

}

//Output: Your number is odd


Assignment Operators (=)

 

These are the operators used to assign the values to the variable. There are of forms of assignment operators

 

Ex:

int a=4;

int b=1;

 

int result1 = a+b;

int result2 = a*b;

int result3 = a/b;

int result4 = a-b;

int result5 = a%b;

        

 

Short- hand method of writing an assignment operator

 

Suppose if someone wants to assign a value to a variable and that value includes the old value of that variable then we can use a short hand method.

 

 

Ex:

a+=1;     // same as a= a+1

a-=3;      // same as a=a-3

a*=2;      // same as a=a*2

a/=1;     // same as a= a/1

a%=3;      // same as a=a%3

 


Bitwise Operators

Bitwise operators are the ones which work on the binary digits of any number and the fetch the results. There are 6 bitwise operators in java , they are

 

&                      AND

|                       OR

<<                     LEFT SHIFT

>>                     RIGHT SHIFT

^                      exclusive OR or  XOR

~                      bitwise compliment

>>>                  Unsigned rightshift

 

Below is the truth table which shows us the different output using &,| and ^ operator.



Note that in binary code, ‘1’ is considered as true an ’0’ as false.

 

AND (&) – In AND operators if both the values are true (or if all the values are true,) then only the output is true.

 

OR (|) - In OR operator if either of the two values (or any one value is true ) then the output is true.

 

XOR (^) - In XOR operator if both the values are same (or all the values are same) then the output is false and if the two values are different (or all the values are different) then the output is true.

 

Bitwise Compliment (~) – In bitwise compliment the digits 0 is replaced by 1 and 1 is replaced by 0.


Bitwise rightshift  (>>) and leftshift (<<)

 

In the bitwise rightshift operator the value of the binary code is shifted towards right by the number specified after the operator, and the empty spaces left in left are filled by zeroes. The same happens in the leftshift operators to the left.

Ex:

public class Coding_Winds {

 

public static void main(String[] args) {

 

int x=13;  // binary code for 6 is 0000 1101

System.out.println(x>>2);  /* Output is 3 which has a binary code 0000 0011 */

System.out.println(x<<2); /* Output is 52 which has a binary code 0011 0100 */

 

}

}

 

Unsigned right shift


This operator is also known as ‘right shift zero fill operator ‘ and this is similar to right shift operator in java , the only difference comes when we have a negative number, since the high order bit (or the most significant bit) of a negative number is set to 1. So when we use an unsigned right shift, it shifts all the upper bits including the high order bit to zero, thus making the resulting number a positive one.

Ex:

public class Coding_Winds {

 

     public static void main(String[] args) {

         int x=-1;

         /* binary code for -1 is 1111 1111 1111 1111 1111 1111 1111 1111 */

         System.out.println(x>>>20);  /* Output is 4095 which has a binary code 0000 0000 0000 0000 0000 1111 1111 1111 */

             

     }

}

 

Some other operators

 

Ternary operators:

 

These operators are used to check a condition, and act accordingly regarding the result. The basic syntax of a ternary operator is

Syntax:

 

(condition)? exp1: exp2;

 

In a ternary operator we first write the write the condition to be checked and then if the condition is true, the compiler returns the first value after ‘?’ and if the condition is false the second value after the colon is returned.

 

Ex:

public class Coding_Winds {

 

public static void main(String[] args) {

int x=10;

int y=11;

 

String str = x>y? "x is a greater number":"y is a greater number";

System.out.println(str);

}

 

}

//Output: y is a greater number

 

 

Increment/ Decrement Operator

 

The increment and decrement operators increase and decrease the value by 1 respectively. There are two types of increment/decrement operators

 

i)Pre increment/decrement (++var / --var)

 

In this operator the compiler first increments or decrements the value of the variable and then reads it.

Ex:

 

public class Coding_Winds {

 

     public static void main(String[] args) {

         int x=10;

         int y=11;

        

         System.out.println(++x);  //Output: 11

         System.out.println(--y);  //Output: 10

     }

}

 

ii) Post increment/decrement (var++/ var--)

 

In this operator the compiler first reads the value of the variable and then increments or decrements its value.

Ex:

 

public class Coding_Winds {

 

     public static void main(String[] args) {

         int x=10;

         int y=11;

        

         System.out.println(x++);  //Output: 10

         System.out.println(y--);  //Output: 11

     }

}

 

Dot Operators (.)

Dot operators are used to access the reference methods and variables of a class, when objects are used. We will learn more about them in the classes

Below is the table describing the order of precedence and associativity of operators




Hope you are clear on this  topic do read our more articles on JAVA LANGUAGE.

If you still have any doubt on this topic then do come to us via email "sophomoretechs@gmail.com" or via Instagram "@coding.winds".


This article is SUBMITTED By : Pranjal Rai


Do subscribe to our daily blog update by clicking here.


Thank You!