• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

object refference

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Paper beats rock. Scissors beats tiny ad.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic