This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Byte class(Wrapper class) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Byte class(Wrapper class)" Watch "Byte class(Wrapper class)" New topic
Author

Byte class(Wrapper class)

lalitha kaparapu
Greenhorn

Joined: Nov 05, 2007
Posts: 16
I have tried a code like this.But surprisingly I found that output is false instead of true..Can anyone help in this regard???
class Test{
public static void main(String args[])
Byte b1 = new Byte("127");
if(b1.toString==b1.toString)
System.out.println("True");
else
System.out.println("False");
}
}
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39575
    
  27
Welcome to JavaRanch.

String equality is tested by the String.equals method, not by the == operator. Try "b1.toString().equals(b1.toString())".

Since the toString method creates a new string every times it's called, the objects you're comparing are not equal in the "==" sense.


Ping & DNS - updated with new look and Ping home screen widget
 
 
subject: Byte class(Wrapper class)
 
Similar Threads
* Byte == comparision
comparison
related to "=="
Byte and String
String Question