Category | Operators |
Simple assignment | = |
Arithmetic | + - * / % |
Unary | + - ++ -- ! |
Relational | == != > >= < <= |
Conditional | && || ? : (ternary) |
Type comparison | instanceof |
Bitwise and Bit shift | ~ << >> >>> & ^ | |
int x = 10; float y = 3.5F;
String message = “Hello world”; File csv = new File(“test.csv”);
Operator | Meaning |
+ | Addition (and strings concatenation) operator |
- | Subtraction operator |
* | Multiplication operator |
/ | Division operator |
% | Remainder operator |
public class ArithmeticDemo {
public static void main(String[] args) {
int x = 10;
int y = 20;
int result = x + y;
System.out.println("x + y = " + result);
result = x - y;
System.out.println("x - y = " + result);
result = x * y;
System.out.println("x * y = " + result);
result = y / x;
System.out.println("y / x = " + result);
result = x % 3;
System.out.println("x % 3 = " + result);
}
} Output:x + y = 30 x - y = -10 x * y = 200 y / x = 2 x % 3 = 1
public class StringConcatDemo {
public static void main(String[] args) {
String firstName = "James";
String lastName = "Gosling";
String greeting = "Hello " + firstName + " " + lastName;
System.out.println(greeting);
}
}Output:Hello James Gosling!
Operator | Meaning |
+ | Unary plus operator; indicates positive value (numbers are positive by default, without this operator). |
- | Unary minus operator; negate an expression. |
++ | Increment operator; increments a value by 1. |
-- | Decrement operator; decrements a value by 1; |
! | Logical complement operator; inverts value of a boolean. |
public class UnaryDemo {
public static void main(String[] args) {
int x = 10;
int y = 20;
int result = +x;
System.out.println("+x = " + result);
result = -y;
System.out.println("-y = " + result);
result = ++x;
System.out.println("++x = " + result);
result = --y;
System.out.println("--y = " + result);
boolean ok = false;
System.out.println(ok);
System.out.println(!ok);
}
}Output:+x = 10 -y = -20 ++x = 11 ++y = 19 false trueNote that the increment and decrement operators can be placed before (prefix) or after (postfix) the operand, e.g. ++x or x++, --y or y--. When using these two forms in an expression, the difference is:
public class PrefixPostfixDemo {
public static void main(String[] args) {
int x = 10;
int y = 20;
System.out.println(++x);
System.out.println(x++);
System.out.println(x);
System.out.println(--y);
System.out.println(y--);
System.out.println(y);
}
}Output:11 11 12 19 19 18
Operator | Meaning |
== | equal to |
!= | not equal to |
> | greater than |
>= | greater than or equal to |
< | less than |
<= | less than or equal to |
public class RelationalDemo {
public static void main(String[] args) {
int x = 10;
int y = 20;
boolean result = x == y;
System.out.println("x == y? " + result);
result = x != y;
System.out.println("x != y? " + result);
result = x > y;
System.out.println("x > y? " + result);
result = x >= y;
System.out.println("x >= y? " + result);
result = x < y;
System.out.println("x < y? " + result);
result = x <= y;
System.out.println("x <= y? " + result);
}
}Output:x == y? false x != y? true x > y? false x >= y? false x < y? true x <= y? true
Operator | Meaning |
&& | conditional -AND operator |
|| | conditional-OR operator |
? : | ternary operator in form of: A ? B : C |
public class ConditionalDemo {
public static void main(String[] args) {
int x = 10;
int y = 20;
if ((x > 8) && (y > 8)) {
System.out.println("Both x and y are greater than 8");
}
if ((x > 10) || (y > 10)) {
System.out.println("Either x or y is greater than 10");
}
}
}Output:Both x and y are less than 8 Either x or y is greater than 10Other conditional operators are ? and : which form a ternary (three operands) in the following form:
result = A ? B : C
This is interpreted like this: if A evaluates to true, then evaluates B and assign its value to the result. Otherwise, if A evaluates to false, then evaluates C and assign its value to the result. For short, we can say: If A then B else C. So this is also referred as shorthand for an if-then-else statement.Here’s an example of the ternary operator:public class TernaryDemo {
public static void main(String[] args) {
int x = 10;
int y = 20;
int result = (x > 10) ? x : y;
System.out.println("result 1 is: " + result);
result = (y > 10) ? x : y;
System.out.println("result 2 is: " + result);
}
}Output:result 1 is: 20 result 2 is: 10
public class InstanceofDemo {
public static void main(String[] args) {
String name = "Java";
if (name instanceof String) {
System.out.println("an instance of String class");
}
// test for subclass of Object
if (name instanceof Object) {
System.out.println("an instance of Object class");
}
// test for subclass of an interface
if (name instanceof CharSequence) {
System.out.println("an instance of CharSequence interface");
}
}
}Output:an instance of String class an instance of Object class an instance of CharSequence interface
Operator | Meaning |
~ | unary bitwise complement; inverts a bit pattern |
<< | signed left shift |
>> | signed right shift |
>>> | unsigned right shift |
& | bitwise AND |
^ | bitwise exclusive OR |
| | bitwise inclusive OR |
public class BitDemo {
public static void main(String[] args) {
int x = 10;
int result = x << 2;
System.out.println("Before left shift: " + x);
System.out.println("After left shift: " + result);
}
}Output:Before left shift: 10 After left shift: 40Related Topics:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.