parseint() and toString() methods in java

  • parseInt() method

The method Integer.parseInt() in java is used is in parsing a String method argument into an Integer object. The Integer.parseInt() method in java is a very useful API which is used to convert a user input in String format to more strong object type like Integer. Similarly we can use parseDouble or parseFloat to convert the input Strings into double or float.  Let us see a simple example that asks user to input the String value and uses the parseInt method to convert it to Integer.

public class Parsing {
public static void main(String[] args){

String s = JOptionPane.showInputDialog("What is your roll number? ");

int r = Integer.parseInt(s);

JOptionPane.showMessageDialog(null, "Your roll number is: "+r);


In this example the program asks the user to input the number in the form of string and it converts it to integer using the parseInt() method and finally prints the result.

  • toString() method

This method does exactly what the parseInt() method does. It is used to get a String object representing a certain value of a number object. Let us do an example using toString() method.

public class ToStringMethod{

public static void main(String[] args){

int x = 6;








This will convert the given integer into String and print it.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s