Win a copy of Head First Android this week in the Android 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

value pass by reference comparing with equals method kathy seirra practice exam

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am breaking my head from 2 hours by drawing diagrams for the following code..but still unable to get concept...please can anyone help me...

 
Bartender
Posts: 322
24
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Akshay,

The concepts of pass-by-reference and pass-by-value can be tough (but extremely important) concepts to grasp at first. Luckily, there are two great Campfire Stories on the topic to help you.
I recommend you start with the Cup Size -- a story about variables and then follow-up with Pass-by-Value Please.

After checking out the Campfire Stories, please advise if you have specific questions and we will try to help you through it.
 
Akshay Rawal
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i referred above concept..but i think this example is much complicated i am able to grasp basic example that how object reference are passe in method, and how they modify the vaalue..like in below example i am able to gauge the difference.
..but the example in this post i am unable to decide

Swanky s1 = new Swanky();

s1.s = new Swanky();// wheather s1 and s refer to same or different object..and when method is called gs.s = s;

gs.s is pointing to dummy s which in turn hold reference to s1..than why output is only 1
 
Chris Barrett
Bartender
Posts: 322
24
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Akshay Rawal wrote:gs.s is pointing to dummy s which in turn hold reference to s1..than why output is only 1


What makes the problem confusing is that a Swanky object has an instance variable of Swanky inside of it. When a Swanky object is created, the inner Swanky of s is assigned null by the default constructor. If we had a no-args constructor like this:
We would recursively create Swanky objects inside Swanky objects until a StackOverflowError occurred. Kind of a (s1.s.s.s.s.s.s.s.s.s.s.s.s...) situation. Each s is a Swanky, with it's own instance variable reference of another Swanky, that then has it's own instance variable reference of another Swanky, etc.

So, it's not a dummy s. It's an instance reference variable of type Swanky pointing at null . When go(s1) is called, the reference s1 is passed to the method. In the method, a new variable reference of gs is created and assigned a new Swanky object. The instance Swanky variable of gs.s is then assigned the object referenced also by s1. When the method ends, gs is returned and s2 is assigned to the same Swanky object as gs. What's inside gs? gs.s referencing the same object as s1. Therefore, s2.s also references s1.

As Swanky has no overriding equals() method, the Object class's equals() method is called making a "this == this" check.

Therefore, s2.s == s1 and the output is "1 ". Technically, s2 == gs also, but since gs is out of scope you cannot really see that. Add some System.out.println() in the right spot, and you will be able to see the memory address of the Swanky object gs references before the end of go(s1) is the same that s2 references after the go(s1).

I hope that helps!
 
Akshay Rawal
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i got your explanation i will post each step starting from main() what is happening plesae correct me if i am wrong..

main()
step 1: s1 = new Swanky()
step s2: s1.s = new Swanky()//s1 is assigned object and s1.s is assigned different object

go(s1.s)

step 1: s1.s = new Swanky() =s(d)//method parameter of go() i have added 'd' to avoid confusion
in this step s1.s and s(d) refer to same object

step2: gs = new Swanky();//new static method object..totally new object is assigned

step3: s1.s = new Swanky() = s(d) = s //now s1.s , s(d) and s pointing to same object

step4: returning gs but not stored in any reference

go(s1)

step1 : s1 = new Swanky() = s(d) //method parameter of go() i have added 'd' to avoid confusion
in this step s1 and s(d) refer to same object

step2: gs = new Swanky();//new static method object..totally new object is assigned

step3: s1 = new Swanky() = s(d)= s //now s1 , s(d) and s pointing to same object

step4: in main() s1 = new Swanky() = s =s2 //s2 is assigned in main ..unlike previous method here return value of static method is stored in s2

noww....

if(s2.s.equals(s1)) System.out.print("1 "); // understood

if(s2.equals(s1)) System.out.print("2 ") // here according to my logic s1 and s2 point to same object..step4: in go(s1) method

if(s1.s.equals(s1)) System.out.print("3 ");//no idea

if(s2.s.equals(s2)) System.out.print("4 "); // no idea

 
author
Posts: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Akshay Rawal wrote:
noww....

if(s2.s.equals(s1)) System.out.print("1 "); // understood

if(s2.equals(s1)) System.out.print("2 ") // here according to my logic s1 and s2 point to same object..step4: in go(s1) method




Can you elaborate what you mean by "understood"? .. If you understood the first point, then you should also understand the second point. And unfortunately, you are wrong with the second point -- the s1 and s2 reference variables are not pointing to the same object.


... because ... if you know that the s1 and s1.s references are pointing to different objects (first two lines of main), and that the s2.s and s1 references are pointing to the same object (point one what you understood), then how can the s1 and s2 references point to the same object?

Henry
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic