Home > Java > javaTutorial > body text

Detailed explanation of valueOf method examples in java

零下一度
Release: 2017-05-25 14:52:18
Original
2785 people have browsed it

case1: Object to String

What needs to be emphasized is the String.valueOf() method. When the parameter type is object and the value is null, its processing method

    public 
static
 String valueOf(Object obj) {
        
return
 (obj == null) ? "null" : obj.toString();
    }
Copy after login

In this way, no error will be reported in Chengdu later, but it may be logically wrong. The general habit is to write str!=null && str.length()>0. When it is obtained by str = String.valueOf(obj), the above amount There is a problem in judging the conditions

So when you encounter an Object object and convert it to a String, it is best to force the conversion to str= (String) obj. At this time, when obj is null, str is still null. Case2: Object is loaded with basic types. Packaging type, (pay attention to integer and Long, Double)

First, after obj is assigned, obj still belongs to the packaging type of the basic type of the data, which can be determined by instanceOf.

So you can just use forced conversion. There is no need to use methods like Long.ParseLong(String.valueOf(obj)). One problem with this complicated problem is that the return value cannot contain null

If it is null, an exception will be thrown directly, because null has been converted to a string in the String.valueOf() method, and the Long.parseLong() method will report an exception if a null string is passed in,

Long.ParseLong(String.valueOf(obj)) method is equivalent to (long)obj, use basic type to force conversion

A better way is to use (Long)obj to force conversion, Determine and handle the null situation in the following program

case3: The basic type packaging type and String's vlaueOf method are different from the processing of obj=null, and the processing of String.valueOf(obj) The "null" string returned, while the basic type of packaging type returns null

I would like to emphasize that String is not a basic type, let alone a basic packaging type

Summary:

1 When encountering the obj type to string, it is best to force the conversion

2 When encountering the obj type to convert several times of the packaging type, it is best to force the conversion. If necessary, you can first use instanceOf to judge it. Type and then force transfer

Related recommendations】

1. The difference between valueOf, parseInt, and toString in Java

2. The difference between valueOf and toString, (String) in Java

3. Usage of tostring() and valueof() and the difference between the two

4. In-depth understanding of valueOf function and toString method

5. Introduction to object conversion function toString() and valueOf()

6. Use toString() method in JavaScript to return the time as a string

The above is the detailed content of Detailed explanation of valueOf method examples in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!