Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
|
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 7th, 2006, 07:10 PM
Registered User
 
Join Date: Mar 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default problem with equals() and ==

can some one plz explain me the difference between these two things..
what is the difference betwn equals () and ==?
why do they show diffenett result while applyin on object references ..

 
Old March 7th, 2006, 07:27 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)

- A.Kahtava
 
Old March 10th, 2006, 01:37 AM
Registered User
 
Join Date: Mar 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks Adam..again one problem with --how to override equals () and hashcode() method..i never come to know how would they be implemented?
plz help me with this topic..also if u have any links , then plz do send them..
regards,
praveena

 
Old March 20th, 2006, 10:48 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)));
    }
}


 
Old March 24th, 2007, 12:11 AM
Authorized User
 
Join Date: Mar 2007
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

the == and equals are different
String s1="Malli";
String s2="Malli";
if(s1==s2) it fails because it compare the address only not content
if(s2.equals(s1)) it compares the content

K.Mallikarjunarao
 
Old March 26th, 2007, 07:29 AM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 373
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Malli,

i think you're mistaken, the s1==s2 will return true, as adam has mentioned its a shallow comparision it compares references, the s1 and s2 will point to the same reference, and the equals() method will return true.

Regards,
Rakesh
 
Old April 11th, 2007, 12:13 AM
Authorized User
 
Join Date: Mar 2007
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

== operator work only primitive data types
primitive data types are such as int,long,byte,double,float etc
equals operator work for reference only
such as Integer,Double,Float

K.Mallikarjunarao
 
Old June 15th, 2007, 08:05 AM
Authorized User
 
Join Date: Sep 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to vinay.bk
Default

Dear Mallikarjun

according to the above discussion both of u r right and perhaps u would like to have a look at the snippet mentioned in http://java.sun.com/docs/books/tutor...datatypes.html
This would tell u the special importance given to java.lang.String

You can read through these lines mentioned
In addition to the eight primitive data types listed above, the Java programming language also provides special support for character strings via the java.lang.String class. Enclosing your character string within double quotes will automatically create a new String object; for example, String s = "this is a string";. String objects are immutable, which means that once created, their values cannot be changed. The String class is not technically a primitive data type, but considering the special support given to it by the language, you'll probably tend to think of it as such.

Thanks & Regards
     vinay





Similar Threads
Thread Thread Starter Forum Replies Last Post
Refresh the browser equals insert a new record!? Magi BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 30 November 18th, 2007 06:01 PM
Invalid operator for data type. Operator equals di Pusstiu SQL Server 2000 2 August 10th, 2007 04:51 AM
how to override equals() and hashcode() praveena Java Basics 3 March 14th, 2006 02:30 AM
SQL "equals operator" in queries Steve777 SQL Language 1 June 24th, 2005 10:42 AM
Rules on use of == double equals (see p142, 146) ababb BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 1 January 20th, 2005 05:58 PM





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