Shift Operators
Shift operators are applied only to integer types.
- x << k : shift the bits in x, k places to the left
- x >> k : shift the bits in x, k places to the right filling in
with the highest bit on the left hand side
- x >>> k : shift the bits in x, k places to the right and fill in
with 0
Conditional Operator
The conditional operator ? : uses the boolean value of one
expression to decide which of the two other expressions should be
evaluated.
exp1 ? exp2 : exp3
If exp1 is true, then exp2 is chosen. If exp1 is false then exp3 is
chosen.
Operator Precedence
In Appendix C of your book you have a chart showing the list of
operators in Java and their precedence. When two operators have
the same precedence, their associativity determines the order
of evaluation. All binary operators are left associative. The
assignment operators are right associative.
Operand Evaluation Order
The left hand operand of a binary operator is evaluated before
any part of the right hand operand is evaluated. The order for
evaluating operands takes precedence over the operator precedence
rule.
Strings
A String is a sequence of characters. In Java String is
a class from which you make String objects, like:
String name = "Alan Turing";
String course = "CS 303E";
String ssn = "123456789";
A String has an associated length. To obtain the length of a String
do:
int nameLen = name.length();
You can also concatenate two Strings to get a third String using the
+ operator.
String zip9 = "78712" + "-" + "1059";
You can convert data that is entered as a primitive type into a String
by using the valueOf() method.
int i = 10;
String ten = String.valueOf ( i );
Basic Output
In Java there are no I/O statements in the Java language. The I/O
methods belong to classes in the java.io package. Any
source or destination for I/O is considered a stream of bytes.
There are three objects that can be used for input and output.
- System.out can be used to write output to the console.
- System.err can be used to write error messages to the console.
- System.in can be used to handle input from the console.
The System.out object has two methods - print() and
println(). The println() method will print a carriage
return and line feed after printing so that the next output will be printed
on a new line. The method print() will keep the cursor on the
same line after printing.