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 String Question 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 "String Question" Watch "String Question" New topic
Author

String Question

Wai Iu
Ranch Hand

Joined: Apr 04, 2000
Posts: 39
I got the following question in one certification excercise:
public class test{
public static void amethod{
Byte b1 = new Byte("127");
if(b1.toString() == b1.toString())
System.out.println("True");
else
System.out.println("False");
}
}
Answer choice:
A) Compilation error, toString() is not avialable for Byte.
B) Prints "True".
C) Prints "False".
The correct answer is c)Prints "False". Please hekp me
understand why b1.toString() == b1.toString() is false?
Thanks!
Wai
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
In b1.toString() == b1.toString(), you are explicitly creating two new strings that are not on the string pool. Since they are different objects, the == fails.
 
Consider Paul's rocket mass heater.
 
subject: String Question
 
Similar Threads
Why this prints false?
Object Comparison
Doubt Regarding wrapper Class.
Kumar #9
Strange Byte Comparision