Net-informations.com
SiteMap  | About    

Convert String to int

Converting a String to an int or Integer is a very common operation in Java. There are a few simple ways to do this conversion. The frequently used method is Integer.parseInt() .

Integer.parseInt() method

Integer.parseInt() Parses the string argument as a signed decimal integer and throws a NumberFormatException if the string can’t be converted to an int type.

The value of numStr would be 999.

If the first character is a minus(-) sign it will return a minus value.

The value of numStr would be -999.

If the given String is not valid, conversion will throws a NumberFormatException .

The above code will throws a NumberFormatException.

Example

Output

Integer.valueOf()

Java Integer.valueOf() returns an Integer instance representing the specified int value.

Output

Integer's Constructor

You can convert a string to an integer value using Integer's Constructor.

Output

Difference between parseInt() and valueOf()

Integer.valueOf() returns an Integer object, while Integer.parseInt() returns an int primitive.







net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.