Operators, Assignments, Comparisons
by Michael Thomas
(Tutorial Home Page)
by Michael Thomas
(Tutorial Home Page)
Objectives
Topic |
Notes |
What's on the Test! |
Most languages have
operators that are similar. Java's operators are similar to C's. We will not
cover the common operators. See the chart "Java Operators" for a list of
operators. The test includes the operators that many of us may skip over.
- Bitwise (>>,<<,>>>)
- instanceof
- Object reference operators (+,+=,==,Object.equals(), and overridden equals() )
|
Standard Operators |
(see the table at the
end: "Java Operators" ) Notes.
- Follow the precedence chart. In the same level, then left to right.
- Integers may throw an ArithmeticException. Floating points (double & float) do
not!
- Divide by 0 w/ Integers throws an ArithmeticException.
- Divide by 0 w/ Floating points = infinity
Examples
- Modulus: 10 % 10 = 0 (remainder)
- Modulus: 5 % 2 = 1 (remainder)
- Integer Division: 1 / 2 = 0 (no decimals)
- Floating point Division: 1 / 2.0 = .5
- Precedence: 2 + 2 * 3 = 8
Precedence: (2 + 2) * 3 = 12
- int y = 0; int x = y++; Results: y=1, x=0
int y = 0; int x = ++y; Results: y=1, x=1
|
Bitwise
Shifting |
Bitwise Shifting:
- << - Bits shift to the left.
- >>> - Bits shift to the right (including the sign bit).
- >> - Bits shift to the right. Remember this saying: "Two right
keep tight!" This means that the high bit (sign indicator) stays
set and it also moves to the right! (see my web site source code examples).
- Bitwise shifting returns an int. If you cast to a lower
value you may loose bits.
Remember:
- Once a bit at the far left or far right and is shifted again, that bit is lost (goes to
the bit bucket).
ex: If int = "0010 0011"
int = int >> 1 , results = 0001
0001
int = int << 1 , results = 0010 0010
int = int >>> 1, results = 0001 0001
- With negatives the >> and >>> work differently.
- intTest = -8
11111111111111111111111111111000 = -8
- intTest = intTest >> 1 (now -4)
11111111111111111111111111111100 = -4
Notice that the sign bit (far left) stays a 1.
- intTest = intTest << 1 (back to -8)
11111111111111111111111111111000 = -8
Notice that the right bit sifts in as "0".
- intTest = intTest >>> 1
01111111111111111111111111111100 = 2147483644
Notice that the sign bit (far left) is "0".
This is the difference in >> & >>>.
>> - sign bit (far left bit) stays 1 if it was 1.
>>> - sign bit (far left bit) is always 0.
Examples:
- int i = 0x00000021; int ans = 0; ans = i >>2;
Results in base 10: i = 33 and ans = 8
Results in base 2: i=0010 0001, ans=0000 1000
|
Negatives
and Binary |
Negatives
and Binary
Rules
- If the first bit (sign bit) of an integer is 1, then the
number is a negative.
- Steps to figure out the value of a 32 byte with the sign bit
set.
- Apply 2's compliment to the byte. To do this you
invert every bit. All 1's become 0's and all 0's
become 1's.
- Add 1 to the byte.
Examples
Integer (32 bits) |
Binary for Integer |
-1 |
11111111111111111111111111111111
00000000000000000000000000000000 (2's compliment)
00000000000000000000000000000001 Plus 1
00000000000000000000000000000001 Equals 1
Answer is -1. |
-2 |
11111111111111111111111111111110
00000000000000000000000000000001 (2's compliment)
00000000000000000000000000000001 Plus 1
00000000000000000000000000000010 Equals 2
Answer is -2. |
-3 |
11111111111111111111111111111101
00000000000000000000000000000010 (2's compliment)
00000000000000000000000000000001 Plus 1
00000000000000000000000000000011 Equals 3
Answer is -3. |
Good luck. This can be complicated. |
Mini lesson in bases! |
Click here for a "Mini Lesson In Bases" (located at the end of this
web page) |
&,
| |
Boolean -
AND/OR
- Data types must be boolean. Results is boolean.
- All conditions are evaluated from left to right, even if the final result can be
predicted.
Example:
- int y = 0;
if (true | (y=5) == 5);
results: y = 5
Bitwise - AND/OR
- Data types must be integers. Results is an integer.
- int Y = ( 1 | 2 ); Results Y = 3
0001
0010
-----
0011 = 3 base 10
Examples:
- (4 & 3) + (2 | 1) = 3 (in base 10)
4 = 1000 2 = 0010
3 = 0011 1 = 0001
AND ---- OR ----
0000 + 0011
0 + 3 = 3
Base 10
|
&&, || |
Logical Boolean
- AND/OR
- Data types must be boolean. Results is a boolean.
- Conditions are evaluated from left to right, until the final result can be predicted.
Examples:
- int y = 0;
if (true || (y=5) == 5);
results: y = 0
|
instanceof |
Tests if an object:
- is an instance of another object (subclassed, etc...)
- or implements an interface.
Notes:
- All objects are an instance of the Object class.
- All numeric wrappers are an instance of the Numeric class.
- All applets are an instance of the Applet class.
- Any class that implements an interface is an instance of that interface.
- Any class that is a subclass of a class is an instance of that class.
Examples:
- System.out.println( (new Integer(5)) instanceof Number) ); shows true - because all
wrappers are extended from the Number class.
|
==,
used with Objects. |
== (operator when
applied to objects):
- True is returned if the two objects reside in the same memory location (the object
references are equal).
- Two different objects may have identical data but will not be == because they reside in
different memory locations.
- Identical object references can be in different variables. The two variables may
point to the same location.
- The method equals() in the Object class does the same thing as the ==. Remember
that all objects are instance of the Object class and inherit this method.
- WARNING - some classes override the equals() method and return true if the data value is
equal. (see equals() section below!)
|
equals() |
equals() - a method in
the class Object.
- Unless this method is overridden by a class, it returns true if the objects reside in
the same memory location.
- In other words, by default equals() and == do the same thing.
Value of an object v.s. Object references - The equals() is
overridden by the following classes to return true if the data value is equal!
- String (on the Test)
- all Wrappers (on the Test)
- Date - if date & time is equal.
- BitSet - same sequence of bits.
- File - if same relative path name. Warning: possibly maybe a different
underlying file.
Example:
- Boolean b1 = new Boolean(true);
Boolean b2 = new Boolean(true);
System.out.println( "Test: "+ b1.equals( b2 ) ); = true
System.out.println( "Test: "+ ( b1 == b2 ) ); = false
|
(? : ) |
( <boolean exp> ?
<true exp> : <false exp> ) This is like an inline if statement.
Examples:
- System.out.println( (true ? "True choosen" : "False ") ) ; shows
"True choosen"
- int X = 5; int Y = 3; int Z = 0;
Z = ( X > Y ? X + Y : 0 );
Results: Z = 8
|
Not
on Test:
~, ^ |
- ~ - reverse bits. This will return the One's complement.
Ex: int i = 0x00000008; i = ~i; Results i = 0xFFFFFF7
Why: Binary 0000 = 0 reverse bits to 1111 = F
Why: Binary 1000 = 8 reverse bits to 0111 = 7.
- ^ - exclusive-or (XOR) - returns true if one is true and one is false.
( true ^ false ); Returns true!
( true ^ true ); Returns false!
( false ^ false ); Returns false!
Note: Math.pow(double a, double b) - Returns the
value of the first argument raised to the power of the second
argument. Some languages use the ^ for
this, but not Java. |
|
|
Following table is from pg. 529 of "Java Master Reference" by Arthur Griffith
ISBN:0-7645-3084-4 (Awesome reference!)
Operator |
Prece-
dence |
Associa-
tivity |
Description |
() [] . |
1 |
LtoR |
The primary expression
operators |
++ -- |
2 |
RtoL |
Unary increment (post
& pre) |
+ - |
2 |
RtoL |
Unary plus and minus |
~ |
2 |
RtoL |
One's complement (or
reverse bits) |
! |
2 |
RtoL |
Boolean complement |
() |
2 |
RtoL |
Casting of Types |
* / % |
3 |
LtoR |
Mult,Division,Modulus |
+ |
4 |
LtoR |
Addition & String
concatenation |
- |
4 |
LtoR |
Subtraction |
<< >>
>>> |
5 |
LtoR |
Bitwise: shift left
& right. >> keeps the sign bit (left most bit) |
< > <= >= |
6 |
LtoR |
Less than, greater
than, Less than or equal to, etc... (returns a boolean) |
instanceof |
6 |
LtoR |
Type comparison
(returns a boolean) |
== != |
7 |
LtoR |
Equal, not equal
(returns a boolean) |
& |
8 |
LtoR |
Boolean AND, bitwise
AND |
^ |
9 |
LtoR |
Boolean XOR, bitwise
XOR |
| |
10 |
LtoR |
Boolean OR, bitwise
OR |
&& |
11 |
LtoR |
Conditional AND
(returns a boolean) |
|| |
12 |
LtoR |
Conditional OR (returns
a boolean) |
( ? : ) |
13 |
RtoL |
Conditional ternary
operator (inline if) |
=, +=, /=
%=, +=, -=
<<=, >>=, >>>=
&=, ^=, ~= |
14 |
RtoL |
Assignment operators |
|
|
|
|
Notes:
- Conditional AND/OR - You evaluate the left most expression, continue to the next
expression only if needed (conditional).
- Boolean AND/OR/XOR - both sides of the operators must be boolean.
- Bitwise AND/OR/XOR - both sides must be integers. Each bit is evaluated.
- Unary operator - uses one operand. (Ex: MyInt++)
- Binary operator - uses two operands. (Ex: 10 + 5)
- Ternary operator - uses three operands (Ex: ( <boolean> ?
<true> : <false> ) )
- Coercion - the automatic conversion from one data type to another by an
operator. If an operator is a String, all others are converted to String.
Next, numbers are converted to the largest data type.
Here is a mini lesson in bases. Remember that math books have full chapters on
this subject.
The name of the base represents how many digits are represented in that numbering
system. Base 10 has 10 digits (0-9), Base 2 has 2 (0-1), Base 8 has 8 (0-7), Base 16
has 16 (0-9, then A-F). Notice that the name of the base is not included as a digit.
Example base 8 does not have the "8" as a digit. (Note:
Technically there is no digit "10" in base 10. Base 10 has the digits 0 -
9 which is ten digits. Therefore, what we call "ten" is realy 1 unit of 10 and 0
units of 0. When dealing with bases, you should pronounce the numbers
10 as "one, zero" not the word "ten" !!!)
Example of the digits (numbers) used in the 4 common bases.
- Base 2 - 0,1
- Base 8 - 0,1,2,3,4,5,6,7
- Base 10 - 0,1,2,3,4,5,6,7,8,9
- Base 16 - 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
Lets count in the different bases.
- Base 2 - 0,1,10,11,100,101,110,111, etc..
- Base 8 - 0,1,2,3,4,5,6,7,10,11,12,13,14,15,16,17,20,21, 22,23,24,25,26,27,30, etc...
- Base 10 - 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18, 19,20, etc...
- Base 16 - 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,
15,16,17,18,19,1A,1B,1C,1D,1E,1F,20, etc...
Converting Base 16 to Base 10: A=10, B=11, C=12, D=13, E=14, F=15.
( Notice that the name of the base does not exist as a digit. There is no 2
in base 2. There is now 8 in base 8. There is no digit "10" in base
10, yet base 10 has "one,zero - known as 10" just like base 2,8, & 16. )
Units
Each base has units. Each unit starts with the base raised to the power of 0,
then increments by 1. (Note: any number raised to 0 is = 1). 10^0=1, 10^1=10,
10^2=100, 8^2=64, etc...
Table of Base 2, 8, 10, & 16
Units Converted to Base 10 Values
|
Base^7 |
Base^6 |
Base^5 |
Base^4 |
Base^3 |
Base^2 |
Base^1 |
Base^0 |
Base 2 |
128 |
64 |
32 |
16 |
8 |
4 |
2 |
1 |
Base 8 |
2,097,152 |
262,144 |
32,768 |
4,096 |
512 |
64 |
8 |
1 |
Base 10 |
10,000,000 |
1,000,000 |
100,000 |
10,000 |
1,000 |
100 |
10 |
1 |
Base 16 |
~268.4 M |
16,777,216 |
1,048,576 |
65,536 |
4,096 |
256 |
16 |
1 |
Use the above table to convert the following numbers to base 10. Do do this find
the base on the left row, then go to the units column to find the base 10 value.
Easy Examples, make sure you understand the principles:
- 10 (Base 10) = 10 (Base 10)
Formula: (1*10) + (0*1) =
10
- 10 (Base 2) = 2 (Base 10) Formula: (1*2) + (0*1) = 2
- 10 (Base 8) = 8 (Base 10) Formula: (1*8) + (0*1) = 8
- 10 (Base 16) = 16 (Base 10) Formula: (1*16) + (0*1) = 16
More Advanced Example:
- 21 (Base 10) = 10 (Base 10) Formula: (2*10) + (1*1) = 21
- 101 (Base 2) = 5 (Base 10) Formula:
(1*4) + (0*2) + (1*1) = 5
- 21 (Base 8) = 8 (Base 10) Formula: (2*8) + (1*1) = 17
- 21 (Base 16) = 16 (Base 10) Formula: (2*16) + (1*1) = 33
More Challenging:
- 1010 (Base 2) = 10 (Base 10) Formula: (1*8)+(0*4)+(1*2) + (0*1) = 10
- 1111 (Base 2) = 15 (Base 10) Formula: (1*8)+(1*4)+(1*2) + (1*1) = 15
- 1 0000 (Base 2) = 16 (Base 10) Formula: (1*16)+(0*8)+(0*4)+(0*2) + (0*1) = 16
- A (Base 16) = 10 (Base 10) Formula: (10*1) = 10
- FF (Base 16) = 255 (Base 10) Formula: (15*16)+(15*1) = 255
- 100 (Base 16) = 256 (Base 10) Formula: (1*256)
(0*16)+(0*1) = 255
TIP: Converting Base (2 to 16) & ( 16 to 2 )
To convert Base 2 (binary) to base 16 (hexidecimal), group the bits into
groups of 4. Then use the table below to convert to base 16. You can use this
same principle to convert Base 16 to Base 2. Look at the Conversion Table and the
examples below.
Conversion Table
Base 10 to 2 to 16
Base 10 |
Base 2 |
Base 16 |
|
Base 10 |
Base 2 |
Base 16 |
0 |
0000 |
0 |
|
8 |
1000 |
8 |
1 |
0001 |
1 |
|
9 |
1001 |
9 |
2 |
0010 |
2 |
|
10 |
1010 |
A |
3 |
0011 |
3 |
|
11 |
1011 |
B |
4 |
0100 |
4 |
|
12 |
1100 |
C |
5 |
0101 |
5 |
|
13 |
1101 |
D |
6 |
0110 |
6 |
|
14 |
1110 |
E |
7 |
0111 |
7 |
|
15 |
1111 |
F |
Good Luck, I hope you have good dentures
(bitwise!)
Exercises w/answers:
- 21 (base 16) = 0010 0001 (base 2) = 33 (base 10)
- 08 (base 16) = 0000 1000 (base 2) = 8 (base 10)
- FF (base 16) = 1111 1111 (base 2) = 255 (base 10)
- 1A3 (base 16) = 0001 1010 0011 (base 2) = (1*256)+(10*16)+(3*1) = 419 (base 10)
- 0xFEDC123 (base 16)= 1111 1110 1101 1100 0001 0010 0011 (Base 2)