Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old March 13th, 2006, 05:29 PM
Registered User
 
Join Date: Mar 2006
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default toString()

Byte b1 = new Byte("127");
2:
3: if(b1.toString() == b1.toString())
4: System.out.println("True");
5: else
6: System.out.println("False");


why is this method giving false, as toString() method returns 127 and(127==127) should be true..
why is the ans coming false?????
plz help..

Reply With Quote
  #2 (permalink)  
Old March 13th, 2006, 06:39 PM
Wrox Technical Editor
 
Join Date: Nov 2005
Location: , , Canada.
Posts: 271
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Use the following instead of 2 string comparisons:

if ( b1.Equals( b1 ) )

- A.Kahtava
Reply With Quote
  #3 (permalink)  
Old March 13th, 2006, 06:49 PM
Wrox Technical Editor
 
Join Date: Nov 2005
Location: , , Canada.
Posts: 271
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Equals() compares the object's contents (deep comparison)
== compares the reference values (shallow comparison)

"The equals() method determines whether two strings have the same characters, whereas the == operator determines if two operands refer to the same String object. == actually tests to see if the references in the variables point to the same memory address." - http://www-128.ibm.com/developerwork...-ebb0917a.html

- A.Kahtava
Reply With Quote
  #4 (permalink)  
Old March 20th, 2006, 09:52 PM
Authorized User
 
Join Date: Mar 2006
Location: , , .
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Best Example :-

public class Str
{
    public static void main(String args[])
    {
        String s1 = "hello";

        String s2 = "hello";

        String s3 = new String("hello");

        String s4 = new String("hello");

    System.out.println("s1==s2\t" + (s1==s2));
    System.out.println("s1==s3\t" + (s1==s3));
    System.out.println("s3==s4\t" + (s3==s4));

System.out.println("s1.equals(s2)\t" + (s1.equals(s2)));
System.out.println("s1.equals(s3)\t" + (s1.equals(s3)));
System.out.println("s3.equals(s4)\t" + (s3.equals(s4)));
    }
}


Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Why would you override ToString()? chobo2 C# 2008 aka C# 3.0 16 November 11th, 2008 10:01 PM
Request.UrlReferrer.ToString() mike72 BOOK: ASP.NET Website Programming Problem-Design-Solution 2 September 5th, 2005 09:31 AM
Problem with Request.UrlReferrer.ToString velateam .NET Framework 2.0 0 April 5th, 2005 08:22 AM
enum toString() overloading kramis8 C# 0 November 21st, 2004 06:55 PM
.ToString() functions cuts off zeros olambe BOOK: ASP.NET Website Programming Problem-Design-Solution 2 July 6th, 2004 10:20 AM



All times are GMT -4. The time now is 05:54 AM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.