Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to parse JavaString initialize string field

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces you how to analyze the JavaString initialization string domain, the content is very detailed, interested friends can refer to, hope to be helpful to you.

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.

On how to parse the JavaString initialization string domain to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report