Saturday, December 14, 2024
HomeEducationWhat is toString() Method in Java?

What is toString() Method in Java?

tostring java

When programming a software solution, the developers create several user-defined classes that implement the code for their software solution. To assist the developers, all languages provide standard libraries that are sub-divided into packages. These packages may have built-in interfaces, classes, and methods. The classes and methods usually have pre-defined functionality that reduces the developers’ workload while expanding the language’s capabilities. 

Java also has a lot of standard libraries with packages that have a collection of user interfaces, classes, and methods. The classes in a package provide solutions within a common domain. For example, the java.applet has classes and methods to be used for applets, and the java.io has classes and methods for system input and output through data streams, and so on.

Table of Contents

What is toString?

The toString method is a built-in method in the Object class in java. Object class is present in java.lang package, and it’s the parent class of all classes. Every class in Java inherits the default implementation of the toString method. 

Functionality and Return Values of toString method

The functionality of the toString method is to return a String representation of the object on which it’s called. The method describes the object in String or converts a numeric value into a String. 

Parameters and Syntax
The generic form of the method is given below. 

String toString()

The above form shows that the return type of the method is String. The method can also be used with objects and other data types, as shown below.

static String toString(float num)
static String toString(double num)
static String toString(byte num)
static String toString(boolean bool)

Another variation of the method accepts 2 arguments – a number and the base in which the number’s String representation is required. An example is shown in the next section to see how this works. Its syntax is:   

static String toString(int num, int radix)

How to use the toString() method

The below examples show how the toString method can be used.

Example 1:

public class Player{
public static void main(String args[]){
Player player= new Player();
Integer jersey=7;
System.out.println(player.toString()); }
}

For example1, Player@7a81197d is printed to the console. Since ‘player’ is an object, the toString method’s default implementation gives a String that describes the class name + ‘@’+ hashcode value of the object on which the method is called.

Understanding the problem without the toString() method

When the toString method is not used explicitly in the println statement, it gets called by default, and println outputs the String representation of the object.  Let’s try that in our example:

Example2:

public class Player{
public static void main(String args[]){
Player player= new Player();
Integer jersey=7;
System.out.println(player); } }

The output is Player@7a81197d.

The result is the same as in the previous example, showing that the toString method is called by default when println outputs an object. In both the cases above, the result doesn’t serve any purpose, so we will override the default toString method and change how it works. 

Example3:

class Player1{
String name;
int jersey;
String club; //Override the toString method public String toString() { return "Player{"+"name="+name+"" +",Jersey="+jersey+","+"Club="+club+"}"; } Player1(int jersey, String name, String club){
this.jersey=jersey;
this.name=name;
this.club=club;
} public static void main(String args[]) { Player1 player = new Player1(10,"Messi","Paris Saint-Germain"); System.out.println(player.toString()); }
}

The result of the above examples is:

Player{name=Messi,Jersey=10,Club=Paris Saint-Germain}

As seen in the above code, the toString method has code that overrides the default implementation. When toString is called in the println statement, the new code of the toString method returns a value that gets printed. Even if the toString method is not called explicitly in println, it gets called by default, and the same output gets printed.

The below example shows how the toString method can be used on an integer whose String value is required in a different base. Here 2400 is converted into base 8, and the String value is displayed.

Example 4:

public class BaseChange{
public static void main(String args[]){ System.out.println(Integer.toString(2400,8)); }
}

The output of the above code is 4540. 

Advantage of Java toString() method

The toString method is in the Object class, the parent class in java, so it’s available in every class in java by default. The method can be used on any object required to be represented in a String format. This can help debug when you need the details of an object. Sometimes you may want to override the method to implement it the way you want. Either way, it’s advantageous when you are programming in Java.

Frequently Asked Questions (FAQs)

How do we call the toString () method?

The toString() method is present in the Object class; whenever it’s called on an object, it returns the String representation of the object. When there’s a need to describe an object in a simple String format, the toString method is applicable. The method may return just the name of the object, or it can be overridden to include more information about the object. This can be helpful n debugging or for any other purpose. Numeric data types, bytes, URLs, etc., can also be represented as String.  

What is toString (), and why do we need it?

By default, the toString() method is called by println, but the method can also be called explicitly on any object. The method can be called on an object, like this – object.toString(), or a numeric value can be passed to the method as an argument, like this – Integer.toString(10). 

Where is the toString method in Java?

The toString method is in the Object class in java. Since it’s the parent class of all classes, they inherit the default implementation of the toString method.

Does Java automatically use toString? If so, why is toString automatically called?

The toString method is automatically called when something is printed using println. It also gets called when an object is concatenated with a String and can be called explicitly when required. 

In the statement “System.out.println”, println is a public method of the PrintStream class. The implementation of the method println makes a call to String.valueOf(Object) method. Within the valueOf method, toString is called on the object passed as an argument to the valueOf method. Thus, toString method gets called automatically.  

How do you create a toString method in Java?

The toString method already exists in all classes in Java as it’s there in the parent class. So, there’s no need to create it, but you can override the method as per your requirement. 

Source: GreatLearning Blog

RELATED ARTICLES
- Advertisment -

Most Popular

Recent Comments