Operators in Java

operator in java

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following group.

Types of operator in Java

1) Arithmetic Operator in Java

Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra.

  • * : Multiplication
  • / : Division
  • % : Modulo
  • + : Addition
  • – : Subtraction
  • EXAMPLE OF ARITHMETIC OPERATORS
package operator;
public class operator {
public static void main(String[] args) {
int a = 5;
int b = 10;
int c = 20;
int d = 35;
System.out.println("a + b = " + (a + b) );
System.out.println("a - b = " + (a - b) );
System.out.println("a * b = " + (a * b) );
System.out.println("a / b = " + (a / b) );
System.out.println("a % b = " + (a & b) );
System.out.println("d % a = " + (d & a) );
System.out.println("a++ = " + (a++) );
System.out.println("a-- = " + (a--) );

System.out.println("++d = " + (++d) );
}
}
  • OUTPUT
a + b = 15
a - b = -5
a * b = 50
a / b = 0
a % b = 0
d % a = 1
a++ = 5
a-- = 6
++d = 36

2) Relational operator in Java

Many of your programs will compare two values to determine whether they are or are not the same or to determine the relationship between the two values. This happen each time you enter the password into a program. The program compares the password that you enter with the known password. It also happens when a program determines whether your final grade is equal to or greater than the passing grade for a course.

The operator that tell Java to determine this relationship are called Relational operator because they determine the relationship between two operands.

  • EXAMPLE OF RELATIONAL OPERATORS
package operator;
public class relational {
	public static void main(String[] args) {
	 int a = 10;
	 int b = 20;
	 
	 System.out.println("a==b :" + (a==b));
	 System.out.println("a!=b :" + (a!=b));
	 System.out.println("a>b :" + (a>b));
	 System.out.println("a<b :" + (a<b));
	 System.out.println("a>=b :" + (a>=b));
	 System.out.println("a<=b :" + (a<=b));
	 
	}
}
  • OUTPUT
a==b :false
a!=b :true
a>b :false
a<b :true
a>=b :false
a<=b :true

3) Bitwise Operator in Java

A bitwise operator is used to change the value of a bit. Table 3-5 contains the bitwise operator available in Java.

  •  Bitwise AND (&)
  • Bitwise OR (|)
  • Bitwise left shift (<<)
  • Bitwise right shift (>>)
  • Bitwise AND operator :

Bitwise operator are typically used in program that exchanges data, such as network communications software. Therefore, the result of a bitwise operation is not displayed on the screen. However, we will use the bitwise operator in examples that display the decimal value eqivalent of the binary value returned by the bitwise operator.

  • Bitwise OR oerator :

The bitwise OR operator returns a binary 1 if one of the bits is a binary 1.However, a binary 0 is returned if both bits are a binary 0. Lets set up our expression again to see how the OR operator works. The  following expression uses the same binary numbers used to explain other bitwise operators in this chapter. Whenever both bits are 0, the bitwise OR operator returns a binary 0;otherwise, a binary 1 is returned.

  • Bitwise Shift Operators

Java has three bitwise operators used to shift bits:

  1. Left shift (<<) operator.
  2. Signed right shift (>>) operator.
  3. Unsigned right shift (>>>) operator.
  • Example of Bitwise operator in Java
package operator;
public class Bitwise {
public static void main(String[] args) {
int a = 5;
int b = 6;
int c = a & b;
System.out.println(c);
int d = a | b;
System.out.println(d);
int e = b << 1;
System.out.println(e);
int f = b >> 1;
System.out.println(f);
}
}
  • OUTPUT
4
7
12
3

4) Logical Operator in Java

Logical Operators : These operators are used to perform “logical AND” and “logical OR” operation, i.e. the function similar to AND gate and OR gate in digital electronics. One thing to keep in mind is the second condition is not evaluated if the first one is false, i.e. it has a short-circuiting effect. Used extensively to test for several conditions for making a decision.

The following list are of the logical operator.

  • &&
  • ||
  • !

1. &&

&& operator is called as the AND logical operator. If both the operands are non zero then condition becomes true.

2. ||

|| operator is called as the OR logical operator. If any of the two operands are non zero then condition becomes true.

3.!

! operator is called NOT operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.

Conditional operators are-

  • &&, Logical AND : returns true when both conditions are true.
  • ||, Logical OR : returns true if at least one condition is true.
  • Example of logical operator
// Java program to illustrate 
// logical operators
import java.util.*;
public class operators { 
public static void main(String[] args) 
{ 
String x = "Sher"; 
String y = "Locked";
Scanner s = new Scanner(System.in); 
System.out.print("Enter username:"); 
String uuid = s.next(); 
System.out.print("Enter password:"); 
String upwd = s.next();
// Check if user-name and password match or not. 
if ((uuid.equals(x) && upwd.equals(y)) 
|| (uuid.equals(y) && upwd.equals(x))) { 
System.out.println("Welcome user."); 
} 
else { 
System.out.println("Wrong uid or password"); 
} 
} 
}
  • OUTPUT
Enter username:Sher
Enter password:Locked
Welcome user.

5) Assignment operators in Java

The given are some assignment operators which is used in Java.

  • =
  • +=
  • -=
  • *=
  • /=
  • %=
  • <<=
  • >>=
  • &=
  • ^=
  • |=

1. =

Simple assignment operator, Assigns values from right side operand to left side operand.

2.+=

Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand.

3.-=

Subtract AND assignment operator, It subtracts right operand to the left operand and assign the result to left operand.

4.*=

Multiply AND assignment operator, It multiplies right operand to the left operand and assign the result to left operand.

5./=

Divide AND assignment operator, It divides right operand to the left operand and assign the result to left operand.

6.%=

Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand.

7.<<=

Left shift AND assignment operator.

8.>>=

Right shift AND assignment operator.

9.&=

Bitwise AND assignment operator.

10.^=

Bitwise exclusive OR and assignment operator.

11.|=

Bitwise inclusive OR and assignment operator.

Video lecture to learn operators in java –

Also read – How to install Java on Windows 10

Rate this post
Categories: Java

Saurabh

A digital marketer, programmer, and WordPress developer who is always looking for new technology stuff and news. Saurabh has 1+ years of experience in writing content and in web development.

1 Comment

MOHIT RAMESHWAR PATLE · January 30, 2020 at 11:16 am

You also have to read how to install java on windows 10
http://computertechnologer.com/how-to-install-java-on-windows-10/

Leave a Reply

Avatar placeholder

Your email address will not be published.