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 use String Class string in Java

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

Share

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

This article will explain in detail how to use String strings in Java. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

I. Preface

In java, just like C language, there is also a definition of string, and it has its own unique function. Let's learn about it together.

II. Overview of string classes

String is under the package java.lang, so you don't need a boot package.

String strings are the focus of java. The String class represents the string class, and all strings (such as "adf") belong to

This category, that is, those enclosed in "" double quotation marks fall into this category.

Third, the characteristics of strings

Strings are immutable and their values cannot be changed after creation.

Although the values of String are immutable, they can be shared. Sharing means that other members can also have this value.

The string effect is equivalent to an array (char []), but the underlying principle is a byte array (byte [])

Jdk8 used to be an array of characters, followed by an array of bytes after jdk9.

Fourth, the String constructor public String () / / creates a blank string object without any content. Public String (char [] ch) / / creates a character object based on the contents of the character array. Public String (byte [] b) / / creates byte objects based on the contents of the byte array. String s = "abc" / / the object is created by byte assignment, and the content is abc.

The figure shows:

Code demonstration:

Public class StringDemo {public static void main (String [] args) {String s1=new String (); / / create an empty string that does not include any content System.out.println ("S1:" + S1); char [] chs= {'axiomanagrine; String s2=new String (chs); / / create an object System.out.println for chs ("chs:" + S2) / / output ch:abc byte [] bys= {97Power98 s4:abc 99}; String s3=new String (bys); System.out.println ("bys:" + S3); / / output bys:abc, which will be converted to the corresponding Ascll code value String S4 = "abc"; System.out.println ("S4:" + S4); / / output s4:abc}}

To sum up, it is recommended to use direct assignment to get the string object.

Fifth, the characteristics of string objects.

For string objects created through new, each time new requests a memory space, although the content is the same, but the address is different.

Create an object by directly assigning a value, and assign the same value, because it is a constant in the constant pool of heap memory. The rule for entering the constant pool is: if there is no such constant in the constant pool, then create one in the constant pool. If so, assign the existing constant address to him, so create different variables to accept the same value, his content is the same, the address is the same.

6. The method of comparing strings

When we compare whether two numbers are the same, we usually use = to judge, so we need to compare the equality of two strings.

What is used? the answer is equals.

= = is used to determine whether the address of two strings is the same. The same returns true and the different returns false.

Equals is used to compare whether the values of two strings are the same. The same returns true and different returns false.

Usage:

Public class String1 {public static void main (String [] args) {String S1 = "hello"; String S2 = "world"; String S2 = "helloworld"; String s4rooms1rooms2; System.out.println (s7==s8); / / 0 System.out.println (s3==s4); / / compare whether the addresses of the two strings are the same System.out.println (s3.equals (S4)) / / compare whether the values in the two strings are the same}}

Code illustration:

The reasons are analyzed below.

7. Judge whether the addresses of two strings are equal

In a string, two strings can be added to a new string, which we know, but will the address be the same?

Look at the following code:

Public class String1 {public static void main (String [] args) {String S1 = "hello"; String S2 = "world"; String s3 = "helloworld"; String s4rooms1rooms2; String s5 = "he" + "llo"; String s6 = "hello" + "world"; String S7 = "hello" + S2; String s8rooms1 + "world"; System.out.println (s3==s6) / / compare whether two addresses are the same System.out.println (s1==s5); System.out.println (s3==s7); System.out.println (s3==s8); System.out.println (s7==s8); System.out.println (s3==s4); System.out.println (s3.equals (S4)); / / compare whether the values in the two strings are the same}}

Let's analyze it carefully:

The first one:

String S3 = "helloworld"; String S6 = "hello" + "world"

S3 first creates a constant of helloworld in the constant pool. S6 is concatenated with two new strings.

These two string constants create new string constants that are stored in the constant pool because helloworld already exists

So the constant pool does not create new strings, but directly assigns the existing S3 address to S6, so their address

The same.

Second:

String S1 = "hello"; String S5 = "he" + "llo"

This analysis is the same as the first one, with the same address.

The third:

String S2 = "world"; String S3 = "helloworld"; String S7 = "hello" + S2

S2 create world,s3 in constant pool create helloworld,s7 in constant pool is connected by a variable S2

The new string "world" first creates the string "world" in the constant pool, followed by a "+" between the two.

Operation, S7 creates a StringBuilder (or StringBuffer) in heap memory according to the concatenation rules of strings

Object, the string constant "world" and S2 are concatenated by the append method, and the string "helloworld" is spliced at this time.

Is an object of type StringBuilder (or StringBuffer) that is converted to a String pair by calling the toString method

Like "helloworld", so S7 actually points to the "helloworld" object in heap memory.

The address of the elephant is different from the address of the object in the constant pool.

The difference between StringBuilder and StringBuffer

1.StringBuffer synchronizes almost all methods, and the thread is relatively safe, which can be guaranteed in multithreaded systems.

Certificate data synchronization.

2.StringBuilder is not synchronized, thread is not safe, and StringBuilder cannot be used in multithreaded systems.

3. When using StringBuffer in scenarios that need to consider thread safety, if you do not need to consider thread safety, pursue the field of efficiency

You can use StringBuilder under the scene.

Fourth:

String S1 = "hello"; String S3 = "helloworld"; String S8rooms1 + "world"

Explain the same as above, to restate briefly, S3 first created helloworld,s8 in the constant pool by variables S1 and constants

World adds up to create a world in the constant pool first, but they will exist in heap memory now and later, so

They have different addresses.

Summary: general addition operations with variables are created in the heap

Fifth:

String S1 = "hello"; String S2 = "world"; String s7 = "hello" + S2; String s8rooms1 + "world"

The explanation is similar to the above, they all have variables plus constants, so they are all created in heap memory.

The address of heap memory will not be the same.

Sixth:

String S1 = "hello"; String S2 = "world"; String S3 = "helloworld"; String s4=s1+s2

First, create a unique constant in the constant pool, and then, S4 adds the two variables, resulting in the

In heap memory, so the address is different.

This is the end of this article on "how to use String strings in Java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please 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