aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes object r passed by reference ???-check this and u would doubt it Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "object r passed by reference ???-check this and u would doubt it" Watch "object r passed by reference ???-check this and u would doubt it" New topic
Author

object r passed by reference ???-check this and u would doubt it

apurva mehta
Ranch Hand

Joined: Nov 21, 2001
Posts: 35
the objects in java r passed by reference isnt it ???
but try this code & u would possibly doubt it....
i am confused please help me.....
public class Swapping {
public static void main (String[] args)
{
String a,b;
int i,j;
a = new String ("apu");
b = new String("vishu");
i = 1;
j = 5;
System.out.println("value of a before swap : " +a);
System.out.println("value of b before swap : " +b);
swap(a,b);
System.out.println("value of a after swap : " +a);
System.out.println("value of b after swap : " +b);
System.out.println("value of i before swap : " +i);
System.out.println("value of j before swap : " +j);
swap(i,j);
System.out.println("value of i after swap : " +i);
System.out.println("value of j after swap : " +j);
}
public static void swap(String ia ,String ib)
{
String temp = ia;
ia = ib;
ib = temp;
}
public static void swap(int ia ,int ib)
{
int temp = ia;
ia = ib ;
ib = temp;
}
}
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4419
    
    5

Originally posted by apurva mehta:
the objects in java r passed by reference isnt it ???
but try this code & u would possibly doubt it....

What you observe proves that objects are not passed by reference. All parameters in Java are passed by value. That is, the formal parameters get copies of the values of the actual parameters. Whatever happens to the formal parameters in the called method does not affect the actual parameters in the calling code. For objects, what gets passed is the value of the object reference. So initially, your formal object parameters point to the same object as the actual parameters. When you change your formal object parameters to point to different objects, the actual parameters remain unchanged.

Junilu

Junilu - [How to Ask Questions] [How to Answer Questions]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4419
    
    5

I'm having trouble editing my previous message so I'll just add this correction here:
I should not have said "whatever happens to the formal parameter does not affect the actual parameter." Actually, it should be:
Any changes made to the value of the formal parameter will not affect the actual parameter. That is, if you make the formal parameter reference another object, the actual parameter will not follow suit but will remain unchanged. For mutable objects, as long as the formal parameter is not reassigned, any calls to methods that affect the state of the object will also be visible in the calling method.

Junilu
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Well, there is also a whole discussion in the following thread:
http://www.javaranch.com/ubb/Forum24/HTML/013290.html
HIH
Val
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Vikrama Sanjeeva
Ranch Hand

Joined: Sep 02, 2001
Posts: 756
Good question Apurva, at first it confuses me too!!Anyway i am Human thats y get confused.And due to Human i immediately got the crux of code. .Here is the brief explanation.
EXPLANATION
Initially apu & vishu are referenced by a & b respectively.At line,
swap(a,b)
Here you passes the value in a & b.That is references in a & b.And remember refernces are actually memmory addresses.Thus after passing, the memory address(refernce) get copied in ia & ib.Here it is,
public static void swap(String ia ,String ib)
Now apu & vishu are refernced by ia ib respectively as well as by a & b.This is the critical stage.Now the are total 4 references
pointing total of 2 objects.That is,

a & ia are pointing to apu &
b & ib are pointing to vishu
Note that a & b are local variables of main() & ia & ib are local variables of swap().In swap(String ia,String ib)

You have swapped the values of ia & ib.Now ia is pointing to vishu & ib is pointing to apu.
When you print the variables a & b in main method,i.e

You have printed the values pointed by local variables of main method.i.e a & b.And the objects pointed out by this is still apu by a & vishu by b.Hence swapping is done b/w ia & ib,NOT with a & b.
Hope this helps.
Bye.
Viki.
------------------
Count the flowers of ur garden,NOT the leafs which falls away!


Count the flowers of your garden, NOT the leafs which falls away!
Prepare IBM Exam 340 by joining http://groups.yahoo.com/group/IBM340Exam/
Matt Wheeler
Greenhorn

Joined: Nov 20, 2001
Posts: 6
Hold up, I am a little confused. So what you are saying is that a and b after swap(a,b) still contain their initial values of a="abu" and b="vishu" (if I remember correctly)? I understand that their references are not changed by doing bi = ai and bi = temp, but aren't their values changed? If not, how would you change the values so that after swap, a="vishu" and b="abu" which is what I would assume a function named swap should do?
Thanks,
Matt
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Hi Matt

Hold up, I am a little confused. So what you are saying is that a and b after swap(a,b) still contain their initial values of a="abu" and b="vishu" (if I remember correctly)?

Yes exactly. Review the explanation given by Vicky.

I understand that their references are not changed by doing bi = ai and bi = temp, but aren't their values changed? If not, how would you change the values so that after swap, a="vishu" and b="abu" which is what I would assume a function named swap should do?

Well in java this swap is not going to do what its name indicates.


SCJP2. Please Indent your code using UBB Code
Matt Wheeler
Greenhorn

Joined: Nov 20, 2001
Posts: 6
So if in the swap method I just put ai = "vishu"; and bi = "abu"; then a would be equal to "vishu" and b would be equal to abu; after swap returned. Is that correct?
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
you cannot change the String a and b are referencing inside the swap method by any means ! When invoking swap, a copy of the reference a and b are passed to the swap method !
By assigning ai = "vishu" and bi = "abu" you are just changing the reference in the scope method but a and b are left unchanged even after the method swap returns !
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
Matt Wheeler
Greenhorn

Joined: Nov 20, 2001
Posts: 6
Ok, not to beat this horse to death, but what about this?
public class Hold {
public String hold = "a string";
}
class Tryit {
public static void main(String[] args)
{
Tryit tryit = new Tryit();
Hold h = new Hold();
tryit.amethod(h);
}
public void amehtod(Hold h)
{
h.hold = "a different string";
}
}
This does change the value of the string hold in my instance of Hold called h correct? Please say yes.


[This message has been edited by Matt Wheeler (edited November 21, 2001).]
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
yes
but you are not changing the passed reference itself (i.e. h) instead you are using h to change its instance variable hold which is perfectly valid !! Remeber this: you cannot change a reference or a primitive value passed as parameter in the method, but using a reference to make the object (referenced by it) mutate is perfectly correct. In this case, you just changed the String "hold" of Object Hold.
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi guys,
It helps to think of reference variables in terms of libraries and index cards.
When you want to find a book in the library the fastest way is to go to the index file, pull the card for the book and make a note of it's section and row.
A reference variable is like a library index card. It stores the location (memory address) of an object.
When you pass a reference variable to a method, the JVM makes a copy of the address and lets you use that to locate the original object. The library will let you look a the index cards and even remove the book from the library but it won't let you change the index card information.
A method can do the same, it can get a copy of an objects address, it can do whatever it likes to the object, but it cannot change the original reference to the object.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
Co-author Mike Meyers' Java 2 Certification Passport
[This message has been edited by Jane Griscti (edited November 23, 2001).]


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
apurva mehta
Ranch Hand

Joined: Nov 21, 2001
Posts: 35
thanx all of u to help me ..... it has greatly cleared my fundamentals........ java ranch is great place to clear the concepts.....
regards,
Apurva
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: object r passed by reference ???-check this and u would doubt it
 
Similar Threads
object is assigned to null but not reflecting
initialization doubt
Pass By reference Doubt
doubt in arrays
String Doubt