How to convert value to string in java

Data conversion in Java

Sometimes we need to convert data types from e.g. a number to a string or vice versa. We explain the basics behind it in this tutorial.
Go on reading to get the full explanation of all conversion and casts between types. All code snippets will work in your preferred IDE.

Table of Contents

  1. Data type conversion in Java
    1. Widening or Automatic Conversion
    2. Narrowing or Explicit Conversion
    1. Converting Java int to String
    2. Converting Java byte to String
    3. Converting Java short to String
    4. Converting Java long to String
    5. Converting Java double to String
    6. Converting Java float to String
    7. Converting Java boolean to String
    1. Converting Java Character to Int
    2. Converting Java Double to Int
    3. Converting Java long to int
    4. Converting Java Int to Char

    In the tutorial What are data types in Java we have explained the Java data types. But using them will result quickly into the need to convert between them.

    That process is called Java Type Casting or Java Type Cast , and we use type cast in Java when we want to convert an object or variable of one type into another.

    1. Data type conversion in Java

    Changing a value from one data type to another type is known as data type conversion. There are 2 types of conversions:

    1.1. Widening or Automatic Conversion

    This is also named a lower to higher precision conversion and therefore done automatically by the compiler for us. Widening or Automatic conversion takes place when two data types are automatically converted. It can occur when

    • The two data types are compatible
    • When we assign value of a smaller data type to a bigger data type

    So a conversion in this direction will work out of the box:

    byte ➡ short ➡ int ➡ long ➡ float ➡ double

    class Low2High public static void main(String[] args) int i = 100; 
    long l = i;
    float f = l;
    System.out.println("Int value " +i);
    System.out.println("Long value " +l);
    System.out.println("Float value " +f);
    >
    >

    The output for this example will be :

    Int value 100

    Long value 100

    Float value 100.0

    Here is the screenshot from the Eclipse:

    dataConversion-low-2-high

    1.2. Narrowing or Explicit Conversion

    This is also called a higher to lower precision conversion often by developers named as cast or casting.
    So a conversion in this direction will need special handling:

    double ➡ float ➡ long ➡ int ➡ short ➡ byte

    Since this direction is losing information or precision the machine or compiler cannot decide for the human. Here we have to tell the compiler what he needs to do for us.

    class High2Low public static void main(String[] args) double d = 100.05; 
    long l = (long)d;
    int i = (int)l;
    System.out.println("Double value " +d);
    System.out.println("Long value " +l);
    System.out.println("Int value " +i);
    >
    >

    The output for this example will be:

    Double value 100.05

    Long value 100

    Int value 100

    Here is the screenshot from the Eclipse:

    dataConversion-high-2-low

    2. How to convert values to Strings?

    In any programming language we have to handle conversion from a human input (string) to a specific value mostly a number or a different specific type.

    2.1. Converting Java int to String

    Converting int to String is very common in Java, here is the most popular way to do it:

    The integer class has a static method that returns a String object representing the specified int parameter.

    public class Int2String public static void main(String[] args) int number = -125; 
    String numberAsString = Integer.toString(number);
    System.out.println(numberAsString);
    >
    >

    dataConversion-int-to-string

    We can reduce the above code to

    public class Int2String public static void main(String[] args) System.out.println(Integer.toString(-125)); 
    >
    >

    dataConversion-int-to-string

    There are other ways to do it, but this is the most popular way is:

    public class Int2String public static void main(String[] args) int number = -125; 
    System.out.println("anything to note" + number);
    >
    >

    dataConversion-int-to-string

    2.2. Converting Java byte to String

    This is similar method to integer to string, and it is used when you need to convert byte to String in Java, you can use this method of Byte class.

    Here is an example how to do it:

    public class Byte2String public static void main(String[] args) byte b = 20; 
    System.out.println(Byte.toString(b));
    >
    >

    The output of this example will be

    Here is the screenshot from the Eclipse:

    dataConversion-byte-to-string

    2.3. Converting Java short to String

    Using Short.toString(short d)

    Similar method as using byte to String, when you need to convert short to String in Java, you can use this method.

    public class Short2String public static void main(String[] args) short s = 25; 
    System.out.println(Short.toString(s));
    >
    >

    The output of converting to String will be

    Here is the screenshot from the Eclipse:

    dataConversion-short-to-string

    The output of converting to String will be

    2.4. Converting Java long to String

    This is an overloaded method, it can be used convert long to String. The method is static method of String class.

    Here is an example how to use this method:

    public class Long2String public static void main(String[] args) long l = 14861584l; 
    String s = String.valueOf(l);
    System.out.println(s);
    >
    >

    The output for this example will be

    Here is the screenshot from the Eclipse:

    dataConversion-long-to-string

    2.5. Converting Java double to String

    This is an overloaded method used for converting double to String. The valueOf() is the static method of String class.

    public class Double2String public static void main(String[] args) double d = 24.5; 
    String s = String.valueOf(d);
    System.out.println(s);
    >
    >

    Here is the screenshot from the Eclipse:

    dataConversion-double-to-string

    2.6. Converting Java float to String

    This method converts float to String. The toString() is the static method of float class.

    Here is an example of this method:

    public class Float2String public static void main(String[] args) float f = 45.7f; 
    String s = Float.toString(f);
    System.out.println(s);
    >
    >

    The output for this example will be

    Here is the screenshot from the Eclipse:

    dataConversion-float-to-string

    2.7. Converting Java boolean to String

    This method converts boolean to String. The toString() is the static method of boolean class.

    Here is an example of this method:

    public class Boolean2String public static void main(String[] args) boolean b1 = true; 
    boolean b2 = false;
    String s1 = Boolean.toString(b1);
    String s2 = Boolean.toString(b2);
    System.out.println(s1);
    System.out.println(s2);
    >
    >

    The outputs for this example will be

    Here is the screenshot from the Eclipse:

    dataConversion-boolean-to-string

    3. Now we have shown conversion to String, now we can show some conversion to Int

    3.1. Converting Java Character to Int

    We use char to int Java when we want to convert a char value in int, we define a char (for example char c = ‘a’;) and than convert it into int. There are few ways to convert it but the simplest way is to just take the char value and initialize it, here is an example;

    public class Char2Int public static void main(String[] args ) char c = 'a'; 
    int i = c;
    System.out.println(i);
    >
    >

    but let’s explain how it works, basically every character has its ASCII value, and when we convert it into int, the int will return its ASCII value.

    Here is the screenshot from the Eclipse:

    dataConversion-char-to-int

    Note: if we put in char number (example char = ‘1’;) when we convert it to int the return value will not be 1, the int will return its ASCII value (in this case 49)

    3.2. Converting Java Double to Int

    We use double to int Java in cases where we need double value converted in int. It is similar to previous conversion, and this type also has multiple ways of conversion, but we will show you the simplest one:

    public class Double2Int public static void main(String[] args ) double d = 4.255; 
    int i = (int) d;
    System.out.println(i);
    >
    >

    As a result, but even if we put really high decimal value (example 4.9999) the int value will return 4, this method will not round the value.

    Here is the screenshot from the Eclipse:

    dataConversion-double-to-int

    3.3. Converting Java long to int

    This conversion is similar to previous one, it is used when we want to convert long value to int, so here is an example:

    public class Long2Int public static void main(String[] args ) long l = 4245; 
    int i = (int) l;
    System.out.println(i);
    >
    >

    We have already explained how to convert char to int, but what about int to char?

    Here is the screenshot from the Eclipse:

    dataConversion-long-to-int

    3.4. Converting Java Int to Char

    Converting int to char java is made by typecasting. Similar to char to int, when int returned ASCII value of char, now we will have ASCII value of int, and char will return its value, here is an example:

    public class Int2Char public static void main(String[] args ) int i = 38; 
    char c = (char) i;
    System.out.println(c);
    >
    >

    If we put any other number and convert it, we would get different characters, because of the ASCII code

    Here is the screenshot from the Eclipse:

    Источник

    Int to String in Java – How to Convert an Integer into a String

    Ihechikara Vincent Abba

    Ihechikara Vincent Abba

    Int to String in Java – How to Convert an Integer into a String

    You can convert variables from one data type to another in Java using different methods.

    In this article, you’ll learn how to convert integers to strings in Java in the following ways:

    • Using the Integer.toString() method.
    • Using the String.valueOf() method.
    • Using the String.format() method.
    • Using the DecimalFormat class.

    How to Convert an Integer to a String in Java Using Integer.toString()

    The Integer.toString() method takes in the integer to be converted as a parameter. Here’s what the syntax looks like:

    Integer.toString(INTEGER_VARIABLE)

    In the example above, we created an integer – age – and assigned a value of 2 to it.

    To convert the age variable to a string, we passed it as a parameter to the Integer.toString() method: Integer.toString(age) .

    We stored this new string value in a string variable called AGE_AS_STRING .

    We then concatenated the new string variable with other strings: «The child is » + AGE_AS_STRING + » years old» .

    But, would an error be raised if we just concatenated the age variable to these other strings without any sort of conversion?

    The output above is the same as the example where we had to convert the integer to a string.

    So how do we know if the type conversion actually worked?

    We can check variable types using the Java getClass() object. That is:

    Now we can verify that when the age variable was created, it was an Integer , and after type conversion, it became a String .

    How to Convert an Integer to a String in Java Using String.valueOf()

    The String.valueOf() method also takes the variable to be converted to a string as its parameter.

    The code above is similar to that in the last section:

    • We created an integer called age .
    • We passed the age integer as a parameter to the String.valueOf() method: String.valueOf(age) .

    You can also check to see if the type conversion worked using the getClass() object:

    System.out.println(((Object)age).getClass().getSimpleName()); // Integer System.out.println(AGE_AS_STRING.getClass().getSimpleName()); // String

    How to Convert an Integer to a String in Java Using String.format()

    The String.format() method takes in two parameters: a format specifier and the variable to be formatted.

    In the example above, we passed in two parameters to the String.format() method: «%d» and age .

    «%d» is a format specifier which denotes that the variable to be formatted is an integer.

    age , which is the second parameter, will be converted to a string and stored in the AGE_AS_STRING variable.

    You can also check the variable types before and after conversion:

    System.out.println(((Object)age).getClass().getSimpleName()); // Integer System.out.println(AGE_AS_STRING.getClass().getSimpleName()); // String

    How to Convert an Integer to a String in Java Using DecimalFormat

    The DecimalFormat class is used for formatting decimal numbers in Java. You can use it in different ways, but we’ll be using it to convert an integer to a string.

    import java.text.DecimalFormat; class IntToStr < public static void main(String[] args) < int age = 2; DecimalFormat DFormat = new DecimalFormat("#"); String AGE_AS_STRING = DFormat.format(age); System.out.println("The child is " + AGE_AS_STRING + " years old"); // The child is 2 years old System.out.println(((Object)age).getClass().getSimpleName()); // Integer System.out.println(AGE_AS_STRING.getClass().getSimpleName()); // String >>
    • To be able to use the DecimalFormat class in the example above, we imported it: import java.text.DecimalFormat; .
    • We created the integer age variable.
    • We then created a new object of the DecimalFormat class called DFormat .
    • Using the object’s format() method, we converted age to a string: DFormat.format(age); .

    Summary

    In this article, we talked about converting integers to strings in Java.

    We saw examples that showed how to use three different methods – Integer.toString() , String.valueOf() , String.format() — and the DecimalFormat class to convert variables from integers to strings.

    Each example showed how to check the data type of a variable before and after conversion.

    Источник

    Читайте также:  Команды для css стилей
Оцените статью