Skip to content
APPCITORThe Tech Blog
Esc

Part 10 – Java Operators

July 26, 2020 2 min read

Part 10 – Java Operators

1. Java Arithmetic Operators

Here addition, subtraction, division and multiplication are commonly using in mathematics also. So let’s talk about the modular operator (%). This is using to take the value that is remaining when we divide 2 values.

eg: int x = 10%1; ——-> the remaining value is 0.

Next you can see the operator increment. This is using to say that value is increase by 1. And the operator decrement is using to say that value is decrease by 1.

eg: int x = 100;

x++; ——> value is 101

x—; ——-> value is 99

2. Java Relational Operators

Relational operators are using to test or define some kind of relation between 2 entities. That means these operators usually evaluate to true or false.

eg: 10 > 9

10 < 11

10 >= 8

10 <= 15

10 == 10

10 != 11

3. Java Logical Operators

Logical operators can be applied to value of any type and result can be of any type.

The operator short-circuit OR (||) is manipulate to boolean values only. Mostly this is using to test a condition is true or not. This processes its arguments until the first truthy value is reached, and then the value is returned immediately, without even touching the other argument.

eg : if ((a==4) || (b==6))

The operator short-circuit AND (&&) evaluates operands from left to right. Next For each operand, converts it to a boolean. If the result is false, stops and returns the original value of the operand. If all operands have been evaluated, returns that last operand.

eg: if ((a==4) && (b==6))

Simply, AND operator returns the first falsy value or the last value if none were found.

The operator NOT (!) first converts the operand to a boolean value. And then returns the inverse value.

eg: if(x!=5)

4. Ternary Operator

This is the only operator which accepts three operands. The very first operand must be a boolean. The second and third operands can be any expression that returns some value.

eg: int y = x<0?:-x:x;

5. Assignment Operator

This operator is using to assign a value to a variable. Left side of the operand is a variable and right side of the operand is a value. Both value and the variable must be in the same data type.

eg: int a += 10; ——-> this means a = a + 10;

Operator Precedence

Operator Precedence determines the order in which the operators in an expression are evaluated.

Hope you guys got the knowledge about java operators. See you with the next article on the topic Java loops.

Thank you.

Share this article

Keep reading