Click for PDF manual V2.0 – > interview | springboot | springcloud | programming ideas

One, foreword

I believe that we should use a lot of these three methods in daily development, especially the first two, often in the development of time, follow one’s inclinations, want to use which with which, since there is, it should have the truth of its existence, so under what circumstances to use which?

Two, code examples

1. Basic types

(1) Primitive types do not have toString() methods

(2) Recommended use

(3) Can not be strong rotation

(String) is a standard cast that converts an Object to a String. When casting (String), it is best to use instanceof to perform a type check to see if casting can be performed. Otherwise, a ClassCastException will be thrown. Note that the compiler does not indicate syntax errors when writing, so use this method with caution.

Instanceof judgment

2. Package type

(1) toString OK

(2) the String. The valueOf ()

Nature is ok. Recommended: Java advanced video resources

(3) The packaging type cannot be changed strongly

3. Null value problem

(1) The null pointer toString() is abnormal

(2) String.valueof () Returns the String “null”

(3) The null value is successfully converted

Third, source code analysis

1, the toString ()

2, String. The valueOf ()

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

Four,

1. ToString () may throw a null pointer exception

In this way, Java objects can call this method because the java.lang.object class already has a public method. ToString (). Ensure that object is not null. Otherwise, a NullPointerException will be thrown. When using this approach, it is common for derived classes to override the toString() method in Object.

String.valueof (), recommended, returns the String “null”

The string.valueof () method is recommended because it does not generate null pointer exceptions and is static. It is called directly from String. String.valueof () returns the String “null”. Not null.

3. (String) Strong transfer, not recommended

(String) is a standard cast that converts an Object to a String. When casting (String), it is best to use instanceof to perform a type check to see if casting can be performed. Otherwise, a ClassCastException will be thrown. Note that the compiler does not indicate syntax errors when writing, so use this method with caution.

* thanks for reading, hope to help you 🙂 *

Source: blog.csdn.net/guorui_java/article/details/117935620


Recommend 3 original Springboot +Vue projects, with complete video explanation and documentation and source code:

Build a complete project from Springboot+ ElasticSearch + Canal

  • Video tutorial: www.bilibili.com/video/BV1Jq…
  • A complete development documents: www.zhuawaba.com/post/124
  • Online demos: www.zhuawaba.com/dailyhub

【VueAdmin】 hand to hand teach you to develop SpringBoot+Jwt+Vue back-end separation management system

  • Full 800 – minute video tutorial: www.bilibili.com/video/BV1af…
  • Complete development document front end: www.zhuawaba.com/post/18
  • Full development documentation backend: www.zhuawaba.com/post/19
  • Online demos: www.markerhub.com/vueadmin

【VueBlog】 Based on SpringBoot+Vue development of the front and back end separation blog project complete teaching

  • Full 200 – minute video tutorial: www.bilibili.com/video/BV1PQ…
  • Full development documentation: www.zhuawaba.com/post/17
  • Online demos: www.markerhub.com:8084/blogs

If you have any questions, please come to my official account [Java Q&A Society] and ask me