How To Convert String To Int In Java?

Use `Integer.parseInt(“123”)`

Use `Integer.valueOf(“123”)`

Use `new Integer(“123”)` if needed, but it is deprecated for new code

Use `Integer.parseInt(str.trim())` to handle surrounding spaces

Use `Integer.parseInt(str, 10)` to parse with a specific radix

Use `Integer.decode(“123”)` for decimal, hex, or octal string formats

Handle `NumberFormatException` with `try-catch`

Check for `null` before converting

Use `Integer.valueOf(str).intValue()` if you need an `int` from an `Integer` object

Suggested for You

Trending Today