• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object reference and object itsself

 
fei peng
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
fei peng
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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))!
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
fei peng
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank for your recommendation resource, Valentin.
 
Ray Stojonic
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you,Stojonic
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic