About Question enthuware.ocajp.i.v7.2.1204 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
aditya
Posts: 3
Joined: Sun Jul 27, 2014 6:20 am
Contact:

About Question enthuware.ocajp.i.v7.2.1204 :

Post by aditya »

HI Admin ,
when i do following it prints B

Code: Select all

public class TestClass{
  public static void main(String args[ ] ){
    Object a, b, c ;
    a = new String("A");
    b = new String("B"); 
    a = b;   //only swipe this code
    c = a;
       System.out.println(""+c);
  }
}
I read your explanation , but can you please tell me why in above case it is printing B .

Also in explanation you have mentioned that "..c starts pointing to the string object .." where this happens compile time or run time.?

Please explain more and clear my doubt.

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1204 :

Post by admin »

a = b; this assigns the objected pointed to by b (which is "B") to a. So a points to "B" now.
Then you have c = a; this assigns the objected pointed to by a (which is "B" because of above) to c. So c also points to "B" now.
System.out.println(""+c); // this prints B because c is pointing to B as explained above.

All this happens at run time.
If you like our products and services, please help us by posting your review here.

Deleted User 2655

Re: About Question enthuware.ocajp.i.v7.2.1204 :

Post by Deleted User 2655 »

Why is Object's class toString method not used in this example?

Why does the String's class toString() method overrides Object's?

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1204 :

Post by admin »

sugarkanke wrote:Why is Object's class toString method not used in this example?
You have asked a very fundamental question. And I would strongly suggest you to read about polymorphism in Java from a good book before attempting any further mock exams. You should come back to this question after reading that.
Why does the String's class toString() method overrides Object's?
I am not sure I understand your question. String's class toString() method overrides Object's toString because that is what the programmer (i.e. the one who coded String class) intended to do. Object's toString doesn't do what the String class wants to do.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

dxie1154
Posts: 9
Joined: Sat Jan 14, 2017 5:01 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1204 :

Post by dxie1154 »

So in this question c is pointing to a, and once a points to b, c is still sitting there pointing at new String("a") while a switched its reference and now points at new String("b")?

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1204 :

Post by admin »

That is correct.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 95 guests