Java toString() Method: Usage and Examples

Rumman Ansari   Software Engineer   2024-07-04 04:02:02   6674  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

Table of Content:

Description

The method is used to get a String object representing the value of the Number Object.

If the method takes a primitive data type as an argument, then the String object representing the primitive data type value is returned.

If the method takes two arguments, then a String representation of the first argument in the radix specified by the second argument will be returned.

Syntax

Following are all the variants of this method ?

String toString()
static String toString(int i)

String Conversion is done by converting Byte, Short, Integer, Float, Character, Double, Long data types into String.

There are two variants of toString() method. They are used to get String representation of a number.

 

The other variants of these methods are Integer.toBinaryString(int i)Integer.toHexString(int i)Integer.toOctalString(int i) which will return binary, hexa-decimal, octal string representation of specified integer(i) respectively.

Parameters

Here is the detail of parameters ?

  • i ? An int for which string representation would be returned.

Return Value

  • toString() ? This returns a String object representing the value of this Integer.

  • toString(int i) ? This returns a String object representing the specified integer.

Example

public class Test { 

   public static void main(String args[]) {
      Integer x = 5;

      System.out.println(x.toString());  
      System.out.println(Integer.toString(12)); 
   }
}

This will produce the following result ?

Output

5
12

Program

import java.io.*;

class StringConversiontoString
{
    public static void main(String arg[])
    {
        int a = 10;
        float b = 20.5f;
        char c = 'd';
        double d = 19.5;
        System.out.println(a + b + c + d);
        
        String s1 = Integer.toString(a);
        String s2 = Float.toString(b);
        String s3 = Character.toString(c);
        String s4 = Double.toString(d);
        System.out.println(s1 + " " + s2+ " " + s3 + " " + s4);
        
        double cake = 245.75;
        String rate = Double.toString(cake);
        String[] r = rate.split("\\.");
        String r1 = r[0];
        String r2 = r[1];
        System.out.println("The Price of cake is " + r1 + " Rupees " + r2 + " Paise.");    
    }
}

Output

150.0
10 20.5 d 19.5
The Price of cake is 245 Rupees 75 Paise.
Press any key to continue . . .

In the program, ASCII value of d character is 100 so sum of 10, 20.5, 100, 19.5 is 150 so 150 is printed. The int, float, char, double values are converted to String at LINE A, LINE B, LINE C, LINE D and stored in s1, s2, s3, s4 variables respectively. The strings are concatenated and displayed in output. The cake rate of double value is converted into string and splits it into two parts using split method where dot(.) is present and first part is taken as rupee and second part as paise.

THINGS TO TRY

  • Apply toString method for different data types.
  • Try to apply operations on strings which are obtained by other data types
  • Try to get other string formats using toString method.