This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on Pass By Reference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on Pass By Reference" Watch "Question on Pass By Reference" New topic
Author

Question on Pass By Reference

Sudhakar Krishnamurthy
Ranch Hand

Joined: Jun 02, 2003
Posts: 76
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
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Check this out - let me know if you still have questions.
Corey


SCJP Tipline, etc.
Sudhakar Krishnamurthy
Ranch Hand

Joined: Jun 02, 2003
Posts: 76
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
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
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 ]

Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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

Joined: Jun 02, 2003
Posts: 76
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on Pass By Reference
 
Similar Threads
What is the output and Why
static method
Date Format related
Date question
Programmer Certification