Learn Java by Example: Java Program to convert int type variables to String

Java Program to convert int type variables to String

In this program, we will learn to convert the integer (int) variables into the string in Java.

 


Example 1: Java Program to Convert int to string using valueOf()


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

    // create int variable
    int num1 = 36;
    int num2 = 99;

    // convert int to string
    // using valueOf()
    String str1 = String.valueOf(num1);
    String str2 = String.valueOf(num2);

    // print string variables
    System.out.println(str1);    // 36
    System.out.println(str2);    // 99
  }
}

In the above example, we have used the valueOf() method of the String class to convert the int variables into string.

Note: This is the most preferred way of converting int variables to string in Java.


Example 2: Java Program to Convert int to string using toString()

We can also convert the int variable into string using the toString() method of the Integer class. For example,


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

    // create int variables
    int num1 = 476;
    int num2 = 78656;

    // convert int to string
    // using toString()
    String str1 = Integer.toString(num1);
    String str2 = Integer.toString(num2);

    // print string variables
    System.out.println(str1);    // 476
    System.out.println(str2);    // 78656
  }
}

In the above example, we have used the toString() method of the Integer class to convert the int variables into string.

 


Example 3: Java Program to Convert int to String using + Operator


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

    // create int variables
    int num1 = 3476;
    int num2 = 8656;

    // convert int to string
    // using + sign
    String str1 = "" + num1;
    String str2 = "" + num2;

    // print string variables
    System.out.println(str1);    // 3476
    System.out.println(str2);    // 8656
  }
}

Notice the line,

String str1 = "" + num1;

Here, we are using the string concatenation operation to convert an integer into the string.


Example 4: Java Program to Convert int to String using format()


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

    // create a int variable
    int num = 9999;

    // convert int to string using format()
    String str = String.format("%d", num);

    System.out.println(str);    // 9999
  }
}

Here, we have used the format() method to format the specified int variable into a string.

 

 

Python Example for Beginners

Two Machine Learning Fields

There are two sides to machine learning:

  • Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
  • Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.

Data Science Resources: Data Science Recipes and Applied Machine Learning Recipes

Introduction to Applied Machine Learning & Data Science for Beginners, Business Analysts, Students, Researchers and Freelancers with Python & R Codes @ Western Australian Center for Applied Machine Learning & Data Science (WACAMLDS) !!!

Latest end-to-end Learn by Coding Recipes in Project-Based Learning:

Applied Statistics with R for Beginners and Business Professionals

Data Science and Machine Learning Projects in Python: Tabular Data Analytics

Data Science and Machine Learning Projects in R: Tabular Data Analytics

Python Machine Learning & Data Science Recipes: Learn by Coding

R Machine Learning & Data Science Recipes: Learn by Coding

Comparing Different Machine Learning Algorithms in Python for Classification (FREE)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.