Title: Java Puzzlers
1Java Puzzlers
- Roger Lindsjö
- Javaforum 2008-11-26
2Bitar på rymmen
long l1 Long.MAX_VALUE long l2
Long.MAX_VALUE - 1 double d Long.MAX_VALUE Sys
tem.out.format("s, sn", l1 d, l2 d)
Vad skrivs ut? a) false, false b) true, false c)
false, true d) true, true
3Bitar på rymmen
long l1 Long.MAX_VALUE long l2
Long.MAX_VALUE - 1 double d Long.MAX_VALUE Sys
tem.out.format("s, sn", l1 d, l2 d)
long ? double kan tappa precision long har 64
bitars precision double beter sig som 53 bitar
4Java med Automatlåda
Object o true ? new Integer(1) new
Double(3.0) System.out.println(o)
Vad skrivs ut? a) 1 b) 3 c) 1.0 d) 3.0
5Java med Automatlåda
Object o true ? new Integer(1) new
Double(3.0) System.out.println(o)
Automatisk unboxing och boxing JLS 15.25 och 5.6.2
6... Otherwise, if the second and third operands
have types that are convertible (5.1.8) to
numeric types, then there are several cases If
one of the operands is of type byte or Byte and
the other is of type short or Short, then the
type of the conditional expression is short. If
one of the operands is of type T where T is byte,
short, or char, and the other operand is a
constant expression of type int whose value is
representable in type T, then the type of the
conditional expression is T. If one of the
operands is of type Byte and the other operand is
a constant expression of type int whose value is
representable in type byte, then the type of the
conditional expression is byte. If one of the
operands is of type Short and the other operand
is a constant expression of type int whose value
is representable in type short, then the type of
the conditional expression is short. If one of
the operands is of type Character and the other
operand is a constant expression of type int
whose value is representable in type char, then
the type of the conditional expression is
char. Otherwise, binary numeric promotion
(5.6.2) is applied to the operand types, and the
type of the conditional expression is the
promoted type of the second and third operands.
Note that binary numeric promotion performs
unboxing conversion (5.1.8) and value set
conversion (5.1.13). ...
7Eviga Loopar
int i Integer.MAX_VALUE while (i gt i
1) int i Integer.MIN_VALUE while (i -i
i ! 0) double i 1L ltlt 53 while (i i
1) double i Double.NaN while (i !
i) String i while (i ! i 0) //Inga
double eller float short i -1 while (i ! 0)
i gtgtgt 1
8Java Puzzlers
- Joshua Bloch (Google)
- Neal Gafter (Microsoft)
- ISBN-13 978- 0-321-33678-1
- ISBN-10 0-321-33678-X