Java Operator

Operators in Java are special symbols or keywords used to perform operations on variables and values. Java supports a rich set of operators classified into different categories.

Type

Description

Arithmetic Operators

Perform basic arithmetic operations like addition, subtraction, etc.

Relational Operators

Compare two values and return a boolean result.

Logical Operators

Perform logical operations like AND, OR, etc., often used with boolean values.

Assignment Operators

Assign values to variables.

Ternary Operator

A shorthand for if-else conditions.

Instanceof Operator

Test if an object is an instance of a specific class or subclass.


Arithmetic Operators:

Operator

Description

Example

Output

+

Addition

5 + 3

8

-

Subtraction

5 - 3

2

*

Multiplication

5 * 3

15

/

Division

5 / 2

2(int) or 2.5(float)

%

Modulus (remainder)

5 % 2

1

Example
int a = 5;
int b = 2;
System.out.println("Addition: "+(a + b));
System.out.println("Subtraction: "+(a - b));
System.out.println("Multiplication: "+a * b);
System.out.println("Division: "+a / b);
System.out.println("Remainder: "+a % b);
System.out.println("Increment: "+a++);
System.out.println("Decrement: "+a--);

Try it yourself


Relational (Comparison) Operators:

Operator

Description

Example

Output

==

Equal to

5 == 5

true

!=

Not equal to

5 != 3

true

>

Greater than

5 > 3

true

<

Less than

5 < 3

false

>=

Greater than or equal

5 >= 5

true

<=

Less than or equal

5 <= 3

false

Example
System.out.println("Equal to: "+(5 == 5));
System.out.println("Not Equal: "+(5 != 7));
System.out.println("Greater Than: "+(5 > 2));
System.out.println("Less Than: "+(5 < 2));
System.out.println("Greater or Equal: "+(5 >= 5));
System.out.println("Less or Equal: "+(5 <= 5));

Try it yourself


Logical Operators:

Operator

Description

Example

Output

&&

Logical AND

(5 > 3) && (3 > 1)

true

||

Logical OR

(5 > 3) || (3 < 1)

true

!

Logical NOT

!(5 > 3)

false

Example
// AND operator
System.out.println("AND: "+ (true && false));
System.out.println("AND: "+ (5>3 && 3>2));
System.out.println("\n");

// OR operator
System.out.println("OR: "+(true || false));
System.out.println("OR: "+(5>3 | 3>5));
System.out.println("\n");

// NOT operator
System.out.println("NOT: "+(!(5>3)));

Try it yourself


Assignment Operators:

Operator

Description

Example

Equivalent To

=

Assign

a = 5

------

+=

Add and assign

a += 5

a = a + 5

-=

Subtract and assign

a -= 5

a = a - 5 

*=

Multiply and assign

a *= 5

a = a * 5

/=

Divide and assign

a /= 5

a = a / 5

%=

Modulus and assign

a %= 5

a = a % 5

Example
int a = 5;
System.out.println("Assign: "+a);
a += 2;
System.out.println("Add and Assign: "+a);
a -= 2;
System.out.println("Subtract and Assign: "+a);
a *= 2;
System.out.println("Multiply and Assign: "+a);
a /= 2;
System.out.println("Divide and Assign: "+a);
a %= 2;
System.out.println("Modulus and Assign: "+a);

Try it yourself


Ternary Operator:

Operator

Description

Syntax

Example

Output

? :

Shorthand for if-else

condition ? value1 : value2

(5 > 3) ? 10 : 20

10

Example
int age = 20;
String status = age >= 18 ? "Adult" : "Minor";
System.out.println(status); // Adult

Try it yourself


instanceof Operator:

Example
String str = "Hello";
System.out.println(str instanceof String); // Output: true

Try it yourself


Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.