
Java has a series of operators that can make the code less verbose, quicker to write and more readable. Below is a table of the most commonly used operators and an explanation of how to use them. At the bottom of the post you’ll find all the code if you want to play around with it.
| Operator |
Description |
| == |
Test for object reference equality or equality of primitive data types.
int i=1;
int j=1;
System.out.println(i == j);
Output: true
String s1 = "lekkim";
String s2 = "lekkimworld";
System.out.println(s1 == s2);
Output: false
System.out.println(s1 == s1);
Output: true
|
| != |
Test for object reference non-equality or non-equality of primitive data types.
int i=1;
int j=2;
System.out.println(i != j);
Output: true
String s1 = "lekkim";
String s2 = "lekkimworld";
System.out.println(s1 != s2);
Output: true
System.out.println(s1 != s1);
Output: false
|
| && |
Logical AND operator.
boolean b1 = true;
boolean b2 = true;
System.out.println(b1 && b2);
Output: true
|
| || |
Logical OR operator.
boolean b1=true;
boolean b2=false;
System.out.println(b1 || b2);
Output: true
|
| ++ |
This operator can mean two things depending on which side of the variable the operator is on.
If the operator is to the right of the variable it means to use the value of the variable, increment the value and then store result back in the same variable.
int i=0;
System.out.println("i=" + (i++));
Output: i=0
System.out.println("i=" + (i++));
Output: i=1
If the operator is to the left of the variable it means that the JVM should increment the variable, use the variable and store the result back in the variable.
int i=0;
System.out.println("i=" + (++i));
Output: i=1
System.out.println("i=" + (++i));
Output: i=2
Much used in for-loops:
for (int i=0; i<3; i++) {
System.out.print(i + " ");
}
Output: 0 1 2
|
| — |
Same as ++ but subtracting instead of incrementing.
int k=2;
System.out.println("k=" + (k--));
Output: k=2
System.out.println("k=" + (k--));
Output: k=1
int k=2;
System.out.println("k=" + (--k));
Output: k=1
System.out.println("k=" + (--k));
Output: k=0
Can also be used in for-loops:
for (int i=2; i>=0; i--) {
System.out.print(i + " ");
}
Output: 2 1 0
|
| += |
Evaluate the + operator using the value in the variable on the left hand side and the right hand side and store result back in the same variable.
String s1 = "Mikkel";
s1 += " ";
s1 += "Heisterberg";
System.out.println("s1=" + s1);
Output: s1=Mikkel Heisterberg
|
| -= |
Evaluate the – operator using the value in the variable on the left hand side and the right hand side and store result back in the same variable.
int i=10;
int k=5;
i -= k;
System.out.println("i=" + i);
Output: i=5
|
Below is the code for the above examples if you want to play around with it:
public class Main {
public static void main(String[] args) {
equal_equal();
exclamation_equal();
ampersand_ampersand();
pipe_pipe();
plus_plus1();
plus_plus2();
plus_plus3();
minus_minus1();
minus_minus2();
minus_minus3();
plus_equal();
minus_equal();
}
public static void equal_equal() {
int i=1;
int j=1;
System.out.println(i == j);
String s1 = "lekkim";
String s2 = "lekkimworld";
System.out.println(s1 == s2);
System.out.println(s1 == s1);
}
public static void exclamation_equal() {
int i=1;
int j=2;
System.out.println(i != j);
String s1 = "lekkim";
String s2 = "lekkimworld";
System.out.println(s1 != s2);
System.out.println(s1 != s1);
}
public static void ampersand_ampersand() {
boolean b1 = true;
boolean b2 = true;
System.out.println(b1 && b2);
}
public static void pipe_pipe() {
boolean b1=true;
boolean b2=false;
System.out.println(b1 || b2);
}
public static void plus_plus1() {
int i=0;
System.out.println("i=" + (i++));
System.out.println("i=" + (i++));
}
public static void plus_plus2() {
int i=0;
System.out.println("i=" + (++i));
System.out.println("i=" + (++i));
}
public static void plus_plus3() {
for (int i=0; i=0; i--) {
System.out.print(i + " ");
}
System.out.println("");
}
public static void plus_equal() {
String s1 = "Mikkel";
s1 += " ";
s1 += "Heisterberg";
System.out.println("s1=" + s1);
}
public static void minus_equal() {
int i=10;
int k=5;
i -= k;
System.out.println("i=" + i);
}
}