Java Puzzlers

buzdin 450 views 20 slides Oct 12, 2010
Slide 1
Slide 1 of 20
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20

About This Presentation

No description available for this slideshow.


Slide Content

Java Puzzlers
based on GoogleTechTalk

Many thanks.....
•James Gosling and Sun Microsystems for Java

•Google for TechTalks video

•Joshua Bloch for Java Puzzlers

Long Division
@Test
public void LongDivision() {
final long MICROS_PER_DAY =24*60*60*1000*1000;
final long MILLIS_PER_DAY =24*60*60*1000;
long res =( MICROS_PER_DAY / MILLIS_PER_DAY );
assertEquals(1000, res );
}
Result: 1000 - Ok or Error?

Long Division
@Test
public void LongDivision() {
final long MICROS_PER_DAY =24*60*60*1000*1000;
final long MILLIS_PER_DAY =24*60*60*1000;
long res = ( MICROS_PER_DAY / MILLIS_PER_DAY );
assertEquals(1000, res);
}
Result: Ok or Error,
because res is 5. Why?

Long Division
@Test
public void LongDivision() {
final long MICROS_PER_DAY =24L*60*60*1000*1000;
final long MILLIS_PER_DAY =24L*60*60*1000;
long res = ( MICROS_PER_DAY / MILLIS_PER_DAY );
assertEquals(1000, res);
}
Result: Ok,
because res is 1000.

It's Elementary
    @Test
public void itsElementary() {
System.out.println(12345 + 5432l);
}
Result: 66666 ?

It's Elementary
    @Test
public void itsElementary() {
Integer i1 = new Integer(54321);
Integer i2 = new Integer(5432l);
System.out.println(i1-i2);
    }
Result: 0?

It's Elementary
    @Test
public void itsElementary() {
System.out.println(12345 + 5432l);
}
Result: 17777 Why???

What is the size?
@Test
public void whatIsTheSize() {
Set<Short> list = new HashSet<Short>();
for (short i = 0; i<100; i++ ) {
list.add(i);
list.remove( i - 1);
}
assertEquals(1,list.size());
}
Result: 1 ?

What is the size?
@Test
public void whatIsTheSize() {
Set<Short> list = new HashSet<Short>();
for (short i = 0; i<100; i++ ) {
list.add(i);
list.remove( i - 1);
}
assertEquals(1,list.size());
}
Result: 100. Why????

What is size [2] ?
  final String[] URL_NAMES = {
"http://www.google.com",
"http://www.google.lv",
"http://google.com",
};
@Test
public void urlSet() throws MalformedURLException {
Set<URL> favorites = new HashSet<URL>();
for(String name: URL_NAMES) {
favorites.add( new URL(name));
}
assertEquals( 3 ,favorites.size());
}
What is size, 3 ?

What is size [2] ?
  final String[] URL_NAMES = {
"http://www.google.com",
"http://www.google.lv",
"http://google.com",
};
@Test
public void urlSet() throws MalformedURLException {
Set<URL> favorites = new HashSet<URL>();
for(String name: URL_NAMES) {
favorites.add( new URL(name));
}
assertEquals( 3 ,favorites.size());
}
Size is 1. Why???

Evil
class DB {
private static final DB db = new DB();
public static DB getDB(){ return db; }
private static final String URL=System.getProperty("url");
private static final String USR=System.getProperty("usr");
private static final String PSW=System.getProperty("psw");
private DB(){
this.connection = URL+":"+USR+":"+PSW;
}
private String connection;
public String getConnection() {
return connection;
}
}
DB db = DB.getDB();
System.out.println(db.getConnection());
What is result?

Evil
class DB {
private static final DB db = new DB();
public static DB getDB(){ return db; }
private static final String URL=System.getProperty("url");
private static final String USR=System.getProperty("usr");
private static final String PSW=System.getProperty("psw");
private DB(){
this.connection = URL+":"+USR+":"+PSW;
}
private String connection;
public String getConnection() {
return connection;
}
}
DB db = DB.getDB();
System.out.println(db.getConnection());
null:null:null

"Hamlet"
    @Test
public void hamlet(){
Random rnd = new Random();
boolean toBe = rnd.nextBoolean();
Number result = (toBe || !toBe) ?
new Integer(3): new Float(1);
System.out.println(result);
}
(a) 3
(b) 1.0
(c) Throws exception
(d) None of the above

"Hamlet"
    @Test
public void hamlet(){
Random rnd = new Random();
boolean toBe = rnd.nextBoolean();
Number result = (toBe || !toBe) ?
new Integer(3): new Float(1);
System.out.println(result);
}
(a) 3
(b) 1.0
(c) Throws exception
(d) None of the above 3.0, Why???

"Hamlet"
    @Test
public void hamlet(){
Random rnd = new Random();
boolean toBe = rnd.nextBoolean();
Integer i = new Integer(3);
Float f = new Float(1);
Number result = (toBe || !toBe) ? i : f;
System.out.println( result == i);
}
(a) true
(b) false
(c) Throws exception
(d) None of the above

"Hamlet"
    @Test
public void hamlet(){
Random rnd = new Random();
boolean toBe = rnd.nextBoolean();
Integer i = new Integer(3);
Float f = new Float(1);
Number result = (toBe || !toBe) ? i : f;
System.out.println( result == i);
}
(a) true
(b) false
(c) Throws exception
(d) None of the above

The type of a conditional expression is determined as follows:
•If the second and third operands have the same type (which may be the null type), then that is the type of the
conditional expression.
•If one of the second and third operands is of type boolean and the type of the other is of type Boolean, then the
type of the conditional expression is boolean.
•If one of the second and third operands is of the null type and the type of the other is a reference type, then the
type of the conditional expression is that reference type.
•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).
•Otherwise, the second and third operands are of types S1 and S2 respectively. Let T1 be the type that results
from applying boxing conversion to S1, and let T2 be the type that results from applying boxing conversion to S2.
The type of the conditional expression is the result of applying capture conversion (§5.1.10) to lub(T1,
T2) (§15.12.2.7).
Java Language Spesification 15.25

video.google.com
youtube.com
Google
Tags