aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt in pass by refrence 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 "doubt in pass by refrence" Watch "doubt in pass by refrence" New topic
Author

doubt in pass by refrence

vishwa raj
Ranch Hand

Joined: Aug 23, 2007
Posts: 40
Hi All,

I am reading K&B book based on this my understanding is that java makes use of pass by value only. and there is no concept of pass by refrence am i correct please correct me if i am wrong my main concern is how it will work when we serilize and deserilize on different JVMs.

Thanks
vishwa
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1535
Originally posted by vishwa raj:
Hi All,

I am reading K&B book based on this my understanding is that java makes use of pass by value only. and there is no concept of pass by refrence am i correct please correct me if i am wrong my main concern is how it will work when we serilize and deserilize on different JVMs.

Thanks
vishwa



What does "pass by value" and "pass by reference" has got to do with serialization and deserialization on different JVMs? :roll:


Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
vishwa raj
Ranch Hand

Joined: Aug 23, 2007
Posts: 40
Hi,

can any body throw some light on this. Java makes use of Pass by value only and there is no concept of Pass by refrence.

Thanks
Lalit Bansal
Ranch Hand

Joined: Jan 29, 2007
Posts: 63
You are correct.

In case of primitives, the values are passed BY-VALUE.

In case of references, a copy of reference is passed. In other words, the reference is passed BY-VALUE. However, since both references (the original and copied) are pointing to same object, we say (in Java) Objects are passed BY-REFERENCE.

Hope this makes sense.
vishwa raj
Ranch Hand

Joined: Aug 23, 2007
Posts: 40
Hi Thanks for the reply.

so my understanding is that java makes use of Pass by value only.Then consider this an object is serilized on one jvm that object contained refrences to other object if this object is deserilized on the other JVM
now this is a different JVM surely it cant find the same values with the serilized object refrences in that case my thinking is

1. JVM will allocate the memory for the object based on the refrence it got from deserilized Object so if this JVM already contains some information in this refrence it will be simply lost.

2. JVM will allocate the memory for the deserilized object and with whatever the new refrence it got it checks the deserilized object updates the refrences with the new refrence and treats the object as thought it was originally created on it.

is that correct.

Thanks
[ August 23, 2007: Message edited by: vishwa raj ]
Neha Bhattad
Ranch Hand

Joined: Aug 20, 2007
Posts: 30
What about Arrays? I think if we pass an array into a method the original reference is not updated ...so in case of arrays the array object is copied into a new object and now there will be 2 objects?
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Neha Bhattad wrote

What about Arrays? I think if we pass an array into a method the original reference is not updated...

No, it is referenced, and the method works on the original array (even if it is final).
See demo:

prints:
{1, 2, 3}
{1, 0, 3}
{1, 0, 3}

You work on the original object.
Only when you dereference (or assign null as in try_to_kill) the reference to the original array object is lost and you work now on a local copy without changing the original array.



Yours,
Bu.


all events occur in real time
rey leon
Greenhorn

Joined: Aug 23, 2007
Posts: 23
Hi vishwa!

To pass by refence you can "wrap" the variable using an array (I can't remember the exact wording, but wrap is the term I remember). Take a look at the program that I built and see if you can understand it. It compiles and runs fine.

-- the code for the program is below --

class Test
{

String str = new String ("this is the original value in the string");

public static void doTest (Test [] arr)
{

arr[0].str = "this is the new value";


}

public static void main(String [] args)
{
Test t = new Test ();

// display t.str's value
System.out.println(t.str);

// assign the address of t to a new variable (an array)
Test [] w = new Test [1];
w[0] = t;

// pass the reference
doTest (w);
// and now display t. It should display the new string
System.out.println(t.str);

}
}

---------- end prg code ---

I hope this helps.
vishwa raj
Ranch Hand

Joined: Aug 23, 2007
Posts: 40
Hi Thanks for all, for spending your valueable time in replying.

but my doubt was specifically with respect to points 1 and 2 of my previous reply can any body please tell me with respect to these points or please give me the links so that i can study and understand for myself.

Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt in pass by refrence