Junior — Middle
How many parameters does the emplace_back method for the std::vector container accept?
sobes.tech AI
Answer from AI
The toString() method in Java is used to obtain a string representation of an object. By default, it is defined in the Object class and returns a string containing the class name and hash code, which is usually not informative.
Overriding toString() in your classes allows you to output the object's contents in a readable format, which is useful for debugging and logging.
Example:
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person{name='" + name + "', age=" + age + "}";
}
}
// Usage:
Person p = new Person("Ivan", 30);
System.out.println(p); // Will print: Person{name='Ivan', age=30}
Thus, toString() helps get a readable description of the object.