Java Integer decode() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Integer

Definition

In Java, Integer.decode(text) turns text into an Integer and reads the base from a prefix, the same way Java source code does. 0x1F and #1F are hexadecimal, a leading 0 means octal, and everything else is decimal.

An optional - or + may come first. Unlike parseInt, decode lets users type the base themselves, which is handy for configuration values and colours.

A static method of java.lang.Integer (since Java 1.2): call it on the class, as Integer.decode(...). Integer lives in java.lang, so no import is needed.

Syntax

One argument: the text, with an optional sign and prefix.

Java
Integer a = Integer.decode("0x1F");   // 31 (hex)
Integer b = Integer.decode("#FF");     // 255 (hex)
Integer c = Integer.decode("010");     // 8 (octal!)
Integer d = Integer.decode("-42");     // -42 (decimal)
CallWhat it does
Integer.decode("0x1F")Hexadecimal: 0x or 0X prefix.
Integer.decode("#FF")Hexadecimal: # prefix, common in colours.
Integer.decode("010")Octal: a leading zero before more digits.
Integer.decode("-0x10")The sign comes before the prefix.

A leading zero means octal, so decode("010") is 8 and decode("08") fails. A plain "0" is just zero.

Library declarations
Java
// java.lang.Integer
public static Integer decode(String nm) throws NumberFormatException

Parameters

ParameterDescription
nm (String)The text to read. It may begin with - or +, then an optional prefix, then digits.

Return Value

Return ValueDescription
IntegerThe Integer object for the number. Unbox it with intValue() or let Java unbox it automatically.

Compare with Similar Methods

Compare with the plain parsing methods:

MethodHow it differs
parseInt(s)Always decimal unless you pass a radix; it rejects the 0x prefix.
valueOf(s, radix)You choose the base yourself, with no prefix.
parseUnsignedInt(s, 16)Reads eight hex digits that fill all 32 bits.
getInteger(name)Reads a system property and decodes it with the same rules.

Java Examples

Example 1 — Decode Hex, Octal, and Decimal Text

Decode text in each style Java source code allows and see the decimal result.

Java
public class IntegerDecodeDemo {
    public static void main(String[] args) {
        String[] texts = {"255", "0xFF", "#FF", "0377", "-0x10", "+12", "0"};

        for (String text : texts) {
            System.out.println(text + " -> " + Integer.decode(text));
        }
    }
}

How It Works

  1. Prefix chooses the base. 0xFF and #FF are hex, 0377 is octal, and plain digits are decimal.
  2. All three spellings of 255. Decimal 255, hex FF, and octal 377 all decode to the same number.
  3. Sign before prefix. -0x10 is minus hex 10, which is -16, and +12 is simply 12.
  4. A single zero. "0" has nothing after the zero, so it is plain decimal zero, not an octal prefix.

Example 2 — Split a Hex Colour into Red, Green, and Blue

Decode a web colour such as #3366CC and pull out its three channels with shifts.

Java
public class IntegerDecodeColour {
    public static void main(String[] args) {
        int rgb = Integer.decode("#3366CC");

        int red = (rgb >> 16) & 0xFF;
        int green = (rgb >> 8) & 0xFF;
        int blue = rgb & 0xFF;

        System.out.println("rgb:   " + rgb);
        System.out.println("red:   " + red);
        System.out.println("green: " + green);
        System.out.println("blue:  " + blue);
    }
}

How It Works

  1. One call reads the colour. The # prefix makes decode read 3366CC as hex, giving 3368652.
  2. Red is the top byte. Shifting right by 16 bits and masking with 0xFF leaves 0x33, which is 51.
  3. Green is the middle byte. Shifting by 8 bits leaves 0x66, which is 102.
  4. Blue is the low byte. The mask alone keeps 0xCC, which is 204.

Example 3 — Watch for the Octal Trap and Bad Input

Decode text with a leading zero, and see which inputs throw NumberFormatException.

Java
public class IntegerDecodeErrors {
    static String tryDecode(String text) {
        try {
            return String.valueOf(Integer.decode(text));
        } catch (NumberFormatException e) {
            return "NumberFormatException";
        }
    }

    public static void main(String[] args) {
        String[] inputs = {"010", "08", "0x", "0x-5", " 10", "", "-", "0xFFFFFFFF"};
        for (String text : inputs) {
            System.out.println("\"" + text + "\" -> " + tryDecode(text));
        }
    }
}

How It Works

  1. The octal trap. "010" starts with zero, so it is octal and equals 8, not 10.
  2. 8 is not an octal digit. "08" is read as octal too, and the digit 8 does not exist in base 8, so it fails.
  3. Nothing after the prefix. "0x", "-", and the empty string have no digits to read.
  4. Other failures. A sign after the prefix, a leading space, and 0xFFFFFFFF (too big for a signed int) are all rejected.

Frequently Asked Questions

Use Integer.decode("0x1F"), which returns 31. Integer.parseInt rejects the 0x prefix.
parseInt always reads the base you give it (decimal by default). decode chooses the base from a 0x, #, or leading 0 prefix.
A leading zero means octal, as in Java source code. 010 in base 8 is 8. Use parseInt for plain decimal numbers that may have leading zeros.
No. That value exceeds the signed int range and throws NumberFormatException. Use Integer.parseUnsignedInt("ffffffff", 16) instead.
It returns an Integer object. Java unboxes it automatically when you assign it to an int.

Next: divideUnsigned()

Divide two ints as unsigned 32-bit numbers.

divideUnsigned() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful