• Post Reply Bookmark Topic Watch Topic
  • New Topic

What happens if the argument you want to pass is an object instead of a primitive?  RSS feed

 
sonali patil
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please can anyone answer me........
 
Jaikiran Pai
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Irrespective of whether the parameter is primitive or object, it will get passed to the method which accepts it. Did you mean to ask something else?
 
Sachin Dimble
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sonali,
When argument is of Object type, a copy of reference(on stack) get created which points to same object(on heap) and which can modify that object as it points to same mem. allocation.
e.g.
Obj o=new Obj();
o.somevar=23;
S.o.p(o.somevar); //will print the 23
method(o);
S.o.p(o.somevar); //will print the 12
//somewhere in class
void method(Object o)
{
o.somevar=12;
}

I Hope this will suffice.....

-Sachin!!!
 
sonali patil
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you very much jaikiran & sachin sir
 
Sachin Dimble
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey You know me

May i know you are from which batch?

-Sachin!!!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be more precise, you do not actually pass an object into a method. nor do you pass a primitive.

with primitives, you pass a COPY of the VALUE. nothing that changes the value inside the method will effect the value outside the method.

With objects, it actually works the same way. That is becuase your variable does not hold an object, but a REFERENCE TO and object - effectively an address (or a pointer, for you C folk). you pass a COPY OF THE VALUE of the reference. if, inside the method, you change where the method's copy points to, you will not change what the outer reference refers to. it still points to the same old place.

however, if you use the inner reference to alter the object it refers to, then that will be reflected in what the outer reference points to (since it points to the same place).
 
An Sush
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its always a copy/alias of the object thats passed.

---------------------------------------------------
Data Type of the
Formal Parameters Value Passed

---------------------------------------------------
Primitive data types Primitive data value
Class type Reference value
Array type Reference value
---------------------------------------------------


Array Elements as Primitive Data Values:

Output from the program:
Minimum value: 1


Array Elements as Object Reference Values:

Output from the program:
Minimum value in matrix: 2
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!