aspose file tools*
The moose likes Java in General and the fly likes Dear Long.getLong, your method name is misleading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Dear Long.getLong, your method name is misleading" Watch "Dear Long.getLong, your method name is misleading" New topic
Author

Dear Long.getLong, your method name is misleading

Tim Cooke
Bartender

Joined: Mar 28, 2008
Posts: 1132
    
  59

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.


Tim Driven Development
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
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

Joined: Jan 10, 2002
Posts: 61434
    
  67

Cow-worthy -- you've been awarded a cow for helping others avoid this trap.

The method to use would be valueOf().


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Tim Cooke
Bartender

Joined: Mar 28, 2008
Posts: 1132
    
  59

@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

Joined: Mar 05, 2008
Posts: 3018
    
  10
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

Joined: Oct 13, 2005
Posts: 39436
    
  28
Mike Simmons wrote: . . . Bloch and Gafter's Java Puzzlers.
What a lovely book
 
 
subject: Dear Long.getLong, your method name is misleading