Automation QA Testing Course Content

How to convert int to String in Java

In this article we are going to discuss converting int (primitive type) and Object type (wrapper) Integer to String and vice versa. There are many ways to do it in Java. How to convert int to String in Java - 1

How to convert int to string in Java

  1. Convert by adding an empty string.
    The easiest way to convert int to String is very simple. Just add to int or Integer an empty string "" and you’ll get your int as a String. It happens because adding int and String gives you a new String. That means if you have int x = 5, just define x + "" and you’ll get your new String.
    Here is an example:
    //  converting int to string, Java
    public class Demo {
       public static void main(String[] args) {
    
           int x = 5;
           //  java int to string
           String xText = x + "";
           //  the result output
           System.out.println("convert int to String, Java: " + xText);
           //  the int output
           System.out.println("our int: " + x);
           //  adding int and String gives the new String
           System.out.println("adding int = 5 and String = \"5\". The result is a new String = " + xText + x);
           //  integer to string, Java code
           Integer y = 7;
           String yText = y + "";
           System.out.println("convert Integer to String: " + yText);
           System.out.println("our Integer: " + y);
           System.out.println("adding Integer = 7 and String = \"7\". The result is a new String = " + y + yText);
       }
    }
    The output is:
    convert int to String, Java: 5
    our int: 5
    adding int = 5 and String = "5". The result is a new String = 55
    convert Integer to String: 7
    our Integer: 7
    adding Integer = 7 and String = "7". The result is a new String = 77
  2. Java convert int to string using Integer.toString(int)
    Object class is a root class in Java. That means every Java class is directly or indirectly inherited from the Object class and all Object class methods are available for all Java classes.
    Object has a special method toString() to represent any object as a string. So, every Java class inherits this method as well. However the good idea is to override this method in your own classes to have an appropriate result.
    The Integer class’ toString() method returns a String object representing the specified int or Integer parameter.
    Its Syntax:
    public static String toString(int i)
    The method converts argument i and returns it as a string instance. If the number is negative, the sign will be kept.
    Example:
    //  java integer to string using toString method
    
    public class Demo {
       public static void main(String[] args) {
    
           int x = -5;
           //  java convert int to string using Integer.toString
           String xText = Integer.toString(x);
           //  the result output
           System.out.println("convert int to String: " + xText);
           //  the int output
           System.out.println("our int: " + x);
           //  adding int and String gives the new String
           System.out.println("converting int = -5 and adding to String = \"-5\". The result is a new String = " + xText + Integer.toString(x));
    
    
       }
    }
    convert int to String: -5
    our int: -5
    converting int = -5 and adding to String = "-5". The result is a new String = -5-5
    You may use the toString method to convert an Integer (wrapper type) as well.
    Integer number = -7;
    String numberAsString = Integer.toString(number);
    System.out.println("convert Integer to String: " + numberAsString);
    The result is:
    convert Integer to String: -7
    You may use special Integer.toString method toString(int i, int base) that returns a string representation of the number i with the base base and than to String. For example
    The syntax is:
    public static String toString(int i, int base)
    Here is an example:
    int a = 255;
    //  binary
    String customString = Integer.toString(a, 2);
    System.out.println(customString);
    The output is a String binary representation of decimal number 255:
    11111111
  3. Convert int to String using String.valueOf(int)
    Method String.valueOf(int) returns the string representation of the int argument.
    The syntax of the method is:
    public static String valueOf(int i)
    Here is an example of Java convert int to String using String.valueOf(int):
    public class Demo {
       public static void main(String[] args) {
           int z = -5;
           //  Java int to String converting
     String zText = String.valueOf(z);
           //  the result output
           System.out.println("convert int to String: " + zText);
           //  the int output
           System.out.println("our int: " + z);
           //  adding int and String gives the new String
           System.out.println("converting int = -5 and adding to String = \"-5\". The result is a new String = " + zText + z);
       }
    }
    convert int to String: -5
    our int: -5
    converting int = -5 and adding to String = "-5". The result is a new String = -5-5
    You may do the same with an Integer (wrapper type of int):
    Integer number = -7;
    String numberAsString = String.valueOf(number);
    System.out.println("convert Integer to String: " + numberAsString);
    The output will be:
    convert Integer to String: -7
  4. Convert using DecimalFormat
    java.text.DecimalFormat is a class defined in java.text package and a subclass of NumberFormat. It is used for formatting a decimal number to a string representing following a certain pattern. We can use it for Integers as well.
    Example:
    import java.text.DecimalFormat;
    public class Demo {
       public static void main(String[] args) {
           int myNumber = 31415;
           DecimalFormat decimalFormat = new DecimalFormat("#");
           String myNumberAsString = decimalFormat.format(myNumber);
           System.out.println(myNumberAsString);
       }
    }
    The output is:
    31415
  5. Convert using String.format()
    String.format() is one more way to convert an Integer to String Object.
    Syntax
    public static String format(String format, Object... args)
    Example
    public class Demo {
       public static void main(String[] args) {
           int myNumber = 35;
           String myNumberAsString = String.format("%d", myNumber);  //  %d converter defines a single decimal integer variable.
           System.out.println(myNumberAsString);
       }
    }
    The output is:
    35

How to convert String to int in Java

Here is a couple of ways to convert string to int, Java lets do it using parseInt() and valueOf() methods.
  1. Java string to int using Integer.parseInt(String)
    parseInt is a static method of the Integer class that returns an integer object representing the specified String parameter.
    Syntax:
    public static int parseInt(String str) throws NumberFormatException
    Or
    public static int parseInt(String str, int radix) throws NumberFormatException
    Where str is a String you need to convert and radix is a base of the parced number
    Converting String to Integer, Java example using parseInt()
    public class Demo2 {
    //  convert string to int java
    
       public static void main(String args[]) {
           String str = "111";
    
           int num1 = Integer.parseInt(str);
           System.out.println("parseInt of the String = "
                   + num1);
    
           int num2 = Integer.parseInt(str, 2);//binary number
           System.out.println("parseInt of the String with radix parameter = " + num2);
       }
    }
    The output is:
    parseInt of the String = 111
    parseInt of the String with radix parameter =  7
    Here we’ve got 111 in the first variant and 7 for the second. 7 is a decimal form of binary 111.
  2. Convert using Integer.valueOf(String)
    Integer.valueOf(String s) is a Java method of Integer class. It returns an Integer decimal interpretation of a String object so it is used to convert String to Integer.
    Syntax:
    public static Integer valueOf(String str)
    
    Example Java String to Integer using valueOf method:
    public class Demo2 {
    //  java convert string to int using valueOf()
           String myString = "578";
           int parNum = Integer.valueOf(myString);
           System.out.println("Integer from String using valueOf() = " + parNum);
    
       }
    }
    Output:
    Integer from String using valueOf() = 578

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.