public class Reference {
public void setTreeNode(TreeNode n) {
n = new TreeNode(2);

public void setTreeValue(TreeNode n) {
n.val = 3;

public static void main(String args[]) {
Reference r = new Reference();
TreeNode root = new TreeNode(1);
System.out.println( root.val ); // output => 1

System.out.println( root.val ); // output => 3

in the method of setTreeNode, a copy of root reference is passed to n, that means, n has root’s reference, and this reference is pointed to root, however, after   n = new TreeNode(2);, n a value of reference which pointed to another object. The pointer value is changed, but the value inside of two nodes are not changed.

in the method of setTreeValue, n is pointed to root, so the root’s value is changed.


execute the code without main()

sequence is as follows:

  • jvm loads class
  • executes static blocks
  • looks for main method and invokes it

it is possible with static block, but this works only for Java 6.

Java 7 does not support it because it first look at main() as the entry point; if there is no main(), it will not execute the static blocks; but the order is still the same, it first executes the static blocks, then main()

// this will be executed in java 6, but not Java 7
public final class Test {
    static {

// public class Test {
    static {

    public static void main(String args[]) {

null object

what is the difference?
1) return world.equals(“hello”);
2) return “hello”.equals(world);
equals should obey three rules:
reflexitivity, symmetricity, transitivity
the above two are the same if world is not null.
if world is null, 1) throw an nullPointerException
2) return false
= 1024 terabytes
stack overflow一般是要处理的数据过大

cache pool

public class Test {
public static void main(String args[]) {
String hi1 = “Hello”;
String hi2 = “Hello”;
String hi3 = “Hell” + “o”;
String hi4 = “Hell”;
hi4 += “o”;

Integer one1 = 1;
Integer one2 = 0;
one2 += 1;

Integer many1 = 256;
Integer many2 = 255;
many2 += 1;

A: System.out.println(hi1 == hi2);
B: System.out.println(hi1 == hi3);
C: System.out.println(hi1 == hi4);
D: System.out.println(one1 == one2);
E: System.out.println(many1 == many2);




  • == return true if two objects point to the same reference because the objects are not primitive type
  • have string pool
  • internpublic String intern()

    Returns a canonical representation for the string object.

    A pool of strings, initially empty, is maintained privately by the class String.

    When the intern method is invoked, if the pool already contains a string equal to this String object

 as determined by the equals(Object) method, then the string from the pool is returned.
 Otherwise, this String object is added to the pool and a reference to this String object is returned.
  • compiler checks the string pool
  • complier checks that at the initialization of strings
  • “hell” + “o” creates two strings
  • there will be two strings with value “hello” in the string pool
    If the value p being boxed is true, false, a byte, or a char in the range \u0000 to \u007f, or an int or short number between -128 and 127 (inclusive),
 then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.
means there will be Integer pool like the string pool for Integers between -128 and 127
  • why? for optimization

for example, if not so, Boolean b = true will create many objects without optimization

but with optimization, the fact is there will only two Boolean objects in the pool

Boolean x = true;
Boolean y = true;
System.out.println(x == y);   => true

  • boxed primitive cannot be cached too many because the objects is relatively large