aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Object reference and object itsself Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Object reference and object itsself" Watch "Object reference and object itsself" New topic
Author

Object reference and object itsself

fei peng
Ranch Hand

Joined: Sep 15, 2003
Posts: 40
Hi,every one.
The following question is form J@Whiz1.4:
Which of the statements below is true?
a.)Object references can be converted in assignments but not in method calls.
b.)Object references can be converted in both method calls and assignments, but the rules governing these conversions are very different.
c.)Object references can never be converted.
d.)Object references can be converted in both method calls and assignments, and the rules governing these conversions are identical.
e.)Object references can be converted in method calls but not in assignments.
I think the answer should be A,although the object its self may be changed
in a methode,the object refrences can't been changed in the method,because
it is passed into the methode by value,the object itsself is different with is references
but the answer is D.
what's wrong with my understanding to this question?
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
Originally posted by fei peng:
I think the answer should be A,although the object its self may be changed
in a methode,the object refrences can't been changed in the method,because
it is passed into the methode by value,the object itsself is different with is references
but the answer is D.
what's wrong with my understanding to this question?

fei peng, the object are passed to the method by reference, not by value... If you change it in the method, the reference also changed outside the method for sure... I think u'd better try with the real code, which passes the object into the paramter of a method and change its reference in the method... Then you will see that its reference can be change...
Hope u can find a reasonable solution...


Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
fei peng
Ranch Hand

Joined: Sep 15, 2003
Posts: 40
Hi,Ko Ko Naing
Java is actually pass by value for all variables running wthin a single VM(in K&B's book P184),it's true;
and I think the "object" is different from the "object reference",the last one is just a bit pattern like integer,when pass a object into a method
,a copy of this bit pattern is passed ,so origin "object reference" will never be modified in this method,
this is the reason that I can't agree the question's answer
Lee Ming
Greenhorn

Joined: Dec 15, 2003
Posts: 16
hi fei,
Consider the class member varibles, you may know the answer!
for example:
class A{
Object a;
void a(){
a = new Object();
}
}
the reference is changed after calling the method a()!
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
U will see it clearly in the following code...

If a class call the above method of test class and pass its connection into it, u will never get your connection back after the method call...
I hope it clears everything...
Jacky Chow
Ranch Hand

Joined: Sep 01, 2002
Posts: 63
hi Ko Ko,
In the case you express, the object reference never changed, the method just changes the state(s) of the object, the to reference still contains the object(with different state(s))!


Jacky
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
Originally posted by Jacky Chow:
hi Ko Ko,
In the case you express, the object reference never changed, the method just changes the state(s) of the object, the to reference still contains the object(with different state(s))!

What if it is assigned to other object reference like in Lee Ming's post... Maybe to another connection in my case...
It will surely change to another object reference...
Jacky Chow
Ranch Hand

Joined: Sep 01, 2002
Posts: 63

What if it is assigned to other object reference like in Lee Ming's post... Maybe to another connection in my case...

Lee's explanation is right, and it's also ok if the parameter pass to the method is a member variable in the same class!
fei peng
Ranch Hand

Joined: Sep 15, 2003
Posts: 40
In lee's post,The object reference is changed by assignment,but no matter
how it be changed, it happen in method,I think this question is not discribed well.
thank for you all.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Following are two great resources that will help you understand the concepts:
- JR Campfire Story: Pass-by-Value Please
- Corey McGlone's Flash Animation
Happy learning


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
fei peng
Ranch Hand

Joined: Sep 15, 2003
Posts: 40
thank for your recommendation resource, Valentin.
Ray Stojonic
Ranch Hand

Joined: Aug 08, 2003
Posts: 326
Back to the original question.
Fei, the statements have to do with casting.
In fact, if you replace 'converted' with 'cast' in each line, the meaning becomes clear:

If you review JLS 5.3, you'll see that D is the right answer.
[ December 19, 2003: Message edited by: Ray Stojonic ]
fei peng
Ranch Hand

Joined: Sep 15, 2003
Posts: 40
Thank you,Stojonic
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object reference and object itsself