The Object.toString()
method in Java is used to return a string representation of an object.
Table of Contents
- Introduction
toString()
Method Syntax- Examples
- Default
toString()
- Overriding
toString()
- Using
toString()
in Collections
- Default
- Real-World Use Case
- Conclusion
Introduction
The Object.toString()
method is a member of the Object
class in Java. It returns a string representation of the object. By default, the toString()
method returns a string that consists of the class name followed by the "@" character and the object's hash code in hexadecimal. However, this method can be overridden to provide a more meaningful string representation of an object.
toString()() Method Syntax
The syntax for the toString()
method is as follows:
public String toString()
The method returns a string representation of the object.
Examples
Default toString()
The default implementation of the toString()
method in the Object
class returns the class name followed by the "@" character and the object's hash code in hexadecimal.
Example
public class DefaultToStringExample {
public static void main(String[] args) {
Object obj = new Object();
System.out.println(obj.toString());
}
}
Output:
java.lang.Object@15db9742
Overriding toString()
To provide a meaningful string representation of an object, you can override the toString()
method in your class.
Example
class Person {
String name;
int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person{name='" + name + "', age=" + age + "}";
}
}
public class CustomToStringExample {
public static void main(String[] args) {
Person person = new Person("Ramesh", 30);
System.out.println(person.toString());
}
}
Output:
Person{name='Ramesh', age=30}
Using toString()
in Collections
When objects are stored in collections, the toString()
method is used to provide a string representation of the objects in the collection.
Example
import java.util.ArrayList;
import java.util.List;
public class CollectionToStringExample {
public static void main(String[] args) {
List<Person> people = new ArrayList<>();
people.add(new Person("Ramesh", 30));
people.add(new Person("Suresh", 25));
System.out.println(people.toString());
}
}
Output:
[Person{name='Ramesh', age=30}, Person{name='Suresh', age=25}]
Real-World Use Case
Logging and Debugging
In real-world scenarios, the toString()
method is often overridden to provide a useful string representation of an object for logging and debugging purposes.
Example
class Product {
String name;
double price;
Product(String name, double price) {
this.name = name;
this.price = price;
}
@Override
public String toString() {
return "Product{name='" + name + "', price=" + price + "}";
}
}
public class LoggingExample {
public static void main(String[] args) {
Product product = new Product("Laptop", 800.0);
System.out.println("Logging product: " + product);
}
}
Output:
Logging product: Product{name='Laptop', price=800.0}
Conclusion
The Object.toString()
method in Java is a fundamental method for providing a string representation of an object. By understanding how to use and override this method, you can ensure that your objects have meaningful and useful string representations. Whether you are using the default implementation, overriding the method for custom representations, or using it in real-world scenarios like logging and debugging, the toString()
method provides a reliable way to convert objects to strings in Java applications.
Comments
Post a Comment
Leave Comment