File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Integer decode() vs. valueOf() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Integer decode() vs. valueOf()" Watch "Integer decode() vs. valueOf()" New topic

Integer decode() vs. valueOf()

lalit upadheyay
Ranch Hand

Joined: Jun 20, 2005
Posts: 110

Can someone clarify me the actual purpose and difference between two different functions for similar result in java.lang.Integer class. Their signatures are:

static Integer decode(String nm)
static Integer valueOf(String s)

Pls. also specify me whether i can pass an octal number as string to any of these methods and get back the hexadecimal number in a single step?

SCJP1.4, SCWCD1.4, SCBCD5.0(working on...)
John Dell'Oso
Ranch Hand

Joined: Apr 08, 2004
Posts: 130

The difference between these two methods is that with the decode() method you can pass octal and hexadecimal strings in addition to decimal strings. The single parameter version of the valueOf() method only interprets strings representing signed decimal integers.

For the decode() method, you pass radix specifiers such as "0x" or "#" for hex strings and "0" for octal strings.

The two parameter version of the valueOf() method is more flexible in that it can interpret strings represented in many different bases (up to base 36 I think).

Look at the following example:

Lalit, as much as I am happy to help you out here, the brief information I supplied above is all available from the Java API javadocs. You really should learn to use the javadocs for the API - it's a skill that will you serve you well.

[ July 04, 2005: Message edited by: John Dell'Oso ]
lalit upadheyay
Ranch Hand

Joined: Jun 20, 2005
Posts: 110
Thanks John Dell'Oso for the nice reply. After you suggestion i have found way to convert an octal number to hexadecimal number in a single step precisely. It can be done as:

I agree. Here's the link:
subject: Integer decode() vs. valueOf()
It's not a secret anymore!