2017.4.14 the differences between Java parseint and valueof

Source: Internet
Author: User

The difference between their return types is the biggest reason:


static int parseint (String s)
The string argument is parsed as a signed decimal integer.

static Integer valueOf (int i)
Returns an Integer instance that represents the specified int value.
Static Integer valueOf (String s)
Returns an Integer object that holds the value of the specified String.

From the return value you can see their difference parseint () returns the base type int
The valueof () returns the wrapper class integer integer that can be used with the object method, and the int type cannot be converted to the object type.

The Integer.parseint (Chuan) return value is of type int.
The integer.valueof (Chuan) return value is integer. If you assign an integer to the int type, the JRE will do the work itself.

The difference is still there. If you write a method with a formal parameter of type int, for example:
void Test (int a) {
Todo:
};

When you call this method, test (Integer.parseint (Chuan)) will translate, but test (integer.valueof (Chuan)) will translate the error

The role of Java string.valueof ()

A cast of data is required to read the data, and a string.valueof (a) can be used to cast a to a string type

Sometimes a is already a string type, but for the rigor of the code, will also add string.valueof (a), then string.valueof (a) omitted will not be wrong.

But sometimes it involves a server problem, so it's better to join String.valueof (a).

Using string.valueof () for data conversion, this method returns a "null" string if the converted data is null

String) method, if the converted data is NULL, a null object is returned instead of a "null" string.

2017.4.14 the differences between Java parseint and valueof

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.