Friday, April 11, 2008

Integer.getInteger. Are you kidding me?

Photos and diary are on haitus. Now a little technology.

I just discovered a method introduced in Java 5: the method Integer.getInteger(String):

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Integer.html#getInteger(java.lang.String)
Determines the integer value of the system property with the specified name.

The first argument is treated as the name of a system property. System properties are accessible through the System.getProperty(java.lang.String) method. The string value of this property is then interpreted as an integer value and an Integer object representing this value is returned. Details of possible numeric formats can be found with the definition of getProperty.

So let me see if I understand:
  • Integer.valueOf(String) converts a String to a number by assuming the String is a numeric representation. In other words. Integer.valueOf("12345") yields the number 12345.
  • Integer.getInteger(String) converts a String to a number by assuming the String is the name of a system property numeric representation. In other words. Integer.getInteger("12345") is likely to yield null.
Why would anybody consider this a sufficient distinction? How many bugs are people going to create by using getInteger when they meant valueOf and vice versa?

This type of overloading is called near-phrase overloading. I just made that term up right now. It's when people use very similar words to mean different things. Consider two words x and y, their general meanings gm(x) and gm(y), and their meanings in a given context, cm(x) and cm(y). If
distance(gm(x), gm(y))< distance(cm(x), cm(y))
then it's a bad use of x and y! Go find another x and y for their contextual uses. Really, they could have called it getIntegerProperty.

This is the worst case of avoidable ambiguity I've seen in Java; I expect better coming out of them.

Update: it turns out there is something worse: Boolean.getBoolean("true") is usually equal to Boolean.FALSE.

49 comments:

Anonymous said...

I thought that you would just like to know that you just really really helped me.

I'm probably going to start reading your blog now.

konberg said...

I do like to know that kind of stuff! Thanks, Anonymous!

Unknown said...

Just got bit by this one, really ugly bug...

Leo said...

Thanks, saved me a bit of time.

Anonymous said...

One more thanks, also ran into this. Googling for an Interger-class bug which I suspected led me to your post and will now let me sleep in peace.

Seriously, WTF Sun?

Especially when scrolling through the Method Summaries in the API-Doc it shows up before the valueOf entry and you suspect nothing - at least make "system property" bold, italics and blinking...

Cláudio Esperança said...

Thanks for this... It helped a lot.

Anonymous said...

I always use
Integer.parseInt(String)

It denotes that something is being "parsed", so there's less chance for confusion.

Anonymous said...

I cannot even begin to describe the level of fail at work here. Not only the ambiguity, but also, HOW IN THE WORLD did anyone seriously think it would be a good to scatter the getters for system properties all over the data type abstractions?

WHY?

Anonymous said...

Actually very few would make this mistake. It fits in and conforms perfectly with their design model (see: Boolean.getBoolean() for example). Perhaps if you do no research or are new to the Java business you could find this really irking, but it's really not a very big flaw.

greenlyblue said...

I think it's funny that you expect better from Java. It's one of the most verbose and ceremonial languages out there and it hasn't moved it's conceptual base forward in like a gazillion years.

Anonymous said...

I agree with greenyblue! I'm not an expert but I've been using a less well-known high level language for over a decade, recently picked up Processing then did a few months messing with Java. It was OK. Recently moved to using a C++ framework for graphics, etc. Compared to Java, I'm writing half the amount of code, it reads much clearer and runs 10 times faster. C++ is hard but Java is a constant WTF-athon.

James said...

I highly suggest that you read Joshua Bloch's "Java Puzzlers" - it covers a lot of the weird Java pitfalls, including this one.

It's saved me from having to debug one the of the weird "why in the world is it doing " bugs a few times, and it's a fun read to see where the Java designers went wrong (as written by one of them, after the fact!)

James said...

