Integer. getInteger, aren't you playing with me?

Source: Internet
Author: User

I just found a method declared in Java 5: Integer. getInteger (String ):

Http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Integer.html#getInteger (java. lang. String)

Obtains the integer of the system Attribute Based on the specified name.

The first parameter is considered as the name of the system property. System properties can be accessed through the System. getProperty (java. lang. String) method. 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.

Let me see if I understand:

Integer. valueOf (String) converts a String to a number by assuming that String represents a value. That is, Integer. valueOf ("12345") returns the number 12345.

Integer. getInteger (String) converts String into a number by assuming that String is the name of a system attribute value. That is to say, Integer. getInteger ("12345") should be null.

Who would think there is a clear difference between the two? How many bugs are caused by the use of valueOf but getInteger?

This method overload is called the near-phrase overloading ). This is the term I just invented.It means that people use very similar terms to express completely different things. Let's take a look at the two words x and y. In one case, they are used for gm (x) and gm (y '), in another context, cm (x) and cm (y) are used '). If you use

 
 
  1. distance(gm(x), gm(y))< distance(cm(x), cm(y)) 

This is an incorrect y usage for x! You should use variable names other than x and y in this context. In fact, that method should be called getIntegerProperty.

This is the worst case of ambiguity I have seen in Java. They should make things more beautiful.

Update: The result is a worse one: Boolean. getBoolean ("true"). The result is equal to Boolean. FALSE.

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.