Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of String initialization string Domain in Java

Shulou Source: shulou.com Published: 2022-06-03 18:32:49 09月27日 Update

This article mainly shows you the "sample analysis of String initialization string domain in Java", which is easy to understand and well-organized. I hope it can help you solve your doubts. Let me lead you to study and study the "sample analysis of String initialization string domain in Java".

Exercise:

What's the difference between creating a class that contains a string field that is initialized when defined, and another string field that is initialized by a constructor?

Look at this procedure and the results, first of all, clear three knowledge points:

1.String is a reference object

2.equals () compares values

3. "=" is the address of the comparison

String S1 = "abc"; String S2 = "abc"; String S3 = new String ("abc"); String S4 = new String ("abc"); System.out.println (s1==s2); / / true System.out.println (s3==s4); / / false System.out.println (s1==s3); / / false System.out.println (s1.equals (S2)); / / true System.out.println (s1.equals (S2)); / / true

Java has two types of storage types, one is basic types of variables, such as int,float, and the other is reference objects, such as String, various class created by ourselves, arrays, and so on, which require new

It is generally stored in two places, the reference variables of basic types and objects are stored in the stack, and the objects from new are placed in the heap.

In addition, there is a block area constant pool, String S1 = "abc", and "abc" is stored in the constant pool. After we create another String S2 = "abc", the bottom layer of java finds "abc" in the constant pool and lets S2 point to this value and not re-create it. So the first one is true.

Our new is in the heap and will not be checked, so the address is different.

Look at another example.

String S1 = "aaa"; String S2 = "bbb"; String S3 = "aaabbb"; System.out.println (S3 = = S1 + S2); / / false System.out.println (S3 = = ("aaa" + "bbb")); / / true

In this example, S3 = = S1 + S2 when comparing, java opens up a memory in the heap for "S1 + S2", and then splices the strings.

After "aaa" + "bbb" is spliced, it is found that "aaabbb" is equal in the constant pool.

The above is all the content of the article "sample Analysis of String initialization string Fields in Java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Objects constants examples analysis content articles types examples variables addresses knowledge storage learning help three two memory regions places characters Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft MySQL Shulou Technology Huawei Linux