• 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

Passing values from one method to another

 
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am really confuse when passing the objects from sender to receiver

Q1) In which scenario receiver and sender will refer to same object?When receiver makes an changes when it reflects orginal copy in sender.
Q2) In my below receiver is making different object at while concating and it's not reflecting the sender copy?





 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1)ref="sathyajeet"
2)b=sathyajeet
//after concat
3)b=sathyajeetkadam //but refering to diffrent object so wont be affected in main code
4)ref="sathyajeet"//not changed

to make the change refelected the in mainreturn the string changed
ref=modify()//don't forget to change the returntype
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

amolpalekar kadolkar wrote:I am really confuse when passing the objects from sender to receiver

Q1) In which scenario receiver and sender will refer to same object?When receiver makes an changes when it reflects orginal copy in sender.


When pass object reference to a method, it means you pass the bit pattern of the variable. So that particular object is referenced(at least) by two referenced variables. One from your previous method, and the second from from the method, to which you pass the reference of that object. So, you can change the state of that object using either of the references. But if you pass primitive variables, because primitive variable's bit pattern holds the values you assigned to that primitive variable(For example, int b = 9;) here, in the bit pattern of b holds 9. So when you pass this bit pattern to methods, it means you pass that value(in this case, you pass 9), if you change them in the calling method, it won't reflected to called method.
Try this,


For Strings, because of the String Literal Pool, the story is different.
 
I was born with webbed fish toes. This tiny ad is my only friend:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic