Win a copy of Secure Financial Transactions with Ansible, Terraform, and OpenSCAP this week in the Cloud/Virtualization forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

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

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please can anyone answer me........
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you very much jaikiran & sachin sir
 
Sachin Dimble
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey You know me

May i know you are from which batch?

-Sachin!!!
 
lowercase baba
Posts: 12975
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Every plan is a little cooler if you have a blimp. And a tiny ad.
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic