Home » Java Tutorial » How to Convert String to Int Java with Examples

How to Convert String to Int Java with Examples

In java, you may have seen the methodologies to transform java int to string. But if a string consists of digits such as 1,9,45, etc., it is not feasible to conduct any mathematical operations until it is transformed into an integer. So, in that case, you should use the concept namely string to int java transformation.

Whenever we obtain TextField or TextArea content, the information stored is retrieved as a string. We, therefore, need to convert string to int java if the entered data is in numeric form. We use the Integer.parseInt() mechanism to do so.

Using the Integer.parseInt() technique, we can convert string to int in the coding language java. We can also use the Integer.valueOf() method to retrieve the Integer class reference.

We’ll see dual ways to transform java string to int. Those ways are as mentioned below:

  1. Integer.parseInt()—retrieval of primitive integer.
  2. Integer.valueOf()—gives the object of an integer back.

Methodology to transform a string to int java

The Integer wrapper class’s Integer.parseInt() mechanism evaluates the string as just a signed integer variable.

ParseInt() is the static Integer class form. The exact syntax of parseInt() is written below:

public static int parseInt (String demo_s)
{
  // code or statement
}

This is how its transformed from string to int java as shown in the below illustration: 

//Let’s use the methodology Integer.parseInt() to convert a string into an integer in java programming
class st_int_demo{
	public static void main(String args[]){
	//Let’s declare a random string value
	String s_demo= “679”;
	String s2_demo= “-270”;
	//Let’s convert the above declared random string into an integer
	int i_demo = parseInt(s_demo);
	int i2_demo= parseInt(s2_demo);
	System.out.println(“The value of i after its conversion from string into int is: ”+ i_demo);
	System.out.println(“The value of i2 after its conversion from string into int is: ”+ i2_demo);
}
}
# Output of this illustration will be:
# The value of i after its conversion from string into int is: 679
# The value of i2 after its conversion from string into int is: -270
# In this illustration, the negative or positive values don't affect. Both are treated the same in the transformation from string to int.

Let’s have a grasp on another illustration of string to int java.

//Let’s use the methodology Integer.parseInt() to convert a string into an integer in java programming
class st_int_demo2{
	public static void main(String args[]){
	//Let’s declare a random string value
	String st_demo= “28”;
	int it_demo=32;
	//Let’s convert the above declared random string into an integer
	int it_demo2 = parseInt(st_demo);
	int summation_demo=it_demo+it_demo2;
	System.out.println(“The summation or the result displayed is: ”+summation_demo);
}
}
# Output of this illustration will be:
# The summation or the result displayed is: 60

The alternate methodology to transform a string to int java

Integer.valueOf(String random_S) functions in the same way as Integer.parsetInt (String random_S). It also changes the value of a string to int java. Adding further, there is a distinction between Integer.valueOf() and Integer.parseInt(), while the method valueOf(String) recovers an integer class object, whereas the procedure parseInt(String) returns an int primitive item. The conversion performance will be the same, where either of the form you pick.

Let’s quickly dive in the illustration of the above methodology discussed:

//Let’s use the methodology Integer.valueOf() to convert a string into an integer in java programming
class st_vof_demo1{
	public static void main(String args[]){
	//Let’s declare a random string value
	String st_vof_demo= “189”;
	String st2_vof_demo= “-161”;
	//Let’s convert the above declared random string into an integer
	int i_vof_demo = Integer.valueOf(st_vof_demo);
	int i2_vof_demo= Integer.valueOf(st2_vof_demo);
	System.out.println(“The value of i after its conversion from string into int is: ”+ i_vof_demo);
	System.out.println(“The value of i2 after its conversion from string into int is: ”+ i2_vof_demo);
}
}
# Output of this illustration will be:
# The value of i after its conversion from string into int is: 189
# The value of i after its conversion from string into int is: -161

Another illustration of string to int java utilizing valueOf() methology is as shown below:

/Let’s use the methodology Integer.valueOf() to convert a string into an integer in java programming
class st_vof_demo2{
	public static void main(String args[]){
	//Let’s declare a random string value
	String st_vof_demo= “59”;
	int it_vof_demo= -16;
	//Let’s convert the above declared random string into an integer
	int it_vof_demo2 = Integer.valueOf(st_vof_demo);
	int summation_demo2=it_vof_demo+it_vof_demo2;
	System.out.println(“The summation or the result displayed is: ”+summation_demo2);
}
}
# Output of this illustration will be:
# The summation or the result displayed is: 43
Do you know?
1. Java Stack with Examples
2. ParseInt Java
3. Try Catch JAVA
4. For loop Java with Examples
5. Java vs Javascript
6. Java Iterator with Examples
7. Substring Java
8. Java Switch Statement with Examples
9. Java Queue
10. String to Int Java

Pin It on Pinterest