Skip to content

Instantly share code, notes, and snippets.

@tonymorris
Last active August 30, 2016 13:03
Show Gist options
  • Save tonymorris/085bf77f5f9c1dd479d36a48989b70c6 to your computer and use it in GitHub Desktop.
Save tonymorris/085bf77f5f9c1dd479d36a48989b70c6 to your computer and use it in GitHub Desktop.
What does this program output?
// What does this program output?
class S {
static final int I = 7;
static {
System.out.println("S");
}
}
class T {
static final Object S = null;
static {
System.out.println("T");
}
}
class U {
static final Object S = "s";
static {
System.out.println("U");
}
}
class V {
static final String S = "s";
static {
System.out.println("V");
}
}
class W {
static final String S = new String("s");
static {
System.out.println("W");
}
}
class X {
static final int I = 7;
static {
System.out.println("X");
}
}
class Y {
static final int I;
static {
I = 8;
System.out.println("Y");
}
}
class Z {
static final Integer I = new Integer(7);
static {
System.out.println("Z");
}
}
class Stnts {
public static void main(String[] args) {
int s = new S().I;
Object t = T.S;
Object u = U.S;
String v = V.S;
String w = W.S;
int x = X.I;
int y = Y.I;
Integer z = Z.I;
}
}
@runnerdave
Copy link

that was confusing, what is the explanataion? thanks

@NeQuissimus
Copy link

NeQuissimus commented Aug 30, 2016

V.S and X.I get inlined?

Another one:

Integer x = 129;
Integer y = 129;
System.out.println(x == y);
x = 12;
y = 12;
System.out.println(x == y);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment