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

What are the differences of toString (), String.valueOf, (String) strong turn in Java

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

Share

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

This article mainly talks about "what are the differences of toString (), String.valueOf, (String) strong turn in Java". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what are the differences between toString (), String.valueOf, (String) in Java?"

Catalogue

I. Preface

Second, code examples

1. Basic types

2. Encapsulation type

3. Null value problem

III. Source code analysis

1. ToString ()

2. String.valueOf ()

IV. Summary

I. Preface

I believe that you should use a lot of these three methods in daily development, especially the first two, often use whichever you like when developing, and since it exists, there should be a reason for it to exist. so under what circumstances?

Code example 1, basic types

(1) there is no toString () method for basic types.

(2) recommended use

(3) unable to turn forcefully

(String) is a standard type conversion. Convert Object type to String type. When using (String) strong conversion, it is best to use instanceof to do a type check to determine whether strong conversion can be performed, otherwise it is easy to throw a ClassCastException exception. It is important to note that when writing, the compiler will not prompt for syntax errors, so this method should be used with caution.

Instanceof judgment

2. Encapsulation type

(1) toString ok

(2) String.valueOf ()

Of course, it's possible.

(3) Encapsulation type cannot be forced.

3. Null value problem

(1) null pointer exception reported by toString ()

(2) String.valueOf () returns the string "null"

(3) the null value has been strongly changed successfully.

3. Source code analysis 1. ToString ()

2. String.valueOf ()

String.valueOf () has one more non-null judgment than toString.

IV. Summary

1. ToString (), which may cause a short pointer exception.

In this usage, because there is already a public method .toString () in the java.lang.Object class, any java object can call this method. Note, however, that you must make sure that object is not a null value, otherwise a NullPointerException exception will be thrown. With this approach, it is common for derived classes to override the toString () method in Object.

2. String.valueOf (), which is recommended and returns the string "null"

The String.valueOf () method is recommended by the editor because it does not have a null pointer exception, and it is a static method that can be called directly through String, but it is important to note that, as mentioned above, if the return result for null,String.valueOf () is the string "null". Not null.

3. (String) strong conversion, not recommended

(String) is a standard type conversion. Convert Object type to String type. When using (String) strong conversion, it is best to use instanceof to do a type check to determine whether strong conversion can be performed, otherwise it is easy to throw a ClassCastException exception. It is important to note that when writing, the compiler will not prompt for syntax errors, so this method should be used with caution.

At this point, I believe you have a deeper understanding of "what are the differences between toString (), String.valueOf and (String) in Java?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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