In this quick tutorial, I’ll show you how to convert int to String in Java and vice-versa.

Int to String

To convert the int data type to String, we can use the valueOf() method provided in the String class:

public class Demo
{

   public static void main(String[] args)
   {
       // convert int to String
       int number = 5;
       String s = String.valueOf(number);
       System.out.println(s);
   }
}

It is important to note that we can use other primitives with the same method.

String to Int

On the other hand, to convert the String data type to int, we need to perform parsing.

To achieve this, we can use the parse*() methods provided in the wrapper classes:

public class Demo
{

   public static void main(String[] args)
   {  
       // convert String to int
       String s2 = "34";
       int number2 = Integer.parseInt(s2);
       System.out.println(number2);
   }
}

Key Takeaways

In this short tutorial, we learned how to convert int to String data type and vice-versa.

By Ana Peterlić

Ana is a Java Software Engineer with over 6 years of experience in designing and developing software solutions. She loves tutoring and helping others understand Java ecosystem.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x