Robotics C++ Physics II AP Physics B Electronics Java Astronomy Other Courses Summer Session  

Operators

 

Relational

Assignment

Logic

Math

Short Circuit Evaluation

 

 

Relational

 

Java has six relational operators that compare two numbers and return a boolean value.
The relational operators are <, >, <=, >=, ==, and !=.

 

Operator

Example

Meaning of Operator

Value of the Boolean

<

x < y

Less than

True if x is less than y, false otherwise

>

x > y

Greater than

True if x is greater than y, false otherwise

<=

x <= y

Less than or equal to

True if x is greater than or equal to y, false otherwise

>=

x >= y

Greater than or equal to

True if x is greater than or equal to y, false otherwise

==

x == y

Equal

True if  contents of x equal those of y, false otherwise

!=

x != y

Not equal

True if x is not equal to y, false otherwise

 

 Assignment

 

Operator

Example

Meaning of Operator

=

x = y

Place the contents of y in x and leave the contents of y unchanged

 +=    x += y   x = x + y

-=

x -= y

x = x - y

*=

x *= y

x = x * y

/=

x /= y

x = x/y

%=

x % y

x = x % y

Logic

Operator

Example

Meaning of Operator

Value of the Boolean

&&

a && b

Logical and

True if both a and b are true, false otherwise

||

a || b

Logical or

True if either a or b is true, false if both are false

 

Math

 

Operator

Example

Meaning of Operator

Comments

+

a + b

Addition

Add a and b

-

a - b

Subtraction

Subtract b from a

*

a * b

Multiplication

Multiply a and b

/

a / bsee notes

Division

Divide b into a

%

a % bsee notes

Modulus

Remainder from dividing b into a

 

Notes:

 

If a and b are both integers, the division operator will return the quotient

The modulus operator is only applicable to integer division - it will return the remainder