This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dear Long.getLong, your method name is misleading

 
Tim Cooke
Sheriff
Pie
Posts: 2719
111
Clojure IntelliJ IDE Java
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A colleague just got burned with a misleadingly named method in the java.lang.Long class

Here's a quiz: What would be the value assigned to the variable longVar in the following code snip?

If, like us, you thought it converts the given String into a Long value and returns it, giving the Long value 42L then you're wrong. Here's what the JavaDoc for that method has to say for itself:

public static Long getLong(String nm)
Determines the long value of the system property with the specified name.


It looks up a System property! I would have never thought that in a million years. All I can say is thank goodness for Unit Tests or we'd have most likely let that little nugget of future pain slip through the net.
There's no question here, just sharing an experience.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It also says (same method) that details of the numbering are available under getProperty. Which getProperty? I looked under System#getProperty(java.lang.String) and couldn't see anything there.

And I thought java.awt.Color#getRGB() was bad because it returns aRGB not RGB.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64194
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cow-worthy -- you've been awarded a cow for helping others avoid this trap.

The method to use would be valueOf().
 
Tim Cooke
Sheriff
Pie
Posts: 2719
111
Clojure IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Campbell Oh yes I see. It's an all round bag of mystery.
@Bear Yes valueOf() is what's required. Many thanks for the cow. Much appreciated.

Tim "Cow Junkie" Cooke
 
Mike Simmons
Ranch Hand
Posts: 3028
10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, such nasty gotchas also exist for Integer.getInteger() and Boolean.getBoolean(). More such fun traps can be found in Bloch and Gafter's Java Puzzlers.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote: . . . Bloch and Gafter's Java Puzzlers.
What a lovely book
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic