Java toString() method with Examples
We can use this method to get the string representation of an object.
public String toString()
{
return getClass().getName()+"@"+Integer.toHexString(hashCode());
}
public class Student {
int roll;
String name;
int age;
String college;
String dob;
String address;
Student(int roll, String name, int age, String college, String dob, String address) {
this.roll = roll;
this.name = name;
this.age = age;
this.college = college;
this.dob = dob;
this.address = address;
}
public static void main(String[] args)
{
Student student =
new Student(10011,"Sara K", 21, "COEP", "12-01-2021", "Pune");
System.out.println(student);
System.out.println(student.toString());
}
}
output:
Student@2a139a55
Student@2a139a55
In the above program Object class toString() method got executed which is implemented as follows.
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
here getClass().getName() => classname@hexa_decimal_String_representation_of_hashCode
To provide our own String representation we have to override toString() method in our class.
public String toString(){
return name+"........"+rollno;
}
....
public String toString()
{
return " roll:" + roll +" name:" + name+ " age: " + age + " college: " + college + " dob: " + dob + " address: " + address;
}
public class Student {
int roll;
String name;
int age;
String college;
String dob;
String address;
Student(int roll, String name, int age, String college, String dob, String address) {
this.roll = roll;
this.name = name;
this.age = age;
this.college = college;
this.dob = dob;
this.address = address;
}
public String toString() {
return " roll:" + roll + " name:" + name + " age:" + age + " college:" + college + " dob:" + dob + " address: " + address;
}
public static void Student(String[] args) {
Student student = new Student(10011, "Sara K", 21, "COEP", "12-01-2021", "Pune");
System.out.println(student);
}
}
output:
roll:10011 name:Sara K age:21 college:COEP dob:12-01-2021 address: Pune
In String class, StringBuffer, StringBuffer, wrapper classes and in all collection classes toString() method is overridden for meaningful string representation. Hence in our classes also highly recommended overriding the toString() method.
public class ToStringTest {
public String toString() {
return "Hello World";
}
public static void main(String[] args) {
Integer i = new Integer(101);
String s = new String("Sara");
float f = new Float(11.0);
ToStringTest toStringTest = new ToStringTest();
System.out.println(i);
System.out.println(s);
System.out.println(f);
System.out.println(toStringTest);
}
}
output:
$javac ToStringTest.java
$java -Xmx128M -Xms16M ToStringTest
101
Sara
11.0
Hello World
Case:1
We can use the toString() method to convert wrapper object (or) primitive to String.
public String toString();
1. Every wrapper class (including Character class) contains the above toString() method to convert the wrap String.
2. It is the overriding version of the Object class toString() method.
3. Whenever we are trying to print wrapper object reference internally this toString() method only executed.
Example:
class WrapperClassDemo {
public static void main(String[] args) {
Integer i=Integer.valueOf("10");
System.out.println(i);//10
System.out.println(i.toString());//10
}
}
Case:2
Every wrapper class contains a static toString() method to convert primitive to String.
public static String toString(primitive p);
class WrapperClassDemo {
public static void main(String[] args)
String s1=Integer.toString(10);
String s2=Boolean.toString(true);
String s3=Character.toString('a');
System.out.println(s1); //10
System.out.println(s2); //true
System.out.println(s3); //a
}
}
Integer and Long classes contain the following static toString() method to convert the primitive to specific String form.
public static String toString(primitive p, int radix); Example:
class WrapperClassDemo {
public static void main(String[] args)
String s1=Integer.toString(7,2);
String s2=Integer.toString(17,2);
System.out.println(s1);//111
System.out.println(s2);//10001
}
}
Dancing between String, wrapper object, and primitive
Post/Questions related to Java toString() method
Java adding New Line in a StringJava String split() method Examples
String lastIndexOf() method examples in Java
String indexOf() method example in Java
String array in Java
How to check if an enum contains String
Java Object Class and Methods with Examples
Override equals method in java
Utility methods in Java
In this article, we have seen the Java toString() method with Examples.
0 Comments
Post a Comment