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 Note: if the value is too large for the data type the compiler will let you know |
|||||||||||||||||||||||||
Comparison | if ( age > 65 ) { ... } # greater than operator Note: comparison operators always result in a boolean (true or false) |
|||||||||||||||||||||||||
instanceof | String s = "Hello World!" Note: it will check the superclasses as well to confirm it has a IS-A relationship |
|||||||||||||||||||||||||
Equality | int age = 60; String s1 = "Test"; |
|||||||||||||||||||||||||
Arithmetic | int x = 5 * 5; int y = 20 - 10; int z = 20 % 3; |
|||||||||||||||||||||||||
String Concatenation | String s1 = "Hello "; int a = 2; 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;
|
|||||||||||||||||||||||||
Bitwise Complement | int x = 5; ~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; float a = 100.001f; Note: with explicit casting you let the compiler know that you know that you may loose info |
|||||||||||||||||||||||||
Logical | # Short-Circuit # Not Short Circuit 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. |
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. |
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 | " |