Simple way to convert integer to string in java

In this page, we will discuss about simple way to convert integer to string in java. There are several ways to perform integer to string conversion. Integer class have static methods.We can use built in functions of string.

Simple way to convert integer to string in java

Internal implementation:-

Class String

java.lang.Object

java.lang.String

All Implemented Interfaces:

SerializableCharSequenceComparable<String>

 

  • The valueOf() method

  •  The toString() method

  •  Convert integer to string using Stringbuffer and Stringbuilder

 

  1.  The valueOf() method:-

This is simple way to convert integer to string in java. String.valueOf() method return string representation of integer argument.

 

Internal implementation :-

public static String valueOf(Object obj) {

return (obj == null) ? “null” : obj.toString();

}

String.valueOf()method overloaded different numeric data type.

public static String valueOf(char[] c)

public static String valueOf(char c)

public static String valueOf(long l)

public static String valueOf(float f)

public static String valueOf(int i)

public static String valueOf(boolean b)

public static String valueOf(Object o)

public static String valueOf(double d)

Example:-

package com.javatechexpert;

public class Convertintrostring {

public static void main(String[] args) {

int i = 9;

char chr[] = { ‘j’, ‘o’, ‘h’, ‘n’ };

short sh = 2;

double d = 9.2d;

long l = 2L;

float f = 1.9f;

boolean b1 = true;

byte b2 = 10;

 

Convertintrostring obj = new Convertintrostring();

String s1 = String.valueOf(i);

String s2 = String.valueOf(chr);

String s3 = String.valueOf(sh);

String s4 = String.valueOf(d);

String s5 = String.valueOf(l);

String s6 = String.valueOf(f);

String s7 = String.valueOf(b1);

String s8 = String.valueOf(b2);

String s9 = String.valueOf(obj);

System.out.println(s1);

System.out.println(s2);

System.out.println(s3);

System.out.println(s4);

System.out.println(s5);

System.out.println(s6);

System.out.println(s7);

System.out.println(s8);

System.out.println(s9);

}

}

Output :-

9

java

2

9.2

2

1.9

true

10

com.javatechexpert.Convertintrostring@7852e922

 

  1. The toString() method:-

Integer.toString() method return string representation of integer value. Let us now see an example.

package com.javatechexpert;

public class Convertintrostring {

public static void main(String[] args) {

Integer i = new Integer(50);

// returns a string representation of the integer value

String s1 = i.toString();

System.out.println(“Value = ” + s1);

}

}

Output:-

Value = 50

 

3.  Convert Integer to String  using Stringbuffer and Stringbuilder:-

We create object of Stringbuffer class and Stringbuilder and call append() method by passing integer argument.

Example:-

package com.javatechexpert;

public class Convertintrostring {

public static void main(String[] args) {

Integer i = new Integer(10);
StringBuilder sb = new StringBuilder(); // or StringBuffer
sb.append(i);

System.out.println(sb);

}
}

Output:-

10

Related Articles:-

Core Java Interview Questions and Answers

Leave a Reply

%d bloggers like this: