Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 13th, 2006, 06:29 PM
Registered User
 
Join Date: Mar 2006
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..

 
Old March 13th, 2006, 07:39 PM
Wrox Technical Editor
 
Join Date: Dec 2005
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
 
Old March 13th, 2006, 07:49 PM
Wrox Technical Editor
 
Join Date: Dec 2005
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
 
Old March 20th, 2006, 10:52 PM
Authorized User
 
Join Date: Mar 2006
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)));
    }
}







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 11: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 07:55 PM
.ToString() functions cuts off zeros olambe BOOK: ASP.NET Website Programming Problem-Design-Solution 2 July 6th, 2004 10:20 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.