In this article, we will understand with a Java program on how to convert primitive data-types into String using Java 1.8 version
Already in one of the previous article, we discussed how to convert primitive data-types into String using earlier versions of Java like 5 or 7, etc.
Convert primitive data-types into String :
- valueOf() method of String
- This String method is used to return string representation of primitive data-types & Objects
- There are 9 variants or overloaded valueOf() method, those are,
Method signature:
public static String valueOf(boolean b);
public static String valueOf(char c);
public static String valueOf(char[] data);
public static String valueOf(char[] data, int offset, int count);
public static String valueOf(int i);
public static String valueOf(long l);
public static String valueOf(float f);
public static String valueOf(double d);
public static String valueOf(Object obj);
ConvertPrimitiveIntoString.java
package in.bench.resources.java8.string.methods;
import java.util.stream.Stream;
public class ConvertPrimitiveIntoString {
public static void main(String[] args) {
// 1. boolean variables
boolean boolTest1 = false,
boolTest2 = true;
// 1.1 convert boolean to String
String boolToStr1 = Stream.of(boolTest1).map(String::valueOf).findAny().get();
System.out.println("boolean (false) converted to String = " + boolToStr1);
// 1.2 convert boolean to String
String boolToStr2 = Stream.of(boolTest2).map(String::valueOf).findAny().get();
System.out.println("boolean (true) converted to String = " + boolToStr2);
// 2. char variables
char charTest1 = 'z',
charTest2 = 'N';
// 2.1 convert char to String
String charToStr1 = Stream.of(charTest1).map(String::valueOf).findAny().get();
System.out.println("\nchar 'z' converted to String = " + charToStr1);
// 2.2 convert char to String
String charToStr2 = Stream.of(charTest2).map(String::valueOf).findAny().get();
System.out.println("char 'N' converted to String = " + charToStr2);
// 3. char[] Arrays variables
char[] charArrTest1 = {'B', 'e', 'n', 'c', 'h'},
charArrTest2 = {'F', 'r', 'i', 'e', 'n', 'd'};
// 3.1 convert char[] Arrays to String
String charArrToStr1 = Stream.of(charArrTest1).map(String::valueOf).findAny().get();
System.out.println("\nchar[] Arrays {'B', 'e', 'n', 'c', 'h'} converted to String = "
+ charArrToStr1);
// 3.2 convert char[] Arrays to String
String charArrToStr2 = Stream.of(charArrTest2).map(String::valueOf).findAny().get();
System.out.println("char[] Arrays {'F', 'r', 'i', 'e', 'n', 'd'} converted to String = "
+ charArrToStr2);
// 4. double variables
double dblTest1 = 19.23456,
dblTest2 = 258.589635;
// 4.1 convert double to String
String dblToStr1 = Stream.of(dblTest1).map(String::valueOf).findAny().get();
System.out.println("\ndouble (19.23456) converted to String = " + dblToStr1);
// 4.2 convert double to String
String dblToStr2 = Stream.of(dblTest2).map(String::valueOf).findAny().get();
System.out.println("double (258.589635) converted to String = " + dblToStr2);
// 5. float variables
float fltTest1 = 29.23456f,
fltTest2 = 158.589635f;
// 4.1 convert float to String
String fltToStr1 = Stream.of(fltTest1).map(String::valueOf).findAny().get();
System.out.println("\nfloat (29.23456f) converted to String = " + fltToStr1);
// 4.2 convert float to String
String fltToStr2 = Stream.of(fltTest2).map(String::valueOf).findAny().get();
System.out.println("float (158.589635f) converted to String = " + fltToStr2);
// 6. int variables
int intTest1 = 63,
intTest2 = 97;
// 6.1 convert int to String
String intToStr1 = Stream.of(intTest1).map(String::valueOf).findAny().get();
System.out.println("\ninteger (63) converted to String = " + intToStr1);
// 6.2 convert int to String
String intToStr2 = Stream.of(intTest2).map(String::valueOf).findAny().get();
System.out.println("integer (97) converted to String = " + intToStr2);
// 7. long variables
long longTest1 = 1912345,
longTest2 = 333444999;
// 7.1 convert long to String
String longToStr1 = Stream.of(longTest1).map(String::valueOf).findAny().get();
System.out.println("\nlong (1912345) converted to String = " + longToStr1);
// 7.2 convert long to String
String longToStr2 = Stream.of(longTest2).map(String::valueOf).findAny().get();
System.out.println("long (333444999) converted to String = " + longToStr2);
}
}
Output:
boolean (false) converted to String = false
boolean (true) converted to String = true
char 'z' converted to String = z
char 'N' converted to String = N
char[] Arrays {'B', 'e', 'n', 'c', 'h'} converted to String = Bench
char[] Arrays {'F', 'r', 'i', 'e', 'n', 'd'} converted to String = Friend
double (19.23456) converted to String = 19.23456
double (258.589635) converted to String = 258.589635
float (29.23456f) converted to String = 29.23456
float (158.589635f) converted to String = 158.58963
integer (63) converted to String = 63
integer (97) converted to String = 97
long (1912345) converted to String = 1912345
long (333444999) converted to String = 333444999
Q) Difference between int and long ?
- primitive int (integer) data-type is 32-bit
- primitive long data-type is 64-bit
Q) Difference between float and double ?
- Both are used to represent floating numbers where,
- primitive float data-type is 32-bit and always followed with suffix ‘f’
- primitive double data-type is 64-bit
Related Articles:
- Java 8 – How to get a specific character from String ?
- Java 8 – How to check whether particular word/letter/sub-string is present in the String ?
- Java 8 – How to check whether particular String endsWith specific word/letter ?
- Java 8 – How to check whether particular String startsWith specific word/letter ?
- Java 8 – How to check whether a String is empty or not ?
- Java 8 – How to get length of a String ?
- Java 8 – How to convert a String into char[] Arrays ?
- Java 8 – How to convert a String into UpperCase String ?
- Java 8 – How to convert a String into LowerCase String ?
- Java 8 – How to remove leading and trailing whitespaces in a String ?
- Java 8 – How to replace a String with another String ?
- Java 8 – How to split a String based on delimiter ?
- Java 8 – How to convert primitive data-types into String ?
- Java 8 – How to join String[] Arrays elements using different delimiter ?
- Java 8 – How to join List of String elements using different delimiter ?
- Java 8 – How to find 1st and last index of particular character/substring in a String ?
- Java 8 – How to get hashCode of a String ?
- Java 8 – How to get sub-string from a String ?
References:
- https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
- https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html
Happy Coding !!
Happy Learning !!