• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

doubt in pass by refrence

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:
 
vishwa raj
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic