• 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

Question on Pass By Reference

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Folks:
On one of the mock exams i took i came across the following question:

1. d1 is fri december 31 d2 is fri december 31
2. d1 is fri december 31 d2 is sun december 31
3. d1 is sun december 31 d2 is sun december 31

I answered 3 but the correct answer seems to be 2 Can anyone explain??
TIA
-S
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check this out - let me know if you still have questions.
Corey
 
Sudhakar Krishnamurthy
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks corey,
I understood your explanation and so even according to that i think the answer needs to be 3 and not 2. In the method when we do d1=d2, both the date objects are pointing to the Date obj d2 whose year has been changed to 2000, hence when we print d1 and d2 both will contain the same values.
-S
 
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
S -- you are wrong.
When you change d1 to point to d2 within method(), you are altering the automatic (aka local, stack) variables d1 and d2. When method() returns, those two variables are popped off the stack and execution returns to main(), where d1 and d2 still refer to their original objects.
I didn't respond to this post originally because I found the answer choices ambiguous and a bit confusing. d2 (in main) WILL have its year changed to 2000 (1900 + 100), but nothing else will have changed due to method() being invoked.
To summarize what the problem is attempting to test you on:
1) All parameters in Java are passed by value.
2) References passed as parameters will still refer to the same object, and therefore methods invoked on those objects will impact the object, but that's it.
3) REMEMBER that a reference variable is still on the stack (changing it won't change the original value that was passed to the function -- since (1) all parameters in Java are passed by value (aka copied).
That's it -- pretty simple.
[ March 17, 2004: Message edited by: Nathaniel Stoddard ]
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Originally posted by Sudhakar Krishnamurthy:
In the method when we do d1=d2, both the date objects are pointing to the Date obj d2 whose year has been changed to 2000, hence when we print d1 and d2 both will contain the same values.

Be careful there!
Let me modify your code just a little to reduce the amount of confusion here:

Notice that I changed the names of the parameters to the method named method() so that we can distinguish the variables in the main method to those in that one.
Now, let's go through this a piece at a time. First we do this:

This creates 2 objects, I'll call those two object o1 and o2. d1 is a reference variable that points at o1 while d2 is a reference variable that points at o2. At this point, even though o1 and o2 are distinct objects, they contain the same data (they contain the same date).
Next, we execute this line:

This invokes method and passes to it the two reference variables. Note that it doesn't pass the objects - it passes the references to those objects. This is a very key point.
As we start the method, we have this:

Now, this is why I changed the names of our paramaters. What is happening is that d1, which references object o1, is being passed into the method and assigned to the parameter d3. So, at this point, d1 and d3 both reference o1. Likewise for d2 and d4 - they both reference o2. Simple, right?
Next, we execute this line:

In this case, we are invoking a method on the object referenced by d4. This causes the year of that object to change. Now, o1 and o2 contain different dates.
Now, here's the tricky part - this line:

Remember what d3 and d4 are. d3 and d4 are reference variables - they contain references to objects (in this case, o1 and o2). In this line, we're assigning the value that is stored in d4 to d3. At that point, d3 and d4 both reference o2. Ok, that makes sense, right? But, what about d1? What does it point to now?
o1!
d1 is a reference variable from main and its contents were copied into d3. Now, we're changing the contents of d3, but we haven't done a thing to d1. d1 still happily references o1.
So, now that our method is complete, we go back to main and execute this line:

We spit out the date that d1 references, o1, and the date that d2 references, o2.
I hope that helped make sense of this. I would suggest looking back at that Flash demonstration I built again. Obviously, you were missing something last time you looked through it. I built it so that you could graphically see what is happening as I've found many people understand things better if they can see them, rather than having them explained to them, like this.
Let me know if you still have questions.
Corey
 
Sudhakar Krishnamurthy
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Corey and Nathaniel thanks a bunch, i completely missed the point that the variables in the method are local
Now i understand completely...thanks for the elaborate explanation.
-S
 
Hey! Wanna see my flashlight? It looks like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic