Home > Java > javaTutorial > body text

Introduction to String string addition and comparison (detailed)

不言
Release: 2019-04-13 09:36:36
forward
4085 people have browsed it

This article brings you an introduction to the addition and comparison of String strings (detailed). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. help.

String String addition and comparison

public static void main(String[] args)  {
    String a = "helloword";
    final String b = "hello";
    String d = "hello";
    String c = b + "word";
    String e = d + "word";
    String f ="hello"+"word";
    System.out.println(a == c);
    System.out.println(a == e);
    System.out.println(a == f);

}
Copy after login

输出:
true
false
true
Copy after login

The first thing to make clear is the reference data type = = What is compared is the address value, equal is not rewritten, what is compared is the address value, after rewriting, what is compared is the content. String is rewritten, StringBuffer is not rewritten

Secondly:

a= =c is true because b is a final type. When the final variable is a basic data type or String type, if its exact value can be known during compilation, the compiler will use it as a compile-time constant. That is to say, where the final variable is used, it is equivalent to a direct access to the constant and does not need to be determined at runtime. Therefore, in the above piece of code, since the variable b is modified by final, it will be treated as a compiler constant, so where b is used, the variable b will be directly replaced with its value. So c = b "word" is equal to c = "hello" "word" is the same as ---> When final, the content is compared directly instead of the address.

a ==e is false because e = d "word" actually creates a StringBuffer object, and then uses the StringBuffer object to execute the append method to create the string object "ab", and then converts it to String . But this converted String object, that is, the s3 above, is placed in the heap. And s4 is a string constant, placed in the constant pool. So what is returned is false. ----->The address values ​​are different

a ==f is true because: When the constants are added, they are actually added directly to "helloword" during compilation. This is the JVM's Optimized, so when running, the bytecodes of a and f are the same. Because there is a "helloword" in the constant pool, the two references point to a string "helloword", so the return result is also true. ----->Point to the same address

[Related recommendations: Java Tutorial]

The above is the detailed content of Introduction to String string addition and comparison (detailed). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template