Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to convert any primitive data type to string

Tags:

I am using the following approach to convert any primitive data type to string

Example

int i = 5;// String convertToString = ""+i;// convert any primitive data type to string 

To convert int data type to string i can use Integer.toString() but what is the better way to convert any type of primitive data (not only int) types to string

like image 305
Sunil Kumar Sahoo Avatar asked Dec 26 '11 11:12

Sunil Kumar Sahoo


People also ask

Which method is used to convert data into strings?

toString() The Integer. toString() method converts int to String. The toString() is the static method of Integer class.

How do you convert a primitive data type into an object?

Converting a primitive value (an int, for example) into an object of the corresponding wrapper class (Integer) is called autoboxing. The Java compiler applies autoboxing when a primitive value is: Passed as a parameter to a method that expects an object of the corresponding wrapper class.

What is primitive data type conversion?

Casting between primitive Java types. Changing a value from one data type to a variable of another type is known as data type conversion. There are two types of casting, Primitive Type Casting. Reference Type Casting.


1 Answers

Use String.valueOf() method.

int no = 2;  String strValue = String.valueOf( no ); 
like image 129
KV Prajapati Avatar answered Oct 18 '22 11:10

KV Prajapati