How can I convert an int to char?
I am using:
int i=20;
char c= Character.forDigit(i,10);
// OR
//char c= Integer.toString(i).charAt(0);
System.out.println(c);
- I want char as ’20’ (NOT to treat 20 as ASCII value and printing some char whose ASCII is 20).
- The above program works fine for single digit but for double digit like 20, it prints only first character,
2.
How can I print 20 as a whole?
A char is a single character, that is a letter, a digit, a punctuation mark, a tab, a space or something similar. A char literal is a single one character enclosed in single quote marks like this
char myCharacter = ‘g’;
Some characters are hard to type. For these Java provides escape sequences. This is a backslash followed by an alphanumeric code. For instance ‘\n’ is the newline character. ‘\t’ is the tab character. ‘\’ is the backslash itself. The following escape sequences are defined:
\b backspace
\t tab
\n linefeed
\f formfeed
\r carriage return
\” double quote, ”
\’ single quote, ‘
\ backslash, \
The double quote escape sequence is used mainly inside strings where it would otherwise terminate the string. For instance
System.out.println(“And then Jim said, \”Who’s at the door?\””);
It isn’t necessary to escape the double quote inside single quotes. The following line is legal in Java
char doublequote = ‘”‘;
To convert into String:
String.valueOf(20);