Skip to main content
deleted 4 characters in body; deleted 2 characters in body
Source Link
Keating
  • 3.6k
  • 10
  • 38
  • 42

This is my first Class Hello.java

public class Hello { String name = ""; } 

This is my second Class Test1.java

public class Test1 { public static void main(String[] args) { Hello h = new Hello(); Test1 t = new Test1(); t.build(h); System.out.println((h.name)); } void build(Hello h){ h.name = "me"; } } 

When I run Test1.java, it prints "me". I think I understand, because of "reference transfer".

This is my third Class Test2.java

public class Test2 { public static void main(String[] args) { Hello h = null; Test2 t = new Test2(); t.build(h); System.out.println(((h == null))); } void build(Hello h){ h = new Hello(); } } 

When I run Test2.java, it prints "true", why ? Is it "reference transfer" no longer? I am confused.

This is my first Class Hello.java

public class Hello { String name = ""; } 

This is my second Class Test1.java

public class Test1 { public static void main(String[] args) { Hello h = new Hello(); Test1 t = new Test1(); t.build(h); System.out.println((h.name)); } void build(Hello h){ h.name = "me"; } } 

When I run Test1.java, it prints "me". I think I understand, because of "reference transfer".

This is my third Class Test2.java

public class Test2 { public static void main(String[] args) { Hello h = null; Test2 t = new Test2(); t.build(h); System.out.println(((h == null))); } void build(Hello h){ h = new Hello(); } } 

When I run Test2.java, it prints "true", why ? Is it "reference transfer" no longer? I am confused.

This is my first Class Hello.java

public class Hello { String name = ""; } 

This is my second Class Test1.java

public class Test1 { public static void main(String[] args) { Hello h = new Hello(); Test1 t = new Test1(); t.build(h); System.out.println(h.name); } void build(Hello h){ h.name = "me"; } } 

When I run Test1.java, it prints "me". I think I understand, because of "reference transfer".

This is my third Class Test2.java

public class Test2 { public static void main(String[] args) { Hello h = null; Test2 t = new Test2(); t.build(h); System.out.println(h == null); } void build(Hello h){ h = new Hello(); } } 

When I run Test2.java, it prints "true", why ? Is it "reference transfer" no longer? I am confused.

Source Link
Keating
  • 3.6k
  • 10
  • 38
  • 42

Why this java application print "true"?

This is my first Class Hello.java

public class Hello { String name = ""; } 

This is my second Class Test1.java

public class Test1 { public static void main(String[] args) { Hello h = new Hello(); Test1 t = new Test1(); t.build(h); System.out.println((h.name)); } void build(Hello h){ h.name = "me"; } } 

When I run Test1.java, it prints "me". I think I understand, because of "reference transfer".

This is my third Class Test2.java

public class Test2 { public static void main(String[] args) { Hello h = null; Test2 t = new Test2(); t.build(h); System.out.println(((h == null))); } void build(Hello h){ h = new Hello(); } } 

When I run Test2.java, it prints "true", why ? Is it "reference transfer" no longer? I am confused.