3

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.

3
  • 2
    possible duplicate of Is Java pass by reference? Commented May 12, 2010 at 11:35
  • Java passes by value. Java passes references by value. Commented May 12, 2010 at 11:36
  • 1
    those much parentheses? fromer lisp programmer? :-P Commented May 12, 2010 at 11:38

9 Answers 9

7

As you probably know, Java is call-by-value. Wenn you pass a reference, that reference gets copied. To be sure: The reference itself and not the reference's target gets copied.

Let's have a look at your first sample: When calling build(), the reference h will be copied. Because h(the copy in build()) does not get overwritten somewhere in build(), it always points to the memory location of the original h. So changing h.name affects the original h.

Sample 2 is different: reference h gets copied, too. But h gets overwritten in build(). The effect is that the original h and the h in build() point to different memory locations! The h in build() points to the newly generated Hello object, which will be garbage collected somewhen after the return of method build().

Sign up to request clarification or add additional context in comments.

Comments

3

Java is always pass by value. When you have references, it just passes a copy of the reference pointing to the same object. In your case, you just reroute the copied reference to another object. That's why your original one is not changed.

Comments

1

You have two different variables h. The first one is local to main. The second one is local to build.

If you rename your build variable to x, it should become obvious why h in main is not affected.

Edit:

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 x){ x = new Hello(); } } 

Here you can see clearly, x starts as null. Then x becomes a new object. Then x dies. h doesn't know anything about what's going on in x's life.

Comments

0

h.name = "me" changes the object that is referenced by h. This change will be visible from any other place where the same object is referenced. h = new Hello() makes h reference another (new) object. This change only affects that particular variable h - not any other variable that previously referenced the same object.

Comments

0

Mybe this explains why: http://www.yoda.arachsys.com/java/passing.html

Comments

0

In Test1.java, when you pass Hello object h to build() method, the variable main().h and build().h are pointing to same object in memory.

i.e. main().h = build().h => pointing same object

But in Test2.java, even though you pass main().h to build() method, later on inside build method, you're repointing build().h to a new object. But this does not affect the main().h

i.e. main().h != build().h // they both point different objects.

that is why main().h still contains null and your program prints true.

Comments

0

First of all you need to understand difference between Object and Reference.

In the first case, you are directly accessing object(memory in heap) and changing a value, where as in the second case the reference will have the new Object till the end of method. After that it will be no longer accessible.

 public static void main(String[] args){ String str = null; System.out.println(getString(str)== null); } public static String getString(String s){ s = new String(); return s; } 

The above code will print false

Comments

0

To put it simple:

1) You first create a variable 'h' and point it to null

2) When you come into the method "void build(Hello h)" you create a new variabel (also called 'h') which gets pointed to null (the pointer to what the variable is pointing towards is COPIED).

3) When you do 'h = new Hello()' you then change the new 'h' variable in the method to point towards a new instance of Hello (new Hello()).

The original variable called 'h' is unchanged.

Simple as pie.

Comments

-1

Because h is equal to null.

1 Comment

He's asking why h is equal to null, i.e. why doing h = new Hello() inside build does not affect the h in main.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.