Java Operators and Operands

Assignment operators in Java:

int someNumber = 2;

someNumber += 1;

someNumber *= 2;

someNumber -= 2;

someNumber /= 2;

System.out.println(someNumber);

Java’s binary operators (operators with two operands):

Addition or concatenation: +

Subtraction: –

Multiplication: *

Regular division: /

Modulus: %

Exponentiation: Math.pow(9, 2);

Floor division: Math.floor(10.0 / 3.0);

Equality: ==

Not equal to: !=

Assignment: =

Greater than or equal to: >=

Greater than: >

Less than or equal to: <=

Less than: <

And: &&

Or: ||

Java’s unary operators (operators with one operand):

Pre-increment: ++i;

Post-increment: i++;

Pre-decrement: –i;

Post-decrement: i–;

C++ is named after the post-increment operator, saying it’s +1 over C, which is the language it’s based on.

The difference between post- and pre- operators is when they add. If you do something like this:

int x = 3;

System.out.println(x++);

It gets printed out, then 1 is added to it afterwards.

The output will be this:

3

But if you use a pre-increment operator instead of a post-increment one:

int x = 3;

System.out.println(++x);

Then this is the output:

4

Pre-increment adds one and then does everything else afterwards.

← Previous | Next →

Java Topic List

Main Topic List

Leave a Reply

Your email address will not be published. Required fields are marked *