Java toString() Method

The toString() method returns the String representation of the object. However, the Java compiler internally invokes the toString() method on the object if you publish any object. So, booting the toString() system returns the requested object, which can be the state of an object, etc.

How to use Java toString ()method

This tutorial will teach us about the Java toString() method. We’ll examine the Description and programming of the toString() Java Method.

Examples:

After reading this tutorial, you will understand the concepts of the toString() Java method and be comfortable using it in your programs to get the String representation of the object.

Java ToString()

As the name suggests, the Java toString() method returns the String equivalent of the object that invokes it.

Java ToString()

Parameters

NONE

Return Value

Type: Description
A string: The content of the string.

Syntax

public static String toString()
public static String toString(int i)
public static String toString(int i, int base)

We have three variants of the Java String toString() method. All three variants return the String representation for an Integer. We will discuss all three variants in the latter part of this tutorial.

toString() with base 10 and base 2

In this programming example, we will see how the toString() Java method works. Here, we create an object of base 10 and try to get the String representation of that object in base 10 and base 2.

public class TechVidvan_toString {
Public static void main(String[] args) {
//in base 10
Integer obj = new Integer(10);      
//used toString() method for String equivalent of the Integer     
String str1 = obj.toString();     
String str2 = obj.toString(80);       
//in base 2
String str3 = obj.toString(658,2);        
// Printed the value of all the String variables
System.out.println(str1);        
System.out.println(str2);        
System.out.println(str3);
}
}

Output:
10
80
1010010010

toString() With Decimal

This example shows how the Java toString() method works with decimal or float variables.

Here, we have created an object of base 10. Then, we have passed a decimal value (in the previous program, we passed an integer value 80, which returned 80 as an output).

This will throw a compilation error with the message “ The system toString( int) in the type Integer isn’t applicable for the arguments( double).” That’s why we have to use the Double class toString() system to get the String representation of pier/ double, which we will bandy in the coming illustration.

Public class TechVidvan_toString {
Public static void main(String[] args) {
//in base 10
Integer obj =newInteger(10);     
/*
*  The system toString( int) in the type Integer is
* not applicable for the arguments( pier or double)
*/
String str1 = obj.toString(69.47);
( str1);
}

Output:
Unresolved compilation problem

ToString() With Double

As an outgrowth of the former illustration, we will bandy getting the String representation of pier/ double variables in this illustration.

Public class TechVidvan_toString {   
Public static void main(String[] args) [  
// Initialized a double variable with the value146.39
Double dbl =146.39d;
// Getting the String representation of the double variable
String str = Double.toString(dbl);
System.out.println(str);
}
}

Output:
146.39

Scenarios

Scenario 1: Illustrating Java toString(int num, int base value)

Explanation: Then, we will illustrate the Java toString( int number, int base value) and try to get the String representation of the different cases. In this script, we’ve created an object in base 10. We’ve also used Java toString( int num, int base value) to try the base values 2, 8, 16, and 10. Later, we’ve published the String representation of each of these base values for the specified integer value.

Public class TechVidvan_toString {
Public static void main(String[] args) {
// in base 10
Integer obj = new Integer(10);
// in base 2
String str = obj.toString(9876,2);
// It returns a string representation    
System.out.println("String Value of 9876 in base 2 = "+  str);
System.out.println();    
// in base 8
str = obj.toString(350,8);
// It returns a string representation        
System.out.println("String Value of 350 in base 8 = "+ str);  
System.out.println();      
// in base 16 
str = obj.toString(470,16);
// It returns a string representation
 System.out.println(" String Value of 470 in base 16 = " str);
System.out.println();   
// in base 10
str = obj.toString(451,10);     
// It returns a string representation
System.out.println(" String Value of 451 in base 10 = " str);
}
}

Output
String value of 9876 in base 2 = 10011010010100
String value of 350 in base 8 = 536
String value of 470 in base 16 = 1d6
String value of 451 in base 10 = 451

Script 2: We will try Java toString on the negative Integers in this script.

Explanation: Then, we used the same program( as in script 1). The only difference is the use of a negative number. We didn’t change the base value, but the Integer values have been changed into negative figures.

As we examined this program, we learned that the Java toString() method works well with negative figures.

Public class TechVidvan_toString {
Public static void main(String[] args) {
// in base 10
Integer obj = new Integer(10);
// in base 2
String str = obj.toString(9876,2);
// It returns a string representation   
System.out.println(" String Value of 9876 in base 2 = " str); 
 System.out.println();       
// in base 8
str = obj.toString(350,8);
// It returns a string representation
System.out.println(" String Value of 350 in base 8 = " str);
System.out.println();
// in base 16
str = obj.toString(470,16);
// It returns a string representation
System.out.println(" String Value of 470 in base 16 = " str);
System.out.println();
// in base 10
str = obj.toString(451,10);
// It returns a string representation 
System.out.println(" String Value of 451 in base 10 = " str);
}
}

Output
String value of 9876 in base 2 = -10011010010100
String value of 350 in base 8 = -536
String value of 470 in base 16 = -1d6
String value of 451 in base 10 = -451

Conclusion

In this tutorial, we thoroughly understood the Java toString() method. The programming exemplifications for each base value were applicable to learning about converting an Integer into a String representation for a particular base value.

This tutorial was explained using different scenarios to help us better understand. We also learned about the negative and decimal/floating-point number behavior when used in the toString() method. We explored the frequently asked questions, which helped us understand this method clearly.