-3

String.equals() method in Java compares the backing arrays of two strings. So if the arrays are of different size same strings would not be equal.

Example:

char[] in = new char[50];
in[0] = 'K';
in[1] = 's';
in[2] = 'h';
in[3] = 'i';
in[4] = 't';
in[5] = 'i';
in[6] = 'z';
String s = new String(in);
System.out.println(s.equals("Kshitiz"));

Output:

false

Kshitiz Sharma
  • 17,947
  • 26
  • 98
  • 169
  • Ideally you shouldn't but you need to when getting text as char[] from an API. – Kshitiz Sharma Apr 28 '15 at 06:19
  • 2
    There are many ways to create a string from chars. If you want to pass an array, you should at least make it the right size. And `char` arrays are just like `char` values, they're not convenient and should rarely be used. – Denys Séguret Apr 28 '15 at 06:20
  • If the API returns a 50-long char array for a string of 7 chars, then the API is seriously f* up. Why would any sane API do that? – JB Nizet Apr 28 '15 at 06:21
  • When getting char[] from an API method, making it a particular size isn't under my control. – Kshitiz Sharma Apr 28 '15 at 06:21
  • @JBNizet Beats me... – Kshitiz Sharma Apr 28 '15 at 06:22
  • It's all about creating a `String` from a null-terminated `char`-array. Has nothing to do with comparing strings. You should really change your question title! – isnot2bad Apr 28 '15 at 06:47
  • @isnot2bad OP didn't know what was really the way to solve his problem. His title is relevant to the way he thought was the best when asking. – Denys Séguret Apr 28 '15 at 06:50
  • @dystroy I agree. But now he knows and the title doesn't make any sense now. Nevertheless, question is already closed. – isnot2bad Apr 28 '15 at 06:51
  • 1
    @dystroy That's right. That's why I'm not editing the title. Someone else thinking along the same lines is likely to find this more useful and google for similar terms. – Kshitiz Sharma Apr 28 '15 at 07:10

4 Answers4

3

You should not be creating strings that don't hold what they should hold. Using a hack like trim to deal with bad objects is a bad practice.

Assuming you can't fix the API giving you badly sized char arrays, just count the not null chars before creating the string.

    char[] in = new char[50];
    in[0] = 'K';
    in[1] = 's';
    in[2] = 'h';
    in[3] = 'i';
    in[4] = 't';
    in[5] = 'i';
    in[6] = 'z';
    int length = 0;
    while (length<in.length && in[length]!=(char)0) length++;
    String s = new String(in, 0, length);
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
0

I added one more code line to your code to show what happen here.

    char[] in = new char[50];
    in[0] = 'K';
    in[1] = 's';
    in[2] = 'h';
    in[3] = 'i';
    in[4] = 't';
    in[5] = 'i';
    in[6] = 'z';
    String s = new String(in);
    System.out.println(s.equals("Kshitiz"));
    //just use following
    System.out.println(s); 

Now run this code. you can see s having \u0000 char just after Kshitiz

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0

equals() returns false because the two strings are not the same.

If you actually print the original string, then you will see the difference.

public static void main(String[] args) {
    char[] in = new char[50];
    in[0] = 'K';
    in[1] = 's';
    in[2] = 'h';
    in[3] = 'i';
    in[4] = 't';
    in[5] = 'i';
    in[6] = 'z';
    String s = new String(in);
    System.out.println(s.equals("Kshitiz"));
    System.out.println(s+ "X"); // new line
}

O/P :

false
Kshitiz                                X

You can see that the two strings are not the same.

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
0

Java's Strings are not null-terminated.

If you create Strings from a byte or character buffer, use a constructor that lets you specify the length.

new String(bytes, 0, 7, "ASCII");
new String(characters, 0, 7);
Thilo
  • 257,207
  • 101
  • 511
  • 656
  • This would only work in this case. `trim()` is the way to go if you get char[] from somewhere else. – Kshitiz Sharma Apr 28 '15 at 06:25
  • trim() also throws away stuff from the beginning. And spaces and tabs from the end. If someone gives you raw bytes or characters, they will also give you the length. Or have it null-terminated. You could then search for the 0 byte. – Thilo Apr 28 '15 at 06:27