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

Simple usage of the Java String class

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the knowledge of "the simple use of Java String". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

This article illustrates the simple use of the Java String class with an example. Share with you for your reference, the details are as follows:

Instantiation of a String class

1 code

Public class NewString {public static void main (String args []) {String str1= "Hello World!"; / / directly assign the creation object str1 System.out.println ("str1:" + str1); / / output String str2 = new String ("where there is a will, there is a way"); / / create and initialize the object str2 System.out.println ("str2:" + str2); String str3 = "new" + "string" / / generate a new string str3 System.out.println ("str3:" + str3) in concatenation;}}

2 run

Str1:Hello Worldsman str2: where there is a will, there is a way to str3:newstring

Comparison of two string objects

1 code

Public class StringEquals {public static void main (String args []) {String str1 = "Hello World!"; / / Direct assignment String str2 = "Hello World!"; / / Direct assignment String str3 = "Hello World1"; / / Direct assignment String str4 = new String ("Hello World!"); / / Constructor assignment String str5 = str2; / / reference passing System.out.println (str1 = = str2) / / true System.out.println (str1 = = str3); / / false System.out.println (str1 = = str4); / / false System.out.println (str2 = = str5); / / true System.out.println (str1.equals (str2)); / / true System.out.println (str1.equals (str3)); / / false System.out.println (str2.equals (str5)); / / true}}

2 run

Truefalsefalsetruetruefalsetrue

This is the end of the content of "simple usage of Java String class". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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