How to Convert Primitive data into Java String


The example aims to how to convert various primitive data types into String by String valueOf method in Java.

Source Code

package com.beginner.examples;

public class JavaStringValueOfExample {

	public static void main(String[] args) {
		
		int i=93;
		float f = 93.0f;
		long l = 93;
		double d = 93.0d;
		char c = 'c';
		boolean b = true;
		Object o = new String("Hello World");
		
		/* convert int to String. */
		System.out.println("int convert to String : " + String.valueOf(i) );
		/* convert float to String. */
		System.out.println("float convert to String : " + String.valueOf(f) );
		/* convert long to String. */
		System.out.println("long convert to String : " + String.valueOf(l) );
		/* convert double to String. */
		System.out.println("double convert to String : " + String.valueOf(d) );
		/* convert char to String. */
		System.out.println("char convert to String : " + String.valueOf(c) );
		/* convert boolean to String. */
		System.out.println("boolean convert to String : " + String.valueOf(b) );
		/* convert Object to String. */
		System.out.println("Object convert to String : " + String.valueOf(o) );

	}

}

Output:

int convert to String : 93
float convert to String : 93.0
long convert to String : 93
double convert to String : 93.0
char convert to String : c
boolean convert to String : true
Object convert to String : Hello World
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments