Results 1 to 8 of 8

Thread: String.equals()

  1. #1

    Thread Starter
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    String.equals()

    Why is it, that if I need to check equality between two strings I have to use String.equals() rather than ==?
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  2. #2
    New Member
    Join Date
    Dec 2005
    Posts
    5

    Re: String.equals()

    This is because strings are objects. An object is an instance of a class, in other words your String variable is a reference to a String object. If you use the == operator you are comparing the reference value rather than the object itself.

  3. #3

    Thread Starter
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: String.equals()

    I thought equals was one of those special methods link toString() which is invoked when two objects are compared?
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: String.equals()

    Java does not support operator overloading, so AFAIK the == operators will always compare object instances, whereas a specific .equals method can do what it likes (in this case compare string content). In .NET it is the other way round, the string class overloads the == operator for string comparison and .Equals executes a reference comparison.

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: String.equals()

    Which, IMHO, is quite braindead of C#. (Read: unintuitive) But I'm sure some people will disagree.

    == always compares the variable values in Java, no exceptions. And since every variable but the primitives is of reference type, it's always the reference value that's compared for objects.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6

    Thread Starter
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: String.equals()

    Thanks for the help. I just assumed that it be like the toString() method because all objects have an equals method. Out of interest would a comparison here be that of primitive types?
    Code:
    if ("String1" == "String2")
    Or would that again be a comparison between two references to String objects?
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: String.equals()

    String literals in Java are String objects, so the latter I'd assume.

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: String.equals()

    It would be objects. On the other hand, string literals are, I believe, interned, so identical string literals are actually the same object. At least in Sun's JVM.

    Also, toString() isn't as magical as you think. You may think that a string concatenation automatically generates toString() calls, but it really doesn't. This:
    Code:
    "The current date and time is " + new Date() + "."
    is translated to this (JDK 1.5):
    Code:
    new StringBuilder().append("The current date and time is ").append(new Date()).append(".")
    The "magic" of this comes from the overloaded append() method, which has the obvious String one and one for Object, that works like so:
    Code:
    public StringBuilder append(Object o) {
      return append(o.toString());
    }
    Other overloads are provided for the primitives.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width