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'.
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'.
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 :
trueif the given object represents aStringequivalent to this string,falseotherwise
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.
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.
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
Character and String, not char and String.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.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.
Could be a difference between a single quote '' and double quote ""