zzh

zzh

Java equals

We mainly introduce the String.equals function before the java 8 version.
The source code is as follows:
image
Very simple, first check if the addresses of the two string objects are the same, if they are the same, return true; secondly, since java 8 uses a char[] array to store string values, it will take out the corresponding char value one by one and check if they are the same.

Note

In Java 8 and earlier versions, strings internally 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 introduced 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), the data is stored in 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, the data is still stored in 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.