3
String s1 = "a";
System.out.println(s1.equals('a'));

Output:

False

Can anyone please explain me why is it coming false even though the string s1 has only one character 'a'.

3
  • 2
    I fail to see how this result surprises you. The JavaDoc states very clearly "The result is true if and only if the argument [..] is a String object [..]". Commented Jan 13, 2018 at 6:50
  • I see it the other way around - it surprises me that you might assume they would be equal :| Commented Jan 13, 2018 at 18:36
  • @AashitGarodia Please read this meta.stackexchange.com/a/5235 Commented Jan 13, 2018 at 19:20

6 Answers 6

2

To understand why are you getting false, which is unexpected for you. First, you need to understand your code

s1.equals('a')

s1 is a String and 'a' is Character, so you are comparing two different object.

As per documentation :

true if the given object represents a String equivalent to this string, false otherwise

Now, come back to equals method implementation in String class.

// some more code
if (anObject instanceof String) {
  // code here
  // some more code
 }
 return false;

You could see, it is checking if object, which you passed is type of String ?? In your case, No, it is Character. So, you are getting false as a result.

Sign up to request clarification or add additional context in comments.

Comments

2

One should always consult the specification rather than the implementation (except when there is a reason to suspect that the implementation is wrong, which is not the case here). The specification for equals in this case, as quoted in the comment above, is Class String, method equals which says:

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.

Since 'a' is not a String the result is false. 'a' is a literal of type char but it is boxed to type Character because equals requires a reference and 'a' is a value.

Comments

1

No. A char and a String are two different things in Java.

A char is exactly a single character.

A String, on the other hand, is zero or more characters. You can have a String of length 0 or 1 or > 1. But a character can only be of length 1.

The way you define a character is by using a single quote during the assignment.

char first = 'a' // single quotes

For a String, you use double quotes.

String first = "a" // double quotes

3 Comments

OP compares Character and String, not char and String.
Oh. I see. Looks like I made a mistake. But, I think that the logic still holds true even if we use a Character object type.
Correct, both are different types compared to String and an equals call should never yield true. 'a' also is a char literal, but that will be boxed for the equals call, because there is no overloaded version of that method, which accepts a primitive type. Thus OP actually compares Character to String. But you're right, both types are very different.
1

Single quotes are used for literal char, double quotes for literal String. Further if you will look at the equals method in String class, it is overridden as follows:

public boolean equals(Object anObject) {
      if (this == anObject) {
          return true;
      }
      if (anObject instanceof String) {
          String anotherString = (String)anObject;
          int n = count;
          if (n == anotherString.count) {
              char v1[] = value;
              char v2[] = anotherString.value;
              int i = offset;
              int j = anotherString.offset;
              while (n-- != 0) {
                  if (v1[i++] != v2[j++])
                      return false;
              }
              return true;
          }
      }
      return false;
  }

Neither, char is an instance of String class nor they are referencing same object on heap.

Comments

0

Could be a difference between a single quote '' and double quote ""

1 Comment

I'm purposely doing that.
0

If you are using an IDE like Eclipse or IntelliJ, you can see how String.equals() implemented. The method checks the parameter is instance of String and return false if the parameter is not a String.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.