Java/J2EE and SOA (349 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (5 Blogs)
  • Java/J2EE and SOA (346 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

What are Operators in Java and its Types?

Last updated on Jun 17,2021 36.1K Views

Aayushi Johari
A technophile with a passion for unraveling the intricate tapestry of the... A technophile with a passion for unraveling the intricate tapestry of the tech world. I've spent over a decade exploring the fascinating world of...
8 / 72 Blog from Java Core Concepts

Operators are the constructs which can manipulate the values of the operands. Consider the expression 2 + 3 = 5, here 2 and 3 are operands and + is called operator. In this article on Java operators, the goal is to get you the expertise required to get started and work with operators in Java.

Java supports the following types of operators:

Let’s focus on each of these operators one by one.

Arithmetic Operators in Java

Arithmetic Operators are used to perform mathematical operations like addition, subtraction, etc. Assume that A = 10 and B = 20 for the below table.

Operator

Description

Example

+ Addition

Adds values on either side of the operator

A+B=30

– Subtraction

Subtracts the right-hand operator with left-hand operator

A-B=-10

* Multiplication

Multiplies values on either side of the operator

A*B=200

/ Division

Divides left hand operand with right hand operator

A/B=0

% Modulus

Divides left hand operand by right hand operand and returns remainder

A%B=0

Consider the below example:

package Edureka;

public class ArithmeticOperators {
	  public static void main(String[] args) {
		    int A = 10;
		    int B = 20;
		    System.out.println(A + B);
		    System.out.println(A - B);
		    System.out.println(A * B);
		    System.out.println(A / B);
		    System.out.println(A % B);
		  }

}

Output: 

30
-10
200
0
10

Assignment Operators in Java

An Assignment Operator is an operator used to assign a new value to a variable. Assume A = 10 and B = 20 for the below table.

OperatorDescriptionExample
=Assigns values from right side operands to left side operandc = a + b 
+=It adds right operand to the left operand and assigns the result to left operandc += a
-=It subtracts right operand from the left operand and assigns the result to left operandc -= a
*=It multiplies right operand with the left operand and assigns the result to left operandc *= a
/=It divides left operand with the right operand and assigns the result to left operandc /= a
%=It takes modulus using two operands and assigns the result to left operandc %= a
^=Performs exponential (power) calculation on operators and assign value to the left operandc ^= a

Consider the below example:

package Edureka;

public class JavaOperators {
	  public static void main(String[] args) {
		    int a = 10;
		    int b=20;
		    int c;
		    System.out.println(c = a); // Output =10
		    System.out.println(b += a);// Output=30
		    System.out.println(b -= a);// Output=20
		    System.out.println(b *= a);// Output=200
		    System.out.println(b /= a);// Output=2
		    System.out.println(b %= a);// Output=0
		    System.out.println(b ^= a);// Output=0
		  }

}

Moving ahead in Java operators tutorial, let’s see what are comparison operators.

Relational Operators in Java

These operators compare the values on either side of them and decide the relation among them. Assume A = 10 and B = 20.

Operator

Description

Example

==

If the values of two operands are equal, then the condition becomes true.

(A == B) is not true

!=

If the values of two operands are not equal, then condition becomes true.

(A != B) is true

>

If the value of the left operand is greater than the value of right operand, then condition becomes true.

 (a > b) is not true

<

If the value of the left operand is less than the value of right operand, then condition becomes true.

 (a < b) is true

>=

If the value of the left operand is greater than or equal to the value of the right operand, then condition becomes true.

 (a >= b) is not true

<=

If the value of the left operand is less than or equal to the value of right operand, then condition becomes true.

 (a <= b) is true

Consider the below example:

package Edureka;

public class JavaOperators {
	  public static void main(String[] args) {
		    int a = 10;
		    int b=20;
		    System.out.println(a == b); // returns false because 10 is not equal to 20
		    System.out.println(a != b); // returns true because 10 is not equal to 20
		    System.out.println(a > b); // returns false 
		    System.out.println(a < b); // returns true 
                    System.out.println(a >= b); // returns false
		    System.out.println(a <= b); // returns true 
		  }
		  
}


Next up, let’s focus on logical operators in Java.

Logical Operators in Java

The following are the Logical operators present in Java:

Logical Operators - Java Operators - Edureka

 

OperatorDescriptionExample
&& (and)True if both the operands is truea<10 && a<20
|| (or)True if either of the operands is truea<10 || a<20
! (not)True if an operand is false (complements the operand)!(x<10 && a<20)

Consider the below example:

package Edureka;

public class JavaOperators {
	  public static void main(String[] args) {
		    int a = 10;
		System.out.println(a<10 & a<20);  //returns false
		System.out.println(a<10 || a<20); //returns true
		System.out.println(!(a<10 &  a<20)); //returns true
		  }  
}

Now let’s see unary operators in Java.

Unary Operator in Java

 Unary operators are the one that needs a single operand and are used to increment a value, decrement or negate a value.

OperatorDescriptionExample
++increments the value by 1. There is post-increment and pre-increment operatorsa++ and ++a
decrements the value by 1. There is post decrement and pre decrement operatorsa– or –a
!invert a boolean value!a

Consider the following example:

package Edureka;

public class JavaOperators {
	  public static void main(String[] args) {
		    int a = 10;
		    boolean b=true;
        System.out.println(a++);  //returns 11
        System.out.println(++a);  
        System.out.println(a--);  
        System.out.println(--a);  
        System.out.println(!b); // returns false
		  }  
}

Moving ahead, let’s understand bitwise operator in Java

Bitwise Operator in Java

Bitwise operations directly manipulate bits. In all computers, numbers are represented with bits, a series of zeros and ones. In fact, pretty much everything in a computer is represented by bits. Assume that A = 10 and B = 20 for the below table. 

OperatorDescriptionExample
& (AND)returns bit by bit AND of inputa&b
| (OR)returns OR of input valuesa|b
^ (XOR)returns XOR of input valuesa^b
~ (Complement)returns the one’s complement. (all bits reversed)~a

Consider the example shown below:

package Edureka;

public class JavaOperators {
	  public static void main(String[] args) {
		    int a = 58; //111010
		    int b=13; //1101
        System.out.println(a&b);  //returns 8 = 1000
        System.out.println(a|b);  //63=111111
        System.out.println(a^b);  //55=11011
        System.out.println(~a);  //-59
		  }  
}

Next up, let’s focus on the ternary operator in Java

Ternary Operators in Java

The ternary operator is a conditional operator that decreases the length of code while performing comparisons and conditionals. This method is an alternative for using if-else and nested if-else statements. The order of execution for this operator is from left to right.

Syntax:

(Condition) ? (Statement1) : (Statement2);
  • Condition: It is the expression to be evaluated which returns a boolean value.
  • Statement 1: It is the statement to be executed if the condition results in a true state.
  • Statement 2: It is the statement to be executed if the condition results in a false state.

Consider the below example:

package Edureka;

public class JavaOperators {
	  public static void main(String[] args) {
		  int a = 20, b = 10, c = 30, res; 
		res = ((a > b) ? (a > c)? a: c: (b > c)? b: c); 
		System.out.println("Max of three numbers = "+ res); 
		} 
}

Output– Max of three numbers = 30

Moving ahead to the last java operator, let’s understand Shift operators in Java.

Shift Operators in Java

Shift operators are used to shift the bits of a number left or right, thereby multiplying or dividing the number. There are three different types of shift operators, namely left shift operator()<<, signed right operator(>>) and unsigned right shift operator(>>>).

Syntax:

 number shift_op number_of_places_to_shift;

Consider the following example:

package Edureka;

public class JavaOperators {
	  public static void main(String[] args) {
		    int a=58; 
        System.out.println(a<<2); //232=11101000 
        System.out.println(a>>2);  //returns 14=1110
        System.out.println(a>>>2); //returns 14
		  }  
}

With this, we come to an end of this article on the different Java operators. I hope this article was informative to you.

Check out the Java Course Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. We are here to help you with every step on your journey, for becoming a besides this java interview questions, we come up with a curriculum which is designed for students and professionals who want to be a Java Developer.

Got a question for us? Please mention it in the comments section of this “operators in Java ”article and we will get back to you as soon as possible.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 7th December,2024

7th December

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

What are Operators in Java and its Types?

edureka.co