Home Is String sequence of characters in Java?
Reply: 6

Is String sequence of characters in Java?

Aashit Garodia
1#
Aashit Garodia Published in 2018-01-13 06:38:54Z
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'.

Jonathan Rosenne
2#
Jonathan Rosenne Reply to 2018-01-13 09:12:46Z

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.

thedanotto
3#
thedanotto Reply to 2018-01-13 06:43:20Z

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

Utkarsh
4#
Utkarsh Reply to 2018-01-13 06:49:48Z

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
jyshin
5#
jyshin Reply to 2018-01-13 07:04:59Z

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.

ydrall
6#
ydrall Reply to 2018-01-13 07:06:28Z

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.

Ravi
7#
Ravi Reply to 2018-01-13 09:18:20Z

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.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.443187 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO