Operators
String
• The String type is used to declare string variables.
• can declare arrays of strings.
• A quoted string constant can be assigned to a String
variable.
• A variable of type String can argument to println( )
String str = "this is a test";
[Link](str);
The Bitwise Operators
• All of the
integer types
(except char)
are signed
integers.
• This means
that they can
represent
negative values
as well as
positive ones.
• the byte value for 42 in binary is
• 00101010
• thus, 42 is the sum of 21 + 23 + 25, which is 2
+ 8 + 32.
• Java uses an encoding known as two’s
complement, which means that negative
numbers are represented by inverting
(changing 1’s to 0’s and vice versa) all of the
bits in a value, then adding 1 to the result
• –42 is represented by inverting all of the bits
in 42,
• or 00101010, which yields 11010101,
• then adding 1, which results in 11010110, or
–42.
• To decode a negative number, first invert all of
the bits, then add 1.
• For example, –42, or 11010110 inverted,
yields 00101001, or 41, so when you add 1
you get 42.
• The reason Java uses two’s complement is easy to
see when you consider the issue of zero crossing.
• Assuming a byte value, zero is represented by
00000000. In one’s complement, simply inverting
all of the bits creates 11111111, which creates
negative zero.
• This problem is solved by using two’s
complement to represent negative values.
• 1 is added to the complement, producing
100000000. This produces a 1 bit too far to the
left to fit back into the byte value, resulting in
the desired behavior, where –0 is the same as 0,
and
The Bitwise Logical Operators
• The bitwise logical operators are &, |, ^, and ~.
class BitLogic {
public static void main(String args[]) {
String binary[] = {
"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
"1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111“ };
int a = 3; // 0 + 2 + 1 or 0011 in binary
int b = 6; // 4 + 2 + 0 or 0110 in binary
int c = a | b;
int d = a & b;
int e = a ^ b;
int f = (~a & b) | (a & ~b);
int g = ~a & 0x0f;
[Link](" a = " + binary[a]);
[Link](" b = " + binary[b]);
[Link](" a|b = " + binary[c]);
[Link](" a&b = " + binary[d]);
[Link](" a^b = " + binary[e]);
[Link]("~a&b|a&~b = " + binary[f]);
[Link](" ~a = " + binary[g]);
} }
The Left Shift
• The left shift operator, <<, shifts all of the bits in
a value to the left a specified number of times.
• It has this general form:
value << num
• Here, num specifies the number of positions to
left-shift the value in value.
• Java’s automatic type promotions produce
unexpected results when you are shifting
byte and short values are promoted to int when an expression
is evaluated.
// Left shifting a byte value.
class ByteShift {
public static void main(String args[]) {
byte a = 64, b;
int i;
i = a << 2;
b = (byte) (a << 2);
[Link]("Original value of a: " + a);
[Link]("i and b: " + i + " " + b);
}
}
// Left shifting as a quick way to multiply by 2.
class MultByTwo {
public static void main(String args[]) {
int i;
int num = 0xFFFFFFE;
for(i=0; i<4; i++) {
num = num << 1;
[Link](num);
}
}
}
The Right Shift
• The right shift operator, >>, shifts all of the
bits in a value to the right a specified number
of times.
• Its general form is shown here:
value >> num
• When you are shifting right, the top (leftmost)
bits exposed by the right shift are filled in with
the previous contents of the top bit. This is
called sign extension and serves to preserve
the sign of negative numbers when you shift
them right
// Masking sign extension.
class HexByte {
static public void main(String args[]) {
char hex[] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};
byte b = (byte) 0xf1;
[Link]("b = 0x" + hex[(b >> 4) & 0x0f] + hex[b & 0x0f]);
}
}
Here is the output of this program:
b = 0xf1
The Unsigned Right Shift
• want to shift a zero into the high-order bit no
matter what its initial value was.
• This is known as an unsigned shift.
• To accomplish this, you will use Java’s
unsigned, shift-right operator, >>>, which
always shifts zeros
• into the high-order bit.
int a = -1;
a = a >>> 24;
11111111 11111111 11111111 11111111
–1 in binary as an int
>>>24
00000000 00000000 00000000 11111111
255 in binary as an int
The >>> operator is only meaningful for 32- and 64-bit values.
Remember, smaller values are automatically promoted to int
in expressions. This means that sign-extension occurs and
that the shift will take place on a 32-bit rather than on an 8-
or 16-bit value.
// Unsigned shifting a byte value.
class ByteUShift {
static public void main(String args[]) {
char hex[] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
}; b = 0xf1
byte b = (byte) 0xf1; b >> 4 = 0xff
byte c = (byte) (b >> 4); b >>> 4 = 0xff
byte d = (byte) (b >>> 4);
byte e = (byte) ((b & 0xff) >> 4);
(b & 0xff) >> 4 = 0x0f
[Link](" b = 0x"
+ hex[(b >> 4) & 0x0f] + hex[b & 0x0f]);
[Link](" b >> 4 = 0x"
+ hex[(c >> 4) & 0x0f] + hex[c & 0x0f]);
[Link](" b >>> 4 = 0x"
+ hex[(d >> 4) & 0x0f] + hex[d & 0x0f]);
[Link]("(b & 0xff) >> 4 = 0x"
+ hex[(e >> 4) & 0x0f] + hex[e & 0x0f]);
}
}
Bitwise Operator Compound Assignments
class OpBitEquals {
public static void main(String args[]) {
int a = 1;
int b = 2;
int c = 3;
a |= 4;
b >>= 1;
c <<= 1;
a ^= c;
[Link]("a = " + a);
[Link]("b = " + b);
[Link]("c = " + c);
}
}
Relational Operators
• The outcome of these operations is a boolean value.
• The relational operators are most frequently used in the
expressions that control the if statement and the various loop
statements.
• Any type in Java, including integers, floating-point numbers,
characters, and Booleans can be compared using the equality test,
==, and the inequality test, !=.
int a = 4;
int b = 1;
boolean c = a < b;
int done;
// ...
if(!done) ... // Valid in C/C++
if(done) ... // but not in Java.
In Java, these statements must be written like this:
if(done == 0) ... // This is Java-style.
if(done != 0) ...
• In Java, true and false are nonnumeric values that do not
relate to zero or nonzero
Boolean Logical Operators
• The logical Boolean operators, &, |, and ^, operate on boolean
values in the same way that they operate on the bits of an integer.
The logical ! operator inverts the Boolean state: !true == false and !
false == true.
// Demonstrate the boolean logical operators.
class BoolLogic {
public static void main(String args[]) {
boolean a = true;
boolean b = false;
boolean c = a | b;
boolean d = a & b;
boolean e = a ^ b;
boolean f = (!a & b) | (a & !b);
boolean g = !a;
[Link](" a = " + a);
[Link](" b = " + b);
[Link](" a|b = " + c);
[Link](" a&b = " + d);
[Link](" a^b = " + e);
[Link]("!a&b|a&!b = " + f);
[Link](" !a = " + g);
}}
Short-Circuit Logical Operators (new)
• The secondary versions of the Boolean AND and
OR operators are known as short-circuit logical
operators.
• use the || and && forms, rather than the | and &
forms of these operators
• Java will not bother to evaluate the right-hand
operand when the outcome of the expression can
be determined by the left operand alone.
• if (denom != 0 && num / denom > 10)
The Assignment Operator
• var = expression;
int x, y, z;
x = y = z = 100; // set x, y, and z to 100
• “chain of assignment”
The ? Operator
• ternary (three-way) operator that can replace
certain types of if-then-else statements.
• expression1 ? expression2 : expression3
• Both expression2 and expression3 are required
to return the same type, which can’t be void.
ratio = denom == 0 ? 0 : num / denom;
// Demonstrate ?.
class Ternary {
public static void main(String args[]) {
int i, k;
i = 10;
k = i < 0 ? -i : i; // get absolute value of i
[Link]("Absolute value of ");
[Link](i + " is " + k);
i = -10;
k = i < 0 ? -i : i; // get absolute value of i
[Link]("Absolute value of ");
[Link](i + " is " + k);
}
Operator Precedence