0Pricing
Java Academy · Lesson

Parsing and Valueof

Convert strings to numbers.

Parsing and Valueof is a free Java Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Text Into Numbers

User input and files arrive as String text. To do math you must convert those strings into numbers.

Wrapper classes provide two families of methods for this: parseXxx returns a primitive, and valueOf returns a wrapper object.

parseInt Returns a Primitive

Integer.parseInt takes a string and returns a plain int. This is the most common parsing method.

public class Main {
    public static void main(String[] args) {
        int n = Integer.parseInt("123");
        System.out.println(n + 1);
    }
}

valueOf Returns a Wrapper

Integer.valueOf takes a string and returns an Integer object instead of a primitive. It is handy when you need a wrapper directly, for example to put it in a collection.

public class Main {
    public static void main(String[] args) {
        Integer boxed = Integer.valueOf("456");
        System.out.println(boxed);
    }
}

Parsing Decimals

Use Double.parseDouble for floating-point text. It handles values with decimal points and even scientific notation.

public class Main {
    public static void main(String[] args) {
        double price = Double.parseDouble("19.99");
        System.out.println(price * 2);
    }
}

Parsing Other Types

Every numeric wrapper has a parse method: Long.parseLong, Double.parseDouble, Float.parseFloat, Short.parseShort, and Byte.parseByte.

public class Main {
    public static void main(String[] args) {
        long big = Long.parseLong("9000000000");
        System.out.println(big);
    }
}

Parsing Booleans

Boolean.parseBoolean returns true only when the text equals "true" ignoring case. Every other string, including null, returns false.

public class Main {
    public static void main(String[] args) {
        System.out.println(Boolean.parseBoolean("true"));
        System.out.println(Boolean.parseBoolean("TRUE"));
        System.out.println(Boolean.parseBoolean("yes"));
    }
}

Parsing in Different Bases

Integer.parseInt accepts an optional radix argument. Passing 2 reads binary, 16 reads hexadecimal.

public class Main {
    public static void main(String[] args) {
        int binary = Integer.parseInt("1010", 2);
        int hex = Integer.parseInt("ff", 16);
        System.out.println(binary);
        System.out.println(hex);
    }
}

Bad Input Throws

If the string is not a valid number, parsing throws a NumberFormatException. You should wrap risky parses in a try-catch block.

public class Main {
    public static void main(String[] args) {
        try {
            int n = Integer.parseInt("hello");
            System.out.println(n);
        } catch (NumberFormatException e) {
            System.out.println("Not a number!");
        }
    }
}

Watch the Whitespace

Parsing does not tolerate surrounding spaces. Integer.parseInt(" 5 ") throws an exception.

Always call trim() or strip() on user input first.

public class Main {
    public static void main(String[] args) {
        String raw = "  42  ";
        int n = Integer.parseInt(raw.trim());
        System.out.println(n);
    }
}

Numbers Back to Text

To go the other direction, use String.valueOf or Integer.toString. Both turn a number into its text form.

public class Main {
    public static void main(String[] args) {
        String text = Integer.toString(789);
        String text2 = String.valueOf(3.14);
        System.out.println(text.length());
        System.out.println(text2);
    }
}

parseXxx Versus valueOf

Remember the rule: parseInt gives a primitive int, while valueOf gives an Integer object. Choose based on whether you need a primitive or a wrapper.

Quick Check

Test what you learned about parsing.

Recap

You learned how to convert between strings and numbers:

  • parseInt, parseDouble, and friends return primitives
  • valueOf returns wrapper objects
  • Bad input throws NumberFormatException, so trim and catch it
  • parseInt can read other bases with a radix argument
  • Use toString or String.valueOf to go back to text

Frequently asked questions

Is the “Parsing and Valueof” lesson free?

Yes — the full text of “Parsing and Valueof” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “Parsing and Valueof”?

Convert strings to numbers. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Parsing and Valueof” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Primitive vs Wrapper Types
  2. Autoboxing and Unboxing
  3. Parsing and Valueof
  4. Autoboxing Pitfalls
← Back to Java Academy