Ambiguous integer. getinteger and integer. valueof Methods

Source: Internet
Author: User

The integer class has two static methods that look very similar. One is integer. getinteger (string), and the other is integer. valueof (string ). If you only look at the method name, it is easy to split the functional areas of the two methods, or let's take a look at the Java documentation.

Integer. getinteger (string) is used to obtain the integer of the system Attribute Based on the specified name. The first parameter is considered as the name of the system property. You can use
The system. getproperty (Java. Lang. String) method is accessed. The property value string is interpreted as an integer and returned as an integer representing the value. You can find detailed descriptions of possible numeric formats in the getproperty definition description.

Integer. valueof (string) is used to obtain the integer represented by a given string.

Let me see if I understand:

  • The integer. valueof (string) method assumes that the string parameter represents a value and converts the value string to an integer. That is to say, integer. valueof ("123") gets an integer object whose value is 123.
  • The integer. getinteger (string) method assumes that the string parameter is the name of a system attribute value, reads the system attribute, and converts the value of the system attribute into a number. That is to say, integer. getinteger ("12345") should be null (assuming there is no system attribute named 12345 ).


Although the functions of these two methods are different, they are not clearly distinguished from the method name, which is often confusing. There are many bugs because the valueof function is intended but the getinteger method is used incorrectly.

This is a bad example of ambiguity in the Java language. Another bad example is boolean. getboolean ("true "). The function of Boolean. getboolean (string) is similar to that of integer. getinteger (string. Generally, we do not have a system attribute named "true". Therefore, Boolean. getboolean ("true") usually returns Boolean. False. This is even worse than integer. getinteger (string). It is better to return a null value to make it easier to discover errors.

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.