Operators
2013-11-18 19:35
232 查看
Mathematical operators
If you create a Random object with no
arguments, Java uses the current time as a seed
for the random number generator, and will thus produce different output for each execution of the program.
Relational operators
Equivalence and nonequivalence work with all primitives, but the other
comparisons won’t work with type boolean. Because boolean values can only be true or
false, “greater than” and “less than” doesn’t make sense.
The operators == and != compare object references.
What if you want to compare the actual contents of an object for equivalence? You must use the special method equals( ) that exists for all objects (not primitives, which work fine
with == and !=).
The default behavior of equals( ) is to compare references. So unless you override equals( ) in your new class you won’t get the desired behavior.
Logical operators
Notice that when passing smaller types to Integer.toBinaryString( ), the type is automatically
converted to an int.
Bitwise operators
The left-shift operator (<<) produces the operand to the left of the operator after it has been shifted to the left by the number of bits specified to the right of the operator (inserting zeroes at the lower-order
bits). The signed right-shift operator (>>) produces the operand to the left of the operator after it has been shifted to the right by the number of bits specified to the right of the operator. The signed right shift >> uses sign extension: If the value is
positive, zeroes are inserted at the higher-order bits; if the value is negative, ones are inserted at the higher-order bits. Java has also added the unsigned right shift >>>, which uses zero extension: Regardless of the sign, zeroes are inserted at the higher-order
bits .
Truncation and rounding
Casting from a float or double to an integral value always truncates the number. If instead you want the result to be rounded, use the round( ) methods in java.lang.Math
Java has no “sizeof”
If you create a Random object with no
arguments, Java uses the current time as a seed
for the random number generator, and will thus produce different output for each execution of the program.
Relational operators
Equivalence and nonequivalence work with all primitives, but the other
comparisons won’t work with type boolean. Because boolean values can only be true or
false, “greater than” and “less than” doesn’t make sense.
The operators == and != compare object references.
What if you want to compare the actual contents of an object for equivalence? You must use the special method equals( ) that exists for all objects (not primitives, which work fine
with == and !=).
The default behavior of equals( ) is to compare references. So unless you override equals( ) in your new class you won’t get the desired behavior.
Logical operators
Notice that when passing smaller types to Integer.toBinaryString( ), the type is automatically
converted to an int.
Bitwise operators
The left-shift operator (<<) produces the operand to the left of the operator after it has been shifted to the left by the number of bits specified to the right of the operator (inserting zeroes at the lower-order
bits). The signed right-shift operator (>>) produces the operand to the left of the operator after it has been shifted to the right by the number of bits specified to the right of the operator. The signed right shift >> uses sign extension: If the value is
positive, zeroes are inserted at the higher-order bits; if the value is negative, ones are inserted at the higher-order bits. Java has also added the unsigned right shift >>>, which uses zero extension: Regardless of the sign, zeroes are inserted at the higher-order
bits .
Truncation and rounding
Casting from a float or double to an integral value always truncates the number. If instead you want the result to be rounded, use the round( ) methods in java.lang.Math
Java has no “sizeof”