How to convert any Object to String?

Hemant Koli picture Hemant Koli · Aug 6, 2015 · Viewed 212.3k times · Source

Here is my code:

for (String toEmail : toEmailList)          
{
    Log.i("GMail","toEmail: "+toEmail);
    emailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));      
}

Please give me some suggestion about this.

Answer

Salmaan picture Salmaan · Aug 6, 2015

To convert any object to string there are several methods in Java

String convertedToString = String.valueOf(Object);  //method 1

String convertedToString = "" + Object;   //method 2

String convertedToString = Object.toString();  //method 3

I would prefer the first and third

EDIT
If working in kotlin, the official android language

val number: Int = 12345
String convertAndAppendToString = "number = $number"   //method 1

String convertObjectMemberToString = "number = ${Object.number}" //method 2

String convertedToString = Object.toString()  //method 3