Java Convert Object to StringWe can convert Object to String in java using toString() method of Object class or String.valueOf(object) method. You can convert any object to String in java whether it is user-defined class, StringBuilder, StringBuffer or anything else. Here, we are going to see two examples of converting Object into String. In the first example, we are going to convert Emp class object into String which is an user-defined class. In second example, we are going to convert StringBuilder to String. Java Object to String Example: Converting User-defined classLet's see the simple code to convert String to Object in java. Test it NowOutput: Emp@2a139a55 Emp@2a139a55 As you can see above, a reference id of Emp class is printed on the console. Java Object to String Example: Converting StringBuilderLet's see the simple code to convert StringBuilder object to String in java. Test it NowOutput: String is: hello Reverse String is: olleh Now you can write the code to check the palindrome string. Test it NowOutput: Palindrome String So, you can convert any Object to String in java using toString() or String.valueOf(object) methods. Next TopicJava int to long |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India