Java Operators and Assignment

Now that we know about variables we will want to increment them, add them, shift there bits and compare one to another. There are a number of operators in Java that are very similar to other programming languages

Java Operators and Assignment
Assignment Associates or assigns a value to a variable, if its a primitive data type the variable will be a storage holder, if the variable is a string it will be a reference to the String object.
Comparison You have four comparison operators that can be used to compare any combination of integers, floating-point numbers, characters, they will always result in true or false
instanceof is used for object reference variables only and you can use it to check whether an object is of a particular type (class or interface)
Equality compare two things and return a boolean value (true or false)
Arithmetic standard Arithmetic operators, addition, subtraction, multiplication, division and remainder
String Concatenation The plus sign can also be used to concatenate two Strings together
Increment and Decrement

These two operators (++ and --) will increment or decrement a variable by exactly one. The operator can be placed either before (prefix) or after (postfix) a variable.

Shift The shift operator shifts bits to either the right or left
Bitwise The bitwise operators take two individual bit numbers then use AND/OR to determine the result on a bit-by-bit basis. There are three bitwise operators AND (&), inclusive OR (|) and exclusive OR (^).
Bitwise Complement bitwise complement operator (~) flips the bits of the variable 1 becomes 0 and 0 becomes 1
Conditional (Ternary) is a ternary operator (?:) is a bit like a if-else statement
Primitive Casting Allows you to convert primitive values from one type to another, casting can be either explicit or implicit.
Logical The two other logical operators are the OR (||) and AND (&&) otherwise known as the "short-circuit" operators, the others being the bitwise AND and OR other wise know as "not short-circuit". When using the not short circuit (& or |) beware that it will evaluate both expressions even if the second expression would not change the outcome they are inefficient compared with the short-circuit versions.
Java Operators and Assignment Examples
Assignment

int x = 7;                         # pretty standard assignment

byte b = 127;
byte c = b;                        # copy variable b into c, remember there is no relationship between b and c

String text = "Hello World!\n";    # create variable with an object type of String
String text2 = text;               # assign an object to a object variable, point to same object

float f = (float) 32.3;            # Explicitly cast the double literal to a float, otherwise compiler error

Note: if the value is too large for the data type the compiler will let you know

Comparison

if ( age > 65 ) { ... }           # greater than operator
if ( age < 65 ) { ... }           # less than operator
if ( age >= 65 ) { ... }          # greater than or equal to operator
if ( age <= 65 ) { ... }          # less than or equal to operator

Note: comparison operators always result in a boolean (true or false)

instanceof

String s = "Hello World!"
if ( s instanceof String ) { ... }

Note: it will check the superclasses as well to confirm it has a IS-A relationship

Equality

int age = 60;
if ( age == 60 ) { ... }          # compare variable age with integer 60
if ( age != 60 ) { ... }

String s1 = "Test";
String s2 = "Test";
if ( s1 == s2 ) { ... }           # compare two String objects

Arithmetic int x = 5 * 5;
int y = 20 - 10;
int z = 20 % 3;
String Concatenation

String s1 = "Hello ";
String s2 = "World!\n";
String result = s1 + s2;          # result would contain "Hello World!\n"

int a = 2;
int b = 3;
System.out.println( a + b );               # results in 5 being printed
System.out.println( "Answer: " + a + b);   # results in "Answer = 23" being printed

Note: when using integers and Strings, as long as one operand is a String the result is a String, if both operands are integers then the arithmetic will take place

Increment and Decrement int a = 4;
int b = 4;
int a_prefix = a++;        # results in variable a_prefix having the value of 4; (increment after assignment)
int b_postfix = ++b;       # results in variable b_postfix having the value of 5; (increment before assignment)
Shift int result = 95 >> 1;      # shift the bits to the right by one
int result = 95 << 2;      # shift the bits to the left by two
int result = 95 >>> 1;     # shift the bits to the right by 1 (fill the left most bits with zero's)
Bitwise

int a = 10, b = 9;
int c = a & b;             # results in 8 being assigned to c (1010 & 1001 = 1000)
int c = a | b;             # results in 11 being assigned to c (1010 | 1001 = 1011)

X Y &  (AND) |  (OR) ^  (XOR)
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0
Bitwise Complement

int x = 5;
int y = ~x;
System.out.println("Y: " + y);     # results in "Y: -6"

~0000 0000 0000 0000 0000 0000 0000 0101 becomes 1111 1111 1111 1111 1111 1111 1111 1010

Conditional (tenary) String status = (numOfPets < 3) ? "It's OK to have a couple of pets" : "Way too many";
Primitive Casting

int a = 100;
long b = a;            # Implicit cast, an int value always fits in a long

float a = 100.001f;
int b = (int) b;       # Explicit cast, a float can loose info as an int

Note: with explicit casting you let the compiler know that you know that you may loose info

Logical

# Short-Circuit
if (( age >= 5 ) && ( age <= 17 )) { ... }       # both expressions have to be true to execute the if body
if (( age == 18 ) || ( age == 19 )) { ... }      # only one expression has to be true to execute the if body

# Not Short Circuit
if (( age >= 5 ) & ( age <= 17 )) { ... }        # both expressions have to be true to execute the if body
if (( age == 18 ) | ( age == 19 )) { ... }       # only one expression has to be true to execute the if body

Note: using the not short circuit (& or |) is inefficient because it will evaluate both expressions even if the second expression would not change the outcome.

Pass By Value Semantics

When passing a variable or a object to a method two outcomes happen

Passing Variable When you pass a variable to a method, you are passing a copy of the variable which means you can do what ever you want but you will not affect the source of the variable.
Passing Object

When you pass a object to a method you are passing the reference to that object not the object itself, it represents a way to get to a specific object in memory, thus any changes to the object that occur inside the method are being made to the object whose reference was passed.

Operator Precedence Chart

All operators have a order of precedence, they can also operate from left-to-right or from right-to-left.

Operator Type
Associativity
() parentheses
left to right
[ ] array subscript
"
. member selection
"
++ unary postincrement
right to left
-- unary postdecrement
"
++ unary preincrement
"
-- unary predecrement
"
+ unary plus
"
- unary minus
"
! unary logical negation
"
~ unary bitwise complement
"
( type ) unary cast
"
* multiplication
left to right
/ division
"
% modulus (reminder)
"
+ addition
"
- substraction
"
<< bitwise left shift
"
>> bitwise right shift with sign extension
"
>>> bitwise right shift with zero extension
"
< relational less than
"
<= relational less than or equal to
"
> relational greater than
"
>= relational greater than or equal to
"
instanceof type comparison
"
== relational is equal to
"
!= relational is not equal to
"
& bitwise AND
"
^ bitwise exclusive OR, boolean logical exclusive OR
"
| bitwise inclusive OR, boolean logical inclusive OR
"
&& logical AND
"
|| logical OR
"
?: ternary conditional
right to left
= assignment
"
+= additional assignment
"
-= substraction assignment
"
*= multiplication assignment
"
/= division assignment
"
%= modulus assignment
"
&= bitwise AND assignment
"
^= bitwise exclusive OR assignment
"
|= bitwise inclusive OR assignment
"
<<= bitwise left shift assignment
"
>>= bitwise right shift with sign extension assignment
"
>>>= bitwise right shift with zero extension assignment
"