Java - Integer Class toString() Method

Integer class toString() method: Here, we are going to learn about the toString() method of Integer class with its syntax and example. By Preeti Jain Last updated : March 18, 2024

Syntax

public String toString();
public static String toString(int value);
public static String toString(int value, int radix's);

Integer class toString() method

  • toString() method is available in java.lang package.
  • toString() method is used to represent String denoted by this Integer object.
  • toString(int value) method is used to represent String denoted by the given argument of int type.
  • toString(int value, int radix's) method is used to represent String of the given argument of int type in the radix's given by the second argument.
  • These methods don't throw an exception at the time of String representation.
  • These are non-static methods, they are accessible with the class object only and if we try to access the method with the class name then we will get an error.

Parameters

  • In the first case toString(), we don't pass any parameter or value.
  • In the second case toString(int value), we pass only one parameter of the int type it represents the int value to be converted.
  • In the third case toString(int value, int radix's), we pass two parameters of int type it represents the int value to be converted and the parameter radix's represents the radix to be used in String denotation.

Return Value

In the first case, the return type of this method is String - it returns the String representation of this Integer object.

In the second case, the return type of this method is String - it returns the String representation of the given argument is of int type.

In the third case, the return type of this method is String - it returns the String representation of the given argument is of int type in the given radix.

Example

// Java program to demonstrate the example 
// of toString () method of Integer class

public class ToStringOfIntegerClass {
    public static void main(String[] args) {
        // Object initialization
        Integer ob1 = new Integer(100);
        Integer ob2 = new Integer(200);

        // Display ob1,ob2 values
        System.out.println("ob1:" + ob1);
        System.out.println("ob2:" + ob2);

        // It represents the string of this Integer object
        String value1 = ob1.toString();

        // It represents the string of the given integer parameter
        String value2 = Integer.toString(ob2);

        // It represents the string of the given integer parameter with radix 20
        String value3 = Integer.toString(ob2, 20);

        // Display result values
        System.out.println("ob1.toString(): " + value1);
        System.out.println("Integer.toString(ob2): " + value2);
        System.out.println("Integer.toString(ob2,20): " + value3);
    }
}

Output

ob1:100
ob2:200
ob1.toString(): 100
Integer.toString(ob2): 200
Integer.toString(ob2,20): a0

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.