Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Jxam - Just checking!

 
Gregory Garrison
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following question:
Consider the following piece of code and select the correct statements.
1. Object o = new String("abcd");
2. String s = o;
3. System.out.println(s);
4. System.out.println(o);
This obviously won't compile because of the downcast in line 2. However, this answer was supposedly incorrect: Change line 1 to String o = new String("abcd"). Did I just lose my mind here?

 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gregory,
By changing the first line to String o = new String("abcd"); the downcast compiler error is effectively removed. Therefore, the code will compile with no errors.

Regards,
Manfred.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic