1) Integer division truncates towards zero(7/2=3, �C7/2=-3). For integer types, division and remainder obey the rule:
(x/y)*y + (x%y) = x
So 7%2 = 1, and �C7%2=-1.
Integer arithmetic is modular two's-complement arithmeticthat is, if a value exceeds the range of its type (int or long), it is reduced modulo the range. So integer arithmetic never overflows or underflows but only wraps.
2) The strictfp modifier:
If the type of an expression is float or double, then there is a question as to what value set (§4.2.3) the value of the expression is drawn from. This is governed by the rules of value set conversion; these rules in turn depend on whether or not the expression is FP-strict.
Every compile-time constant expression is FP-strict.
If an expression is not a compile-time constant expression, then consider all the class declarations, interface declarations, and method declarations that contain the expression. If any such declaration bears the strictfp modifier, then the expression is FP-strict.
If a class, interface, or method, X, is declared strictfp, then X and any class, interface, method, constructor, instance initializer, static initializer or variable initializer within X is said to be FP-strict.
It follows that an expression is not FP-strict if and only if it is not a compiletime constant expression and it does not appear within any declaration that has the strictfp modifier.
Within an FP-strict expression, all intermediate values must be elements of the float value set or the double value set, implying that the results of all FPstrict expressions must be those predicted by IEEE 754 arithmetic on operands represented using single and double formats.
Within an expression that is not FP-strict, some leeway is granted for an implementation to use an extended exponent range to represent intermediate results; the net effect, roughly speaking, is that a calculation might produce "the correct answer" in situations where exclusive use of the float value set or double value set might result in overflow or underflow.
3) The increment and decrement operators can be either prefix or postfix operatorsthey can appear either before or after what they operate on. If the operator comes before (prefix), the operation is applied before the value of the expression is returned. If the operator comes after (postfix), the operation is applied after the original value is used.
Example:
public static void main(String[] args) { int i = 0; System.out.println(++i + " " + i++ + " " + i ); }
1 1 2
4) The equality operators(==) test for reference identity, not object equivalence. Two references are identical if they refer to the same object; two objects are equivalent if they logically have the same value. Equivalence is tested with the equals method defined by Object, which you should override in classes for which equivalence and identity are different. Object.equals assumes an object is equal only to itself.
5) There are other bit manipulation operators to shift bits within an integer value:
<< Shift bits left, filling with zero bits on the right-hand side
>> Shift bits right, filling with the highest (sign) bit on the left-hand side(Arithmetic Shift)
>>> Shift bits right, filling with zero bits on the left-hand side(Logical Shift)
If the shift count is larger than the number of bits in the word, or if it is negative, the actual count will be different from the provided count. The actual count used in a shift is the count you provide, masked by the size of the type minus one. For a 32-bit int, for example, the mask used is 0x1f (31), so both (n<< 35) and (n<< -29) are equivalent to (n<< 3).
Example:
public static void main(String[] args) { int n = 100; System.out.println(n << 3); System.out.println(n << 35); System.out.println(n << -29); }
Output:
800
800
800
6) An assignment operation is itself an expression and evaluates to the value being assigned. For example, given an intz , the assignment
z = 3;
has the value 3. This value can be assigned to another variable, which also evaluates to 3 and so that can be assigned to another variable and so forth. Hence, assignments can be chained together to give a set of variables the same value:
x = y = z = 3;
Example:
public static void main(String[] args) { int x = 1; System.out.println(5+(x=4)); }
Output:
9
7) Operands to operators will be evaluated left-to-right. Order of evaluation matters if x, y, z has side effects of any kind. Except for the operators &&, ||, and ?:, every operand of an operator will be evaluated before the operation is performed. If evaluation of the left operand of a binary operator causes an exception, no part of the right-hand operand is evaluated. The order of evalution is very specific and evaluation stops as soon as an exception is encountered. If insufficient memory is available for the new object, an OutOfMemoryError exception is thrown before evalution of the constructor argumets occurs.
8) Every expression has a type. The type of an expression is determined by the types of its component parts and the semantics of operators.
If either operand of an arithmetic operator is floating point, the operation is performed in floating-point arithmetic.
A + operator is a String concatenation when either operand to + is of type String or if the left-hand side of a += is a String.
Example 1:
public static void main(String[] args) { int a = 100, b = 200; String c = "JavaBeta"; System.out.println(a+b+c); }
Output 1:
300JavaBeta
Example 2:
public static void main(String[] args) { String a = "JavaBeta"; int b = 100, c = 200; System.out.println(a + b + c); }
Output 2:
JavaBeta100200
9) Preserving magnitude is not the same as preserving the precision of a value. You can lose precision in some implicit conversions. Consider, for example, assigning a long to a float. The float has 32 bits of data and the long has 64 bits of data. A float stores fewer significant digits than a long, even though a float stores numbers of a larger range. You can lose data in an assignment of a long to a float. Consider the following:
Example:
public static void main(String[] args) { long orig = 0x7effffff00000000L; float fval = orig; long lose = (long)fval; System.out.println("orig: " + orig); System.out.println("fval: " + fval); System.out.println("lose: " + lose); }
Output:
orig: 9151314438521880576
fval: 9.1513144E18
lose: 9151314442816847872
When a floating-point value is cast to an integer, the fractional part is lost by rounding toward zero.