how can i convert int to char or string

now les say: i want to convert int to char
ex:
int a = 3
char b = '3'
note they both are same number

or int to String
int a = 35;
String c = "35";

note i already tried

b = (char)a; but failed. it print weird symbol

Try Integer.toString(a) or String.valueOf(a)

you can try this

from int to string

int a=3;
String s=a+"":

from int to char

char c = (char)(((int)'0')+a);

however here the value of 'a' should be a single digit number

commented: good solution +4

The right way to convert an int to a char is using the Character.forDigit method.

char b = Character.forDigit(a,10);
commented: really new thing i get to learn... +4

you can convert it in different way like:

1) built-in convert

int a1 = 5;
String s = ""+a1;

2) integer convert

int a2 = 5;
String s2 = Integer.toString(a2);

3) format convert

int a3 = 9;
String s3 =String.format("%d", a3);

Also one possible solution is:

StringBuilder sb = new StringBuilder();
sb.append("");
sb.append(i);
String strI = sb.toString();

or simply

Integer i;
String s = i.toString();

or

int aInt = 1;
String aString = Integer.toString(aInt);
commented: Best and most complete answer +4

and for a String, you could use
String newString = String.valueOf(a);

One more thing is:
In-fact String.valueOf() method is overloaded to accept almost all primitive type so you can use it convert char, double, float or any other data type into String. Static binding is used to call corresponding method in Java. Here is an example of int to String using String.valueOf().

String price = String.valueOf(123);

instead of 123 you can also pass int variable

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.