Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
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 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); } }
30
-10
200
0
10
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.
Operator | Description | Example |
= | Assigns values from right side operands to left side operand | c = a + b |
+= | It adds right operand to the left operand and assigns the result to left operand | c += a |
-= | It subtracts right operand from the left operand and assigns the result to left operand | c -= a |
*= | It multiplies right operand with the left operand and assigns the result to left operand | c *= a |
/= | It divides left operand with the right operand and assigns the result to left operand | c /= a |
%= | It takes modulus using two operands and assigns the result to left operand | c %= a |
^= | Performs exponential (power) calculation on operators and assign value to the left operand | c ^= a |
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.
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 |
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.
The following are the Logical operators present in Java:
Operator | Description | Example |
&& (and) | True if both the operands is true | a<10 && a<20 |
|| (or) | True if either of the operands is true | a<10 || a<20 |
! (not) | True if an operand is false (complements the operand) | !(x<10 && a<20) |
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 operators are the one that needs a single operand and are used to increment a value, decrement or negate a value.
Operator | Description | Example |
++ | increments the value by 1. There is post-increment and pre-increment operators | a++ and ++a |
— | decrements the value by 1. There is post decrement and pre decrement operators | a– 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 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.
Operator | Description | Example |
& (AND) | returns bit by bit AND of input | a&b |
| (OR) | returns OR of input values | a|b |
^ (XOR) | returns XOR of input values | a^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
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);
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 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.
Course Name | Date | Details |
---|---|---|
Java Course Online | Class Starts on 28th September,2024 28th September SAT&SUN (Weekend Batch) | View Details |
edureka.co