Java.lang.String.toString() Method

Advertisements


Description

The java.lang.String.toString() method represent the result in textual format and returns the string itself.

Declaration

Following is the declaration for java.lang.String.toString() method

public String toString()

Parameters

  • NA

Return Value

This method returns the string itself.

Exception

  • NA

Example

The following example shows the usage of java.lang.String.toString() method.

package com.tutorialspoint;

import java.lang.*;

public class StringDemo {

  public static void main(String[] args) {
  
    // converts integer value in String representation
    int val1 = 325;
    String str1 = Integer.toString(val1);
    System.out.println("String representation of integer value = " + str1);
    
    // converts double value in String representation
    double val2 = 876.23;
    String str2 = Double.toString(val2);
    System.out.println("String representation of double value = " + str2);   
  }
}

Let us compile and run the above program, this will produce the following result:

String representation of integer value = 325
String representation of double value = 876.23



Advertisements
Advertisements