Sometimes we require to perform a mathematical operation on data received as a string. We have to convert the string value into an integer at that time, and then we can perform operations on it.
Note: The data received from the text field would always be a string form. Java provides the methods for converting the string to int.
Index
Integer.parseInt() is the function used for converting the string to int in Java.
This method is the static method of the class Integer. The signature of the parseInt function is as below:
public static int parseInt(String s)
Suppose we get the number as a text field of the form. Then we can convert that string into int as follows:
int x = Interger.parseInt("15");
The above code converts the string 15 into an integer and then assigns it to integer variable x.
Read Also: How to Check Armstrong Number in C Language
// Java program for converting the string into an integer using parseInt() function public class stringtoint{ Public static void main(String args[]){ String s = "20"; // declaration system.out.println(s); // string value int x = Integer.parseInt(s); // convertion of string into integer system.out.println(x); // int value } }
Output
20
20
Explanation
Note: In the above example, we print the value of the variable. So we cannot distinguish the changes in the result.
Let’s consider the following example, which creates a significant difference between integer and string type values.
Read Also: How to Convert String to Integer in Python
In this example, we use JAVA concatenation for understanding the use of parseInt() function in a better way.
// Java program for converting the string into an integer using parseInt() function with concatenation public class stringtoint{ Public static void main(String args[]){ String s = "20"; // declaration system.out.println(s + 19); // result: 2019 because of the concatination of two strings int x = Integer.parseInt(s); // convertion of string into integer system.out.println(x + 19); // result: 39 because of the addition of two numbers } }
Output
2019
39
Explanation
JAVA language also provides the Integer.valueOf() method for converting the string into an integer.
Note: In this method, we can assign value only to an object of class Integer, and in the parseInt() method, we assign value to a variable of type int.
// Java program for converting the string into an integer using Integer.valueOf() function public class stringtoint{ Public static void main(String args[]){ String s = "20"; // declaration system.out.println(s); // string value Integer x = Integer.valueOf(s); // convertion of string into integer system.out.println(x); // int value } }
Output
20
20
Explanation
Must Read: Hibernate Tricky Interview Questions for Experienced
This all about the conversion of JAVA string to int. This method helps when we need to take some number form string and do some operations on that number.
There is a reason big-name companies like CNN use WordPress. WordPress is a popular content…
In this tutorial, I'm going to show you how to install MySQL on your computer.…
Download Turbo C++ for windows 10 in just 7 Mb and run your first C++…
We can redirect any webpage to any other or redirect the whole domain or website…
There are lots of methods to redirect pages, like refresh-redirect from META tag, redirect from…
Include files in PHP are used in appending various global or config files. We can…