Home > Mobile >  Why "eat" and "tea" have different char[]?
Why "eat" and "tea" have different char[]?

Time:08-06

for String "tea", "eat", I used char[] to store the number of the letters in one word respectively. I think "tea" and "eat" should have the same char[] since they have the same number of letters. However, the code tells me that they have the different char[], why?

            String s = "eat"; // or "tea"
            char[] ca = new char[26];
            for (char c: s.toCharArray()){
                ca[c - 'a']  ;
            }

And after I use

String keyStr = String.valueOf(ca);

those two char[] become the same, why?

CodePudding user response:

I think what you are saying is that why does the array ca contains the same value for "tea" and for "ate".

This is because you are just computing the character frequency in the string s. The character frequencies in "tea" and "ate" are the same. The ca array simply order them based on their ascii code.

To have a better understanding, simply try "012022" and "220210". And here, imagine each digit as the index to the array. You will see the value at index 0 is 2, value at index 1 is 1, and the value at index 2 is 3.

CodePudding user response:

I'll fill in the obvious missing line here. What @Teresa is actually asking is why ca1 == ca2 resolves to false, where ca1 is produced by feeding "tea" into this algorithm and ca2 with "eat".

So, answering that question:

Because == does not mean what you think it means. It means 'reference identity', not 'same contents'.

In java, == does a direct value comparison. For all primitives (those are int, long, short, char, byte, float, double, and boolean - the list is hardcoded, you can't make your own primitive types), a 'direct value comparison' is just an equality comparison:

int a = 5;
int b = 5;
a == b; // this checks if 5 is equal to 5. Which it is, of course

But, all other values in java are objects, and 'direct value comparison' for those is checking if they are pointing at the same object. Notably, this also applies to arrays. char[] is not a primitive, it's an object.

Objects are like houses. Variables (like char[] a) are like pages in an addressbook. When you write a[5] , you're telling java: Take a, which is a page in an addressbook. Drive over to that address, open the front door, find the 5th room, walk in, and increment what you find there.

When you write a == b, you're asking java: Are they the exact same address. That's all. You can have 2 identical houses (built at the same time by the same person based on the same specs, and decorated by the same company with the same stuff), which have different street addresses. Are these houses 'equal'? That's a philosophical question of sorts.

Point is, when you use ==, java checks the addresses. So, 2 separate arrays that have identical values are NOT EQUAL. If you want to check actual values, specifically for arrays you use Arrays.equals(a, b) instead. For most other objects, you use a.equals(b).

Note that Strings are also objects. Here is a trivial example of these concepts:

String a = "hello";
String b = "world";
String c = "helloworld";
boolean referenceIdentity = (a   b) == c;
boolean valueEquals = (a   b).equals(c);
System.out.println(referenceIdentity); // false
System.out.println(valueEquals); // true

That's because this code makes 2 separate strings, each having the value "helloworld". == checks if they are the same object (no), .equals checks if these 2 strings have the same content (they do).

  • Related