-1

Possible Duplicate:
How do I compare strings in Java?

When I run the following code as "java XYZ BOY" the output is "Invalid type", how??

public class XYZ 
{
    public static void main(String args[])
    {


        if(args[0]=="BOY")
            System.out.println("He is boy");

        else if(args[0]=="Girl")
            System.out.println("She is girl");

        else
        System.out.println("Invalid type");


    }
}

However when I use args[0].equals("BOY"), it gives the desired output. I want to know why things go wrong here when I don't use String.equals()?

Community
  • 1
  • 1
Jignesh
  • 555
  • 1
  • 8
  • 18

3 Answers3

6

The operator == tests if two variables refer to the very same object which isn't what you're interested in. Instead you want to know if two String variables refer to String objects that hold the same characters in the same order and in the same case, which is what equals(...) does, or regardless of case, which is what equalsIgnoreCase(...) does.

All classes get a default equals(...) method from the ultimate parent class, Object, which if not overridden will do the very same thing as ==, as per the API:

The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).

Many classes have this method overridden to use their own test of equality that makes sense for the class. String is one such class. For more details on how String does this, please check out its API which states:

Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.

Having said all of this -- voting to close this question as a duplicate.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
1

You are seeing "Invalid Type" because you are using == operator. == operator is used to compare whether two objects having same value and their references should be identical. Though in your first condition you are using == operator it will return false. because even though values are same but objects are not identical. next in second condition you are checking BOY==GIRL which will return false. hence it will print "Invalid Type" i.e.

Pratik
  • 1,531
  • 3
  • 25
  • 57
1

Some objects are considered the same if they have the same contents. For those, use .equals(Object) to compare object contents.

Some objects are considered the same if they are the same instance. For those, use == to compare object instances.

You can alter an Object's content comparison by overriding public boolean equals(Object), but if you do so, remember that you must abide by three rules for the rest of the Java framework to work properly.

  1. Reflexive: a.equals(a) must be true.
  2. Symmetric: if a.equals(b), hen b.equals(a) must be true.
  3. Transitive: if a.equals(b) and b.equals(c) then a.equals(c) must be true.

In addition, the equality operation should be consistent, such that if a.equals(b) and no updates are made to a or b, then a.equals(b) should remain true.

Once you override equals, you can gain performance improvements in the hash related collections by overriding public int hashcode() to return identical values whenever equals(...) would return true. Also, it should have a very low chance of returning identical values if equals(...) returns false. There is not need to make a "perfect" hash, which is one where hashcode(...) is guaranteed to return different values if equals(...) returns false, because all of the Java Collections routines check equals(...) after checking for possible equality with hashcode(...).

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138