File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes object refference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "object refference" Watch "object refference" New topic

object refference

Soundar Sweet
Ranch Hand

Joined: Dec 17, 2004
Posts: 31

go through the following program

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




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


i expect the program would print


but it is printing


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";
String b = new String("hai");


Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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 ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

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:

- 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

Joined: Apr 19, 2004
Posts: 91
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

"Do not be afraid of going slow, be afraid of standing still"
Amit Goel
Ranch Hand

Joined: Dec 07, 2000
Posts: 50
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.

Amit<br /> <br />The Less I have, The more I gain..Off the Beaten Path, I Reign.
I agree. Here's the link:
subject: object refference
It's not a secret anymore!