Compare the size of two strings:
I. The compareto () method can be used. In addition, the comparetoignorecase (string) method ignores case sensitivity and compareto (Object string). The return value is int. Take the compareto () method as an example:
1. If the string is equal, the return value is 0, and other values are not returned.
The comparison method is to first compare the size of the corresponding characters (ascall order), starting from the first letter.
2. If the first character is different from the first character of the parameter, end the comparison and return the difference between them (ASCII value)
(The value of the string before the negative value is smaller than that of the string before the positive value. The value of the string before the positive value is greater than that of the string after the negative value)
3. If the first character is the same as the first character of the parameter, compare the second character with the second character of the parameter,
Similarly, the length of the character is compared until the comparison or comparison is completed.
For example:
- String S1 = "ABC ";
- String S2 = "ABCD ";
- String S3 = "abcdfg ";
- String S4 = "1 bcdfg ";
- String S5 = "cdfg ";
- System. Out. println (s1.compareto (S2); //-1 (equal front, 1 smaller S1 length)
- System. Out. println (s1.compareto (S3); //-3 (equal front, small S1 length 3)
- System. Out. println (s1.compareto (S4); // 48 (the ASCII code of "a" is 97, and the ASCII code of "1" is 49, so 48 is returned)
- System. Out. println (s1.compareto (S5); //-2 (the ASCII code of "a" is 97, and the ASCII code of "C" is 99, So-2 is returned)
2. You can also use the "=" And. Equals () methods to determine the size:
1. "=" is an operator that judges equal values. If the two sides are basic data types, it judges values. If the two sides are compound data types (class types ), it determines the address. The address is the same, the content must be the same, the address is different, and the content is not necessarily the same.
2 .. the equals () method can only determine the compound data type. Because it inherits the object class, it determines the address in the memory on both sides by default. The result is the same as "=, however, in some other class libraries, equals () is overwritten. For example, classes such as string, integer, and data have their own implementations, and the comparison content is not an address.
For example:
- String str1 = new String("hello");
- String str2 = "hello";
-
- System.out.println("str1==str2: " + (str1==str2)); \\false
- System.out.println("str1.equals(str2): " + str1.equals(str2)); \\true
When they use (=) for comparison, they compare their storage addresses in the memory, so unless they are the same new object, their comparison result is true, otherwise the comparison result is false.
However, equals () has been rewritten in string, and it determines the content, not the address in memory, so it is true.
However, string is special, because we all know that JVM divides memory into three zones: Method zone, heap zone, and stack zone. In the method area, a String constant pool is maintained. The pool is used to store various types of strings generated during running, and the content in the pool is not repeated. The general object is not in this pool, and the object is placed in the heap. In this case, we need to analyze how to create a string:
1. when you create a String object STR = "X" in any way, the Java Runtime (running JVM) will hold this X to find whether there is a string object with the same content in the string pool, if it does not exist, a string STR is created in the pool. Otherwise, it is not added in the pool.
2. in Java, as long as the new keyword is used to create an object, a new object will be created (in the heap or stack.
3. If you create a String object by specifying a string directly or using a String concatenation, the system only checks and maintains the string in the string pool. If no string exists in the pool, it creates one in the pool! However, this string object will never be created in the stack area.
4. If you use an expression containing variables to create a String object, the system not only checks and maintains the string pool, but also creates a String object in the stack area.
For example:
- String str2 = "hello";
- String str3 = "hello";
- System.out.println("str3==str2: " + (str3==str2)); \\true
- System.out.println("str3.equals(str2): " + str3.equals(str2)); \\true
The reason for "true" is that "hello" is obtained from the constant pool. However, no matter whether the constant pool is used, you can create a new one, and the address is different.
Compare the size of two strings