Java Integer.toString() - Syntax & Examples

[Pages:6]Java Integer.toString() ? Syntax & Examples

Java Integer.toString() ? Examples

In this tutorial, we will learn about Java Integer.toString(), Integer.toString(int) and Integer.toString(int, radix) methods, and learn how to use these method to get the string representation of given integer, with the help of examples.

toString()

Integer.toString() returns a String object representing this Integer's value.

Syntax

The syntax of toString() method is

Integer.toString()

Returns

The method returns value of type String.

Example 1 ? toString()

In this example, we will take an integer object and get its string value using Integer.toString() method. Java Program

public class Example { public static void main(String[] args){ Integer integer = 5; String result = integer.toString(); System.out.println("Result of toString() = " + result); }

} Output

Result of toString() = 5

toString(int i)

Integer.toString(i) returns a String object representing the specified integer i .

Syntax

The syntax of toString() method with int as parameter is

Integer.toString(int i) where

Parameter Description

i

The integer whose string representation has to be found.

Returns The method returns value of type String.

Example 2 ? toString(int i)

In this example, we will take an int i and get its string value by passing this int value as argument to Integer.toString() method.

Java Program

public class Example { public static void main(String[] args){ int i = 5; String result = Integer.toString(i); System.out.println("Result of toString(" + i + ") = " + result); }

}

Output

toString(int i)

toString(int i, int radix)

Integer.toString(i, radix) returns a string representation of the first argument i in the radix specified by the second argument radix .

Syntax

The syntax of toString() method with integer and radix as parameters is

Integer.toString(int i, int radix) where

Parameter Description

i

The integer whose string representation has to be found.

radix

The radix value.

Returns The method returns value of type String.

Example 3 ? toString(i, radix)

In this example, we will string representation of an integer i = 24 in the radix specified by the specified radix = 3 .

Java Program

public class Example { public static void main(String[] args){ int i = 24; int radix = 3; String result = Integer.toString(i, radix); System.out.println("Result of toString(" + i + ", " + radix + ") = " + result); }

}

Output Result of toString(24, 3) = 220

Example 4 ? radix = 16 : Hexadecimal

In this example, we will string representation of an integer i = 24 in the radix specified by the specified radix = 16 . The value of radix is 16 which means we will get hexadecimal representation.

Java Program

public class Example { public static void main(String[] args){ int i = 24; int radix = 16; String result = Integer.toString(i, radix); System.out.println("Result of toString(" + i + ", " + radix + ") = " + result); }

}

Output

Result of toString(24, 16) = 18

Conclusion

In this Java Tutorial, we have learnt the syntax of Java Integer.toString() method, and also how to use this method with the help of Java example programs.

Java Integer Java int Datatype Java Integer Class Java Integer - bitCount() Java Integer - byteValue() Java Integer - compare() Java Integer - compareTo() Java Integer - compareUnsigned() Java Integer - decode() Java Integer - divideUnsigned() Java Integer - doubleValue()

Java Integer - equals() Java Integer - floatValue() Java Integer - getInteger() Java Integer - hashCode() Java Integer - highestOneBit() Java Integer - intValue() Java Integer - longValue() Java Integer - lowestOneBit() Java Integer - max() Java Integer - min() Java Integer - numberOfLeadingZeros() Java Integer - numberOfTrailingZeros() Java Integer - parseInt() Java Integer - parseUnsignedInt() Java Integer - remainderUnsigned() Java Integer - reverse() Java Integer - reverseBytes() Java Integer - rotateLeft() Java Integer - rotateRight() Java Integer - shortValue() Java Integer - signum() Java Integer - sum() Java Integer - toBinaryString() Java Integer - toHexString() Java Integer - toOctalString() Java Integer - toString() Java Integer - toUnsignedLong() Java Integer - toUnsignedString() Java Integer - valueOf()

Java Tutorial Java Tutorial Java Introduction

Java Introduction Java Installation IDEs for Java Development Java Datatypes Java Variable Types Java Operators Java Decision Making Java Loops Java Array Java OOPs Java String Java Exception Handling Java File Operations Java Date & Time Java MySQL Java Random Java Math Java Integer

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download