zzh

zzh

Java equals

We mainly introduce the String.equals function before the java 8 version.
The source code is as follows:
image
It's very simple. First, it checks whether the addresses of the two string objects are the same. If they are the same, it returns true. Secondly, because java 8 uses a char[] array to store string values, it will take out the corresponding char values one by one and check if they are the same.

Note

In Java 8 and earlier versions, strings use a char array to store character data and use an additional int field to record the offset and length of the string. This representation wastes space in strings that contain a large number of ASCII characters because each character still occupies 2 bytes of storage space.
Java 9 introduces the concept of Compact Strings. For strings that only contain the Latin-1 character set (i.e., Unicode code points between U+0000 and U+00FF), they are stored using a byte array, with each character occupying only 1 byte. This greatly reduces the memory usage of such strings. For strings that contain non-Latin-1 characters, they are still stored using a char array, with each character occupying 2 bytes.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.