@Anonymous - while it's true he shouldn't be calling getInteger, Joshua Bloch (aka the third @author in Integer.java - http://www.docjar.com/html/api/java/lang/Integer$IntegerCache.java.html) has, in a talk, even said that it was a bad decision to have those functions being in the core numeric type libraries - it violates what he calls the "principle of least astonishment" - if you ask a software engineer with no java knowledge what "getInteger(String s)" in class Integer does, more likely than not, you'd expect it transforms the string into an integer. It's the least astonishing thing a method with that name in that class could possibly do - which is exactly what it SHOULD be doing. If it's astonishing to the users that it does , you've named it wrong.

You would NOT, without prior knowledge of the system, expect it to return a system property with that name. A system property is not a feature of the Integer class - rather, the design should have been the other way around. A SystemProperties method called getInteger would be obvious in purpose, even to someone who had never used the system.

So while he "should" know better, the API design is flawed and lends itself to walking into this trap.

Anonymous said...

decode and parseInt are a far more nefarious pair, as decode will behave exactly like parseInt in the majority of cases. Finding that bug was a real PITA, and only by chance I passed in a 0...9 number and got an exception that led me to the erroneous code.

With respect to getInteger vs valueOf I can only say that valueOf is such a repeated pattern that it shouldn't be confused with anything else (a weak justification I know).

Anonymous said...

Should be:

Integer i = Integer.getSystemProperty(sPropertyName);

or

Integer i = Integer.getSystemPropertyAsInteger(sPropertyName);

Since we're renaming things, maybe "Homonymal Overloading" is a better term for this bad practice than "near-phrase overloading".

Anonymous said...

Why didn't they name the function Integer.valueOfProperty(String)?

Anonymous said...

similar to parseint?

Anonymous said...

Integer.getProperty()

Calling it Integer.getIntegerProperty() is redundant. The function prototype would be:

int Integer.getInteger()

So renaming it to:

int Integer.getIntegerProperty() is sufficiently overkill. It's not like you would see:

float Integer.getFloatProperty()

That would be insanity, defined.

They made a naming flub, 'tis all.

Anonymous said...

learn how to read

"Returns true if and only if the system property named by the argument exists and is equal to the string "true"."

Anonymous said...

What a pointless article. The documentation says it all. If you don't know what a method does read the documentation. If you use IDE it appears in your intelli-sense. If you are a smart (you don't even have to be that smart) developer you know you should use valueOf or parseInt.

eoin said...

Why would you put a method for getting a system property in a core number type class?

Anonymous said...

"learn how to read" "If you don't know what a method does read the documentation"

These seem to be an argument that names don't really matter much. I strongly disagree.

Anonymous said...

man, I'm so glad I left Java before I could stumble into such ... issues.

Anonymous said...

This is a 1.0 artifact (should really be deprecated).

In those ancient days, system properties were much more commonly used and there was some convenience to this convenience.

There are a number of (in hindsight) glaring design flaws in the old 1.0 APIs (synchronized collections, Properties is-a HashTable, ...)

smilefreak said...

Wow thanks good to know :P

Anonymous said...

What a pointless article. The documentation says it all. If you don't know what a method does read the documentation.

Yes, you just proved you don't know anything about API design, the principle of least astonishment, code smell, the law of Demeter and programming in general.

Please, do not comment on programming posts again for at least a decade.

konberg said...

@Robert, sorry for the delay. Yes I much prefer your term "Homonymal Overloading."

David Anderson said...

Still relevant today, thanks.

Manipriyan said...
This comment has been removed by the author.
dewatampan said...

Kingdomtoto situs judi togel yang setiap hari memberikan kemenangan dengan mudah. Kingdomtoto juga cocok sebagai bandar darat alternatif untuk bermain togel online. Untuk melihat hasil result pasaran togel juga bisa disini ya bos.
Situs yang sama kami refferensikan untuk anda yaitu kinghorsetoto. Situs paito warna terlengkap.

Harsh Goekna said...

Awesome article, it was exceptionally helpful! I simply began in this and I'm becoming more acquainted with it better. The post is written in very a good manner and it contains many useful information for me. Thank you very much and will look for more postings from you.


digital marketing blog
digital marketing bloggers
digital marketing blogs
digital marketing blogs in india
digital marketing blog 2020
digital marketing blog sites
skartec's digital marketing blog
skartec's blog
digital marketing course
digital marketing course in chennai
digital marketing training
skartec digital marketing academy

