Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

object refference

 
Soundar Sweet
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

go through the following program


public class ex1
{
public static void main(String args[])
{
String a = "hai";
String b = new String("hai");

hello(a);
hello(b);

System.out.println(a);
System.out.println(b);

}

private static void hello(String ar)
{
ar= "hello";
}

}


i expect the program would print

hai
hello

but it is printing

hai
hai

i heared that b is passed as refference and how it happens
please explain about this.
And please explain what is difference between
String a = "hai";
and
String b = new String("hai");


Thanks
Bubye
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is being passed is a reference to a String object.
The reference is being passed by value.
That means that inside your method hello the variable ar is a copy of the reference in a or in b.

You cannot change what ar references because a String object is immutable.

You cannot change the references a and b because you have only a copy of them.

However, the formal parameter ar can be considered as a local variable within the body of the method. You can change this local variable to refer to another string. But changing the value of this local variable does not change the variables a and b.

After the method returns the variables a and b still refer to the objects "hai" and new String("hai").

"hai" is a String object with the characters {'h', 'a', 'i'}. new String("hai") creates another different String object with the characters {'h', 'a', 'i'}.
[ August 02, 2005: Message edited by: Barry Gaunt ]
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i heared that b is passed as refference

Just to add to what Barry said, as many people seem to confuse passing a reference with pass-by-reference:

Pass-by-value
- When an argument is passed to a function, the invoked function gets a copy of the original value.
- The local variable inside the method declaration is not connected to the caller's argument; any changes made to the values of the local variables inside the body of the method will have no effect on the values of the arguments in the method call.
- If the copied value in the local variable happens to be a reference (or "pointer") to an object, the variable can be used to modify the object to which the reference points.

Pass-by-reference (NOT available in Java)
- When an argument is passed to a function, the invoked function gets a reference (or alias) to the parameter, not a copy of its value.
- Any changes made to the local variable will be reflected by identical changes to the caller's arguments.
 
deshdeep divakar
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Barry & Steve,

you both are great help to understand this concept
but if we look to this method its not returning anything,so this
could also be a reason as well.Please do correct me if I am thinking
wrong.
 
Amit Goel
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Divakar..

As u have pointed out about return type .. the return type of the method has no effect on println .. even if u change the return type it will still print the same..

its all about pass by value..
java does not support pass by reference.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic