Site icon JavaGoal

Convert string to int

String in java is a unique data type and it is the most common data type that is used in programs.  Whatever we place in “ ”(Double quotes) is treated as a string.
Suppose we have a string “12345” and we want to convert string to int, Java provides some ways to parse string to int.

There are two ways to convert a java string to int, Let’s discuss them one by one with examples.
1. Integer.parseInt() – returns primitive data
2. Integer.valueIf() – return non primitive data

Integer.parseInt()

Java parseInt() method present in the Integer class that is used to convert string to int java.  It can access by className as it is a static method of the Integer class. It throws NumberFormatException if the string cannot be converted to an int type.

public class StringToInt
{
   public static void main(String args[])
   {
	   String s1 = "12345";
	   String s2 = "100";
	   
	   int a = Integer.parseInt(s1);
	   int b = Integer.parseInt(s2);
	   
	   int intValue = 1;
	   System.out.println(intValue + a);
	   System.out.println(intValue + b);
   }
}

Output: 12346
101

NumberFormatException in Integer.parseInt()

The parseInt() method converts the string to int only if the string has numbers. If it is not in number format then it throws an exception.

public class StringToInt
{
   public static void main(String args[])
   {
	   String s1 = "ABC12345";
	   try
	   {
		   int a = Integer.parseInt(s1);
	   }
	   catch(NumberFormatException e)
	   {
		   System.out.println("Invalid integer");
	   }
	   
   }
}

Output: Invalid integer

Integer.valueOf()

The Integer valueof() method exists in the Integer class that is used to convert a string to an Integer object. This method returns the object of the Integer class that holds an int value.  It can access by className as it is a static method of the Integer class. It throws NumberFormatException if the string cannot be converted to an int type.

public class StringToInt
{
   public static void main(String args[])
   {
	   String s1 = "12345";
	   String s2 = "100";
	   
	   int a = Integer.valueOf(s1);
	   int b = Integer.valueOf(s2);
	   
	   int intValue = 1;
	   System.out.println(intValue + a);
	   System.out.println(intValue + b);
   }
}

Output: 12346
101

NumberFormatException in Integer.valueOf()

The valueOf() method converts the string to Integer only if the string has numbers. If it has an alphabet also it throws an exception.

public class StringToInt
{
   public static void main(String args[])
   {
	   String s1 = "ABC12345";
	   try
	   {
		   int a = Integer.valueOf(s1);
	   }
	   catch(NumberFormatException e)
	   {
		   System.out.println("Invalid integer");
	   }
	   
   }
}

Output: Invalid integer

Different between parseInt() and valueOf() in java?

In Java, both Integer.parseInt() and Integer.valueOf() are used for converting a String to an integer value. However, there are some differences between these two methods that are important to consider when choosing which one to use in your code.

  1. Return Type: parseInt() method returns a primitive int value whereas valueOf() method returns an Integer object.
  2. Exception Handling: parseInt() method throws a NumberFormatException if the string cannot be parsed to an integer. On the other hand, valueOf() method returns an NumberFormatException wrapped in an IllegalArgumentException if the string cannot be parsed to an integer.
  3. Caching: valueOf() method uses a cache to improve performance by reusing previously created Integer objects for small values. On the other hand, parseInt() method does not use any caching.
  4. Autoboxing: valueOf() method returns an Integer object that can be used with autoboxing to automatically convert it to an int value. On the other hand, parseInt() method returns a primitive int value that cannot be autoboxed.

Based on the above differences, you can choose the appropriate method for your code. If you need an Integer object, then you can use valueOf() method, and if you need a primitive int value, then you can use parseInt() method. If performance is a concern and you need to convert small integer values frequently, then valueOf() method might be a better choice due to caching. However, if you need to handle exceptions explicitly, then parseInt() method might be a better choice as it throws an exception instead of returning null in case of errors.

Exit mobile version