The Java programming language has around 30 operators as summarized in the following table:

Category

Operators

Simple assignment

=

Arithmetic

+   -   *     /   %

Unary

+   -   ++     -- !

Relational

==   !=   >     >=   <   <=

Conditional

&&     ||   ? : (ternary)

Type comparison

instanceof

Bitwise and Bit shift

~     <<   >>   >>>   &     ^   |

 

Let’s look at each type of operator in details with examples.

 

1. Simple assignment

Perhaps this is the most commonly used operator. It assigns the value on its right to the operand on its left. Here are some examples:

    • Assigning numbers:
      int x = 10;
      float y = 3.5F; 
    • Assigning object references:
String message = “Hello world”;
File csv = new File(“test.csv”); 

 

2. Arithmetic operators

The arithmetic operators are used to perform mathematic calculations just like basic mathematics in school. The following table lists all arithmetic operators in Java:

Operator

Meaning

+

Addition (and strings concatenation) operator

-

Subtraction operator

*

Multiplication operator

/

Division operator

%

Remainder operator

Here’s an example program:

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

 

The addition operator (+) can also be used for joining two or more strings together (strings concatenation). Here’s an example program:

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! 

 

3. Unary operators

The unary operators involve in only a single operand. The following table lists all unary operators in Java:

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.

Here’s an example program:

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
true

Note 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:

    • Prefix form: the operand is incremented or decremented before used in the expression.
    • Postfix form: the operand is incremented or decremented after used in the expression.

The following example illustrates the prefix/postfix:

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 

 

4. Relational operators

The relational operators are used to compare two operands or two expressions and result is a boolean. The following table lists all relational operators in Java.

Operator

Meaning

==

equal to

!=

not equal to

greater than

>=

greater than or equal to

less than

<=

less than or equal to

Here’s an example program:

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 

 

5. Conditional operators

Operator

Meaning

&&

conditional -AND operator

||

conditional-OR operator

? :

ternary operator in form of: A ? B : C

The conditional operators (&& and ||) are used to perform conditional-AND and conditional-OR operations on two boolean expressions and result in a boolean value. They have “short-circuiting” behavior:

    • For the && operator: if the left expression is evaluated to false, then the right expression is not evaluated. Final result is false.
    • For the || operator: if the left expression is evaluated to true, then the right expression is not evaluated. Final result is true.

Here’s an example program:

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 10

Other 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 

 

6. Type comparison operator (instanceof)

The instanceofoperator tests if an object is an instance of a class, a subclass or a class that implements an interface; and result in a boolean value.

Here’s an example program:

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 

 

7. Bitwise and Bit shift operators

These operators perform bitwise and bit shift operations on only integral types, not float types. They are rarely used so the listing here is just for reference:

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

Here’s an example program:

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: 40 

 

Related Topics:

 

Other Recommended Tutorials:


About the Author:

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.



Attachments:
Download this file (ArithmeticDemo.java)ArithmeticDemo.java[Example program for arithmetic operators]0.4 kB
Download this file (BitDemo.java)BitDemo.java[Example program for bitwise and bit shift operators]0.2 kB
Download this file (ConditionalDemo.java)ConditionalDemo.java[Example program for conditional operators]0.2 kB
Download this file (InstanceofDemo.java)InstanceofDemo.java[Example program for instanceof operators]0.4 kB
Download this file (PrefixPostfixDemo.java)PrefixPostfixDemo.java[Example program for increment and decrement operators]0.2 kB
Download this file (RelationalDemo.java)RelationalDemo.java[Example program for relational operators]0.5 kB
Download this file (StringConcatDemo.java)StringConcatDemo.java[Example program for String concatenation]0.2 kB
Download this file (TernaryDemo.java)TernaryDemo.java[Example program for ternary operators]0.2 kB
Download this file (UnaryDemo.java)UnaryDemo.java[Example program for unary operators]0.4 kB

Add comment

   


Comments 

#13Nam2020-03-02 21:24
Hi Filippo,
That's easy to understand. Let me explain:
x == y is a comparison: whether x equals y or not true or false).
Then result = x == y assigns the outcome of the comparison to the result variable.
Quote
#12Filippo2020-03-02 07:38
Hi Ha Minh,
I have a question about relational operators. In the above example you write:

public class RelationalDemo {
public static void main(String[] args) {
int x = 10;
int y = 20;

boolean result = x == y;

I don't understand the meaning of the first = between result and x.
Could you please shed a light on this ?
Thank u very much, greetings, Filippo
Quote
#11Binuraj2020-01-11 10:53
Everything added here to become a great developer. Really super Nam. Very helpful
Quote
#10sdcfsd2019-09-11 00:51
u have an error at : Hello James Gosling!
basically u have one too many '!'
Quote
#9ggfg2019-04-25 09:32
hello sir,
i want to ask that how are you.
Quote