The isNumber() method does not exist in Java — you check if a value is a number using other built-in tools

Java does not have a single isNumber() method that works on all data types. Instead, you use different approaches depending on what you are checking: whether a string contains only digits, whether a string can be converted to a number, or whether a variable already holds a numeric type. The method you choose depends on what your program needs to do with the value afterward.

The most common task is checking whether a string (text) can be parsed into a number before you try to convert it. This prevents your program from crashing when someone enters "abc" instead of "123". The other common task is checking the actual data type of a variable to confirm it is numeric.

Key Takeaways

  • Java's Integer.parseInt(), Double.parseDouble(), and similar methods throw an exception if the string is not a valid number, so you catch that exception to detect non-numbers.
  • The Character.isDigit() method checks whether a single character is a digit, useful for validating one character at a time.
  • Regular expressions with the matches() method let you check for specific number formats, like decimals or negative numbers.
  • Apache Commons Lang's NumberUtils.isCreatable() is a third-party tool that checks if a string can become a number without throwing an exception.
  • If you already have a variable and need to know its type, use instanceof to check whether it is an Integer, Double, or other numeric class.

Using try-catch with parseInt() and parseDouble()

The most straightforward way to check if a string is a valid number is to attempt to parse it and catch the exception if it fails. When you call Integer.parseInt("123"), Java converts the string to an integer. If the string is "abc", the method throws a NumberFormatException, which you can catch and handle.

Here is the pattern:

try {   int number = Integer.parseInt("123");   System.out.println("Valid number: " + number); } catch (NumberFormatException e) {   System.out.println("Not a valid number"); }

This works for integers, but if you need to check for decimals like "3.14", use Double.parseDouble() instead. The same try-catch pattern applies. You can also use Long.parseLong() for very large numbers or Float.parseFloat() for single-precision decimals.

The advantage of this approach is that it is built into Java and requires no extra libraries. The disadvantage is that it is verbose — you write several lines of code for a straightforward check. It also actually converts the string, so if you only want to validate without converting, you are doing extra work.

Checking individual characters with isDigit()

If you need to check whether a single character is a digit (0 through 9), use Character.isDigit(). This method returns true if the character is a digit and false otherwise.

char c = '5'; if (Character.isDigit(c)) {   System.out.println("This is a digit"); }

You can loop through a string and check each character one at a time. This is useful if you want to validate that a string contains only digits, or to count how many digits are in a string.

String input = "12345"; boolean allDigits = true; for (char c : input.toCharArray()) {   if (!Character.isDigit(c)) {     allDigits = false;     break;   } } System.out.println(allDigits);

This approach is fast and does not throw exceptions, but it only checks for digits. It does not handle negative signs, decimal points, or scientific notation like "1.5e10".

Using regular expressions with matches()

Regular expressions let you define a pattern and check whether a string matches it. The matches() method on a string takes a regex pattern and returns true if the entire string matches.

To check if a string is a positive integer:

String input = "12345"; if (input.matches("\\d+")) {   System.out.println("Valid positive integer"); }

The pattern \\d+ means "one or more digits". To allow negative numbers, use -?\\d+, where -? means "zero or one minus sign". To allow decimals, use -?\\d+(\\.\\d+)?, which matches an optional minus sign, one or more digits, an optional decimal point, and more digits.

Regular expressions are powerful but can be slow on very long strings, and the patterns themselves are hard to read if you are not familiar with regex syntax. For straightforward checks, try-catch or isDigit() is usually clearer.

Using Apache Commons Lang for cleaner code

The Apache Commons Lang library provides NumberUtils.isCreatable(), which checks whether a string can be converted to a number without throwing an exception. This method handles integers, decimals, negative numbers, and scientific notation.

import org.apache.commons.lang3.math.NumberUtils; if (NumberUtils.isCreatable("123.45")) {   System.out.println("Valid number"); }

This is cleaner than writing try-catch blocks, and it handles more formats automatically. However, it requires adding the Commons Lang library to your project, which is an extra dependency. If your project already uses Commons Lang for other tasks, this is a good choice.

An older method in the same library is NumberUtils.isNumber(), but isCreatable() is more reliable and is preferred in newer code.

Checking the data type of a variable with instanceof

If you already have a variable and need to know whether it is a numeric type, use the instanceof operator. This checks the actual type of the object, not whether a string can be converted.

Object value = 123; if (value instanceof Integer) {   System.out.println("This is an Integer"); } else if (value instanceof Double) {   System.out.println("This is a Double"); }

This is useful when you receive data from another part of your program and need to handle it differently based on its type. It does not convert anything — it only tells you what type the variable already is. Note that instanceof does not work on primitive types like int or double, only on objects like Integer or Double.

Comparing the methods side by side

MethodWhat it checksHandles decimalsHandles negativesRequires extra library
Integer.parseInt() with try-catchString can become an integerNoYesNo
Double.parseDouble() with try-catchString can become a decimalYesYesNo
Character.isDigit()Single character is 0–9NoNoNo
matches() with regexString matches a patternYes (if pattern allows)Yes (if pattern allows)No
NumberUtils.isCreatable()String can become any number typeYesYesYes (Commons Lang)
instanceofVariable is a numeric typeDepends on typeDepends on typeNo

Frequently Asked Questions

What is the difference between Integer.parseInt() and Double.parseDouble()?

Integer.parseInt() converts a string to a whole number and rejects anything with a decimal point. Double.parseDouble() accepts decimals like "3.14" and also accepts whole numbers. Use parseInt() when you know the input should be a whole number, and parseDouble() when decimals are allowed.

Can I check if a string is a number without using try-catch?

Yes. You can use Character.isDigit() to check each character, use a regular expression with matches(), or use NumberUtils.isCreatable() from Apache Commons Lang. Each approach avoids the try-catch pattern but has different trade-offs in readability and what formats they support.

Why does my code crash with NumberFormatException?

You called a parsing method like Integer.parseInt() on a string that is not a valid number. Wrap the call in a try-catch block to catch the exception, or check the string first using one of the other methods described here before attempting to parse it.

Does instanceof work on primitive types like int?

No. instanceof only works on objects. If you have a primitive int variable, you cannot use instanceof. You can only use it on wrapper classes like Integer, Double, or Long.

What regex pattern checks for both integers and decimals?

Use -?\\d+(\\.\\d+)? to match an optional minus sign, one or more digits, an optional decimal point, and more digits. This matches "123", "-45", "3.14", and "-2.5" but rejects "abc" or "1.2.3".