
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get String Representation of Numbers Using toString in Java
String representation of numbers is nothing but converting numeric values into their corresponding string values using methods like toString(). In Java, this can be done by calling the toString() method on wrapper classes such as Integer, Float, and Double.
The toString() method is an important method of Object class and it can be used to return the string or textual representation of an object. The object class's toString() method returns a string as the name of the specified object's class which is followed by ?@' sign and the hashcode of the object (java.lang.String;@36f72f09)
String Representation Using toString() Method
To get the string representation of numbers, we can use the toString() method. It is useful when the string consists of numbers taken from different variables. In such cases, the numbers can be converted to strings and concatenated to create a combined or formatted string.
Following is the syntax of the toString() method -
ClassName.toString(value);
Here,
- className: The name of the class on which the method is called, such as Integer, Double, Float, etc.
- value: The value parameter refers to the actual data (or literal) that you pass into a method when calling it.
Example 1: String Representation of Integer Values
The following program uses the toString() method on the Integer wrapper classes to create a string representation of the given numbers 50 and 99999:
public class ToStringMethodTest { public static void main(String args[]) { int num1 = 50; Integer num2 = 99999; //converting numbers to string format by toString() String str_int1 = Integer.toString(num1); String str_int2 = num2.toString(); System.out.println("int to string = " + str_int1); System.out.println("Integer to string = " + str_int2); } }
Output
The above program produces the following output:
int to string = 50 Integer to string = 99999
Example 2: String Representation of Decimal Values
The following program uses the toString() method on the Float and Double wrapper classes to create a string representation of the given numbers 80.55f and 3256522.44d:
public class ToStringMethodTest { public static void main(String args[]) { float flt1 = 50.75f; Float flt2 = 80.55f; double dbl1 = 3256522.44d; //converting numbers to string format by toString() String str_flt1 = Float.toString(flt1); String str_flt2 = flt2.toString(); String str_dbl1 = Double.toString(dbl1); System.out.println("float to string = " + str_flt1); System.out.println("Float to string = " + str_flt2); System.out.println("double to string = " + str_dbl1); } }
Following is the output of the above program -
float to string = 50.75 Float to string = 80.55 double to string = 3256522.44