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
Answer from Salmaan on Stack Overflow
Top answer
1 of 12
52

I'm afraid your map contains something other than String objects. If you call toString() on a String object, you obtain the string itself.

What you get [Ljava.lang.String indicates you might have a String array.

2 of 12
14

Might not be so related to the issue above. However if you are looking for a way to serialize Java object as string, this could come in hand

package pt.iol.security;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import org.apache.commons.codec.binary.Base64;

public class ObjectUtil {

    static final Base64 base64 = new Base64();

    public static String serializeObjectToString(Object object) throws IOException {
        try (
                ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
                GZIPOutputStream gzipOutputStream = new GZIPOutputStream(arrayOutputStream);
                ObjectOutputStream objectOutputStream = new ObjectOutputStream(gzipOutputStream);) {
            objectOutputStream.writeObject(object);
            objectOutputStream.flush();
            return new String(base64.encode(arrayOutputStream.toByteArray()));
        }
    }

    public static Object deserializeObjectFromString(String objectString) throws IOException, ClassNotFoundException {
        try (
                ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(base64.decode(objectString));
                GZIPInputStream gzipInputStream = new GZIPInputStream(arrayInputStream);
                ObjectInputStream objectInputStream = new ObjectInputStream(gzipInputStream)) {
            return objectInputStream.readObject();
        }
    }
}
Discussions

java - How get the value from a Object of class through a String? - Stack Overflow
public class Simulator{ static ... can get values by $t1.getValue() } } ... You could use a Map. ... Hint: you never use the $ character in variable names. $ is (by convention) for generated code only! ... It seems that you want to "identify" objects by some string based name. If so, the appropriate data structure in Java is a ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - Get instanced object by String - Stack Overflow
Class c = this.getClass(); // or ... ... = value.split("_"); ... But as a comment points out, if you are really trying to implement an associative array, there are better ways of doing this in Java; e.g. using a Map class. ... Save this answer. ... Show activity on this post. You might have to rephrase the question. If you just want to get the "aaaa" and "bbb" strings from the initial ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to get string value from a Java field via reflection? - Stack Overflow
If classToInspect is a Widget, ... and the value of that Widget#fizz instance is "buzz", then I want to get the buzz string into an actual String instance. ... @TicketMonster A field only makes sense either as a static field of a Class or as an instance field. You therefore have to specify which instance it is (or null when it's static) with the Field#get(Object) ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
network programming - How to turn a String value into a object reference in Java? - Stack Overflow
I am currently working on a very simple client-server-connection. The client and the server are connected through sockets and communicate by using streams. Now I want the client to be able to invoke More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java string โ€บ get string value from a java field via reflection
Get String Value From a Java Field via Reflection | Baeldung
August 26, 2025 - If itโ€™s found, we can call get(example) to retrieve its value from the instance. Since the get method returns an Object, we cast it to a String before comparing it in our test. In most applications, fields are private to follow the principles of encapsulation. Reflection can still access these fields, but we must explicitly override Java...
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ java โ€บ java convert object to string
How to Convert Object to String in Java | Delft Stack
February 2, 2024 - public class SimpleTesting { public ... ... This example explains how to convert a user-defined object to a string using the toString() method....
Find elsewhere
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 76567190 โ€บ how-to-turn-a-string-value-into-a-object-reference-in-java
network programming - How to turn a String value into a object reference in Java? - Stack Overflow
I already found a partial solution for methodName by using this: java.lang.reflect.Method method = myObject.getClass().getMethod(methodName); method.invoke(myObject); This would effectively turn the String value of methodName into a reference ...
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 61997035 โ€บ how-to-get-an-object-using-a-string-in-java
How to get an object using a string in Java - Stack Overflow
May 25, 2020 - you can use a map which contains a string-object mapping with object name as key and object as value ... Please explain what objectName means and what getObject needs to do ...
๐ŸŒ
Real's HowTo
rgagnon.com โ€บ javadetails โ€บ java-get-fields-and-values-from-an-object.html
Get fields and values from an Object - Real's Java How-to
public class Dummy { public String value1 = "foo"; public int value2 = 42; public Integer value3 = new Integer(43); private String value4 = "bar"; } We want to list the fields and the values of an instance. import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.HashMap; import java.util.Map; public class ObjectUtils { private ObjectUtils() {} public static Map<String, Object> getFieldNamesAndValues(final Object obj, boolean publicOnly) throws IllegalArgumentException,IllegalAccessException { Class<? extends Object> c1 = obj.getClass(); Map<String, Object> map = new
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java string โ€บ how to convert an object to string
How to Convert an Object to String | Baeldung
January 5, 2024 - The most straightforward way to convert an object to a string is by using the toString() method. toString() is a built-in method provided by the Object class, which is the root class for all Java objects.
๐ŸŒ
W3Schools
w3schools.com โ€บ JAVA โ€บ ref_string_valueof.asp
Java String valueOf() Method
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, Python, PHP, Bootstrap, Java, XML and more.
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2022 โ€บ 11 โ€บ java-string-to-object-conversion
Java String to Object Conversion
class JavaExample { public static void main(String[] args) { String str = "BeginnersBook"; Object obj = str; //assigning string to object //the class of the value stored in the Object obj System.out.println("Class of the value stored in obj: " +obj.getClass().getName()); System.out.println("The ...
๐ŸŒ
Reddit
reddit.com โ€บ r/javahelp โ€บ how to convert object to string ?
r/javahelp on Reddit: How to convert object to string ?
October 7, 2020 -

Hello guys, I would like to know the best way to convert object to string with commas separating the year month & day in format (yyyy,mm,dd) . I am trying to do so for the program to place information into a data file. The conversion follows the code line below. I seek your kind assistance and many thanks.

MyDate MyDate = cannedFood.getexpiryDate(); // to convert object MyDate into string

๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ java โ€บ lang โ€บ object_tostring.htm
Java Object toString() Method
The following example shows the usage of java.lang.Object.toString() method. In ths example, we've created an Integer instance with value of 50. Now using toString(), a string representation of the object is printed. package com.tutorialspoint; ...
๐ŸŒ
Oracle
forums.oracle.com โ€บ ords โ€บ apexds โ€บ post โ€บ how-to-get-the-values-from-object-4162
how to get the values from object ? - Oracle Forums
November 10, 2008 - hi everybody, i am experimenting with oops concept. first have a look at these coding and then tell me this. what do i have to do to get the value of an object created. i want to use the get method ...