This is the 28th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

Writing in the front

When we want to convert a non-string value to a String, what does it do? You can do toString(), you can do string.valueof (), and you can do strong (String), so you can do whatever you want, you don’t have to think about it. What’s the difference?

There is a good saying that existence is reasonable, so since the JDK comes out with these methods, there must be a place for it. Is there a pros and cons, and which should be considered first in normal development?

Let me tell you, I recommend that you use string.valueof () normally. Why? Well, let’s figure it out.

We are divided into two parts. Throw out the problem instance first, then dive into the source code.

The code examples

Just for demonstration purposes. I’ll just take a screenshot. So you can see it a little bit better

  • First, there is no toString() method for primitive types

  • 2. But you can use string.valueof ()

It feels like String.valueof () can contain everything. Recommended.

  • 3. Try a strong turn

Same thing. Basic types can’t be cast away, so let’s look at the Object class and you know that Object is the parent of all the root classes, and it can go down. It can go down, but how can subclasses usurp the throne, so definitely not, empty

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

Use instanceof to do a type check

Package type test

Above we used the int base type to test. Now let’s use the encapsulation type Integer

Unlike the basic type. The toString method, String.valueof (), is available

Let’s look at a kind of null pointer problem

But string.valueof is fine

Null value conversion problem

good So I’ll leave the code examples here. Why do I suggest you go to String.valueof () and read the source code

The source code

Take a look at the source code for toString()

String. The valueOf the source code

You can obviously see that String.valueof () has one more non-null judgment than toString.

conclusion

  • ToString (), which 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.

  • (String) Strong transfer. This parameter is 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.

OK. That’s all for today. I’ll see you next time

overtones

Thank you for reading, and if you feel like you’ve learned something, you can like it and follow it. Also welcome to have a question we comment below exchange

Come on! See you next time!

To share with you a few I wrote in front of a few SAO operation

Copy object, this operation is a little SAO!

Dry! SpringBoot uses listening events to implement asynchronous operations