Hi
I have a question regarding pass by value and reference.
Suppose I call a function that takes an object reference as argument. Then
that object reference is still "passed by value" .
(http://java.sun.com/docs/books/tutorial/java/javaOO/arguments.html chk the
"Passing Reference Data Type Arguments" paragraph in this link )
*So is there some way in which we pass reference objects "by reference"
,instead of "by value*".
Or is there no such concept for objects
eg.
public class MyClass()
{
int a;
int b;
MyClass(int x,int y)
{
a=x;
b=y;
}
void swapObjects(MyClass obj)
{
MyClass temp = new MyClass();
temp = this;
this.a = obj.a;
this.b =obj.b;
obj=temp;
}
public static void main(STring[] args)
{
MyClass c1 = new MyClass(10,20);
MyClass c2 = new MyClass(15,30);
c1.swapObjects(c2); // here We pass c2 which is MyClass reference object by
value ,is it correct ?
}
}
Thanks
Sharmishta
On Fri, Mar 13, 2009 at 6:39 AM, Selva <[email protected]> wrote:
>
> Hi,
>
> In both the cases local memory variables are created (in the called-
> method to hold the parameters being passed) .
>
> Pass by value = the parameter value that is being passed will be a
> primitive type; Hence there will be copy of value being passed
> Pass by reference = the parameter value that is being passed will be
> reference(address) of an object. Hence there will be copy of reference
>
> Hope this will help
> :-)
>
> On Mar 13, 9:54 am, CIA <[email protected]> wrote:
> > See the link below:
> http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html
> >
> > That is pass by reference. It create more
> > But now, I want to know how really pass by value does !
> > Does it create one more cell in memory?
> >
> > Suppose, I have a variable:
> > int a = 10;
> >
> > then pass by value:
> > method(a);
> >
> > How does it work?
> > Thanks for replying :)
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---