Anonymous said...

Testimoni sabun klinskin
Cara membedakan sabun klinski asli dan palsu
Testimoni sabun klinskin
Cara membedakan sabun klinski asli dan palsu
Testimoni sabun klinskin
Cara membedakan sabun klinski asli dan palsu

OGEN Infosystem (P) Limited said...

Awesome, I’m really thank you for this amazing blog. Visit Ogen Infosystem for creative website designing and development services in Delhi, India.
Website Designing Company

aishu said...

Thanks for spending all your pleasant time to make such a Creative content for us. AWS Course in Chennai

Tutorials said...


Very informative and helpful. Thank You for sharing the blog.
JMeter training in chennai | JMeter course online

Raziah Lauretta said...

How to: Get Windows 11 Right Now For FREE
Do You Need A Microsoft Account For Windows 11 Home
Will Your PC Run Windows 11
How to: Change Taskbar Size in Windows 11
My Thoughts On Windows 11
How to: Take a Screenshot on Iphone
How to: Force Restart, Enter DFU, Recovery Mode on iPhone 11/11 Pro
How To: Reset & Restore your Apple iPhone 12
How To: Reset & Restore your Apple iPhone 7
How to: Reset & Restore your Apple iPhone X

cyber pc said...

thank you for taking the duration to proclamation this find the maintain for an opinion totally beneficial! Bandicam Crack Download

cyber pc said...

prevail! it may be one of the most useful blogs we've ever come across upon the situation. terrific data! Im plus an skillful in this subject matter correspondingly i can take your effort completely dexterously. thanks for the large backing. Quotes On Brothers Day

Sunny said...

Very informative and helpful. Thank You for sharing the blog. CyberArk Training in Hyderabad

Anonymous said...

. I truely genuinely like it. It's so precise and so tremendoorm. I assume this is an enlightening post and it's far extremely useful and educated. In this way, i would want to thanks of the knowledge you distribute,exact submit. I was very interested in the item, it is pretty inspiring i must admaddress 파워볼전용놀이터

Anonymous said...

Thanks for the exceptional proportion. Your article has proved your hard w 메리트카지노

Anonymous said...

consequently, i would really like to thanks for the efforts you have were given made in writing this article . I simply respect the shape of subjects you publish proper here. Thanks for sharing us a extraordinary data that is really beneficial. Specific day! 퍼스트카지노주소

Anonymous said...

공유해 주셔서 감사합니다! 다 알지는 못했지만 다른 뷰 옵션과 같은 일부 변경 사항이 마음에 들었습니다. 베트남 카지노

Anonymous said...

매우 유익한 게시물! 여기에는 성공적인 소셜 네트워킹 캠페인을 시작하는 데 도움이되는 많은 정보가 있습니다. 먹튀검증

careergearup said...

https://careergearup.com/best-alternative-to-laxmikant-polity/
https://careergearup.com/clat-coaching-in-hyderabad/

careergearup said...

https://careergearup.com/sociology-optional-syllabus/
https://careergearup.com/sociology-optional-coaching-online/
https://careergearup.com/clat-coaching-in-hyderabad/
https://careergearup.com/top-10-csat-coaching-institutes-in-hyderabad/
https://careergearup.com/sociology-optional-coaching-in-hyderabad/
https://careergearup.com/why-sociology-is-the-best/
https://careergearup.com/top-law-entrance-exams/

mulesoft training said...

thanks for valuable information
nice article

dellbhoomi training

jewel orchard said...

In the meantime, developers need to be vigilant and ensure they use the correct method based on their specific use case to avoid unexpected behavior in their code.
https://jewelorchard.com/wedding-destination-in-hyderabad/
https://jewelorchard.com/places-for-birthday-celebration-in-hyderabad/
https://jewelorchard.com/wedding-venue-in-hyderabad/
https://jewelorchard.com/pre-wedding-shoot-places-in-hyderabad/
https://jewelorchard.com/best-convention-hall-in-hyderabad/

Telkom University said...

How does the method Integer.valueOf(String) differ from Integer.getInteger(String) in its functionality? Telkom University