# [Sticky] Java Syntax - Basic Operators

0

Starting with any new language, you need to be familiar with the operators.
As Java is derived from C, much of the syntax is the same, though there are some unique pieces.

In my examples, X = 8 and Y = 5.

Arithmetic Operators

'+' is called for addition.
e.g. X + Y returns 13

'-' is called for subtraction.
e.g. X - Y returns 3

'*' is called for multiplication.
e.g. X * Y returns 40

'/' is called for division.
e.g. X / Y returns 1.6

'%' is called for modulus (i.e. remainder of a division)
e.g. X % Y returns 3

'++' is called for an increment.
e.g. X++ returns 9

'--' is called for a decrement.
e.g. Y-- returns 4

Relational Operators (return boolean)

'==' checks if equal to.
e.g. (X==Y) returns false

'!=' checks if not equal to.
e.g. (X!=Y) returns true

'>' checks if greater than.
e.g. (X>Y) returns true

'<' checks if less than.
e.g. (X<Y) returns false

'>=' checks if greater than or equal to.
e.g. (X>=Y) returns true

'<=' checks if less than or equal to.
e.g. (X<=Y) returns false

Bitwise Operators

X=8 and Y=5 therefore X=1000 and Y=0101

'&' performs Binary AND operation.
e.g. X&Y returns 0 (0000)

'|' performs Binary OR operation.
e.g. X|Y returns 13 (1101)

'^' performs Binary XOR operation.
e.g. X^Y returns 13 (1101)

'~' performs Binary Ones Complement operation.
e.g. ~Y returns 3 (0101)

'<<' performs left shift operation.
e.g. Y<<2 returns 40 (101000)

'>>' performs right shift operation.
e.g. Y>>2 returns 2.5 (10.10)

Logical Operators

A very useful feature in Java is the logical operators, if the left operand of an OR comparison is True, then it wont process the second because the outcome is determined. The same can be said if the left operand in an AND comparison is False, it wont process the second. This can be useful if say, the second would cause a div by zero error, or just to reduce processing time.

Assume A is true and B is false.

'&&' is called the logical AND operator.
e.g (A&&B) returns false.

'||' is called the logical OR operator.
e.g. (A||B) returns true.

'!' is called the logical NOT operator.
e.g. !(A&&B) returns true.

Assignment Operators

'=' is the assignment operator.
e.g. X=Y+Z, X now holds the value of Y+Z

'+=' is the addition and assignment operator.
e.g. X+=Y, X now holds the value of X+Y

'-=' is the subtraction and assignment operator.
e.g. X-=Y, X now holds the value of X-Y

'*=' is the multiplication and assignment operator.
e.g. X*=Y, X now holds the value of X*Y

'/=' is the division and assignment operator.
e.g. X/=Y, X now holds the value of X/Y

'%=' is the modulus and assignment operator.
e.g. X%=Y, X now holds the value of X%Y

'<<=' is the left shift and assignment operator.
e.g. X<<=Y, X now holds the value of X<<Y

'&=' is the bitwise AND and assignment operator.
e.g. X&=Y, X now holds the value of X&Y

'^=' is the bitwise XOR and assignment operator.
e.g. X^=Y, X now holds the value of X^Y

'|=' is the bitwise OR and assignment operator.
e.g. X|=Y, X now holds the value of X|Y

Conditional or Ternary Operator ('?')

variable C = (expression) ? valueA if true : valueB if false

If expression is true, then the variable C will be assigned valueA. Else, if the expression is false, C will be assigned valueB.

"Computer science is no more about computers than astronomy is about telescopes."
~ Edsger W. Dijkstra

0

Fantastic Post Louis. I always wondered when have you ever needed to use the modulus operator (%), if ever?

PlanckTime - Amin

0

I think I use div and mod more than divide. It's probably most used if you are checking for an even/odd number.

i.e:
if N mod 2 = 0 then
isEven:= true

"Computer science is no more about computers than astronomy is about telescopes."
~ Edsger W. Dijkstra

0

Oh! That makes sense, thanks mate!

PlanckTime - Amin

1

CAN SOMEBODY HELP ME!? MY CONSOLE LOG HAS STOPPED WORKING!!!

Share:

Working

Please Login or Register