Compare the size of two strings

Source: Internet
Author: User
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:


  
  
  1. String S1 = "ABC ";
  2. String S2 = "ABCD ";
  3. String S3 = "abcdfg ";
  4. String S4 = "1 bcdfg ";
  5. String S5 = "cdfg ";
  6. System. Out. println (s1.compareto (S2); //-1 (equal front, 1 smaller S1 length)
  7. System. Out. println (s1.compareto (S3); //-3 (equal front, small S1 length 3)
  8. 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)
  9. 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:


  
  
  1. String str1 = new String("hello");
  2. String str2 = "hello";
  3. System.out.println("str1==str2: " + (str1==str2)); \\false
  4. 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:


  
  
  1. String str2 = "hello";
  2. String str3 = "hello";
  3. System.out.println("str3==str2: " + (str3==str2)); \\true
  4. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.