Java char, Unicode and Table-lookup Programs (ISC Class 11 Computer Science)
Updated 17 October 2026 · Fact-checked
In Java the char type is a 16-bit Unicode value, so programs can store characters of any script. A char can be written as a literal ('A') or as a Unicode escape ('\u0915'). Casting a char to int gives its code; adding an int to a char gives an int, so (char) is needed to get a character back. Table lookup uses an array or string indexed by a code or digit to find the matching character or word in another language.
A char turns into an int in arithmetic, so cast back with (char); a digit can also be an array index for lookup.
Illustration drawn for QuestionBuddy.
Understand Characters in Java: char, Unicode, Character Arithmetic and Table Lookup
char in Java. 16 bits (0 to 65,535), unsigned; stores a UTF-16 code unit; default value '\u0000'.
Literals. 'A' or '\u0041' (Unicode escape: backslash u followed by four hex digits). Escape sequences: '\n' newline, '\t' tab, '\\' backslash, '\'' quote.
Character arithmetic. In expressions a char is promoted to int.
char c = 'A';
c += 2; // compound assignment keeps type: c is 'C'
System.out.println(c); // C
System.out.println(c + 1); // 68 (int)
System.out.println((char) (c + 1)); // D
System.out.println('a' + 'b'); // 195 (int)
System.out.println("" + 'a' + 'b'); // abUseful: (int) ch gives the code; (char) n gives the character; ch - '0' converts a digit character to its value; Character.isDigit(), isLetter(), toUpperCase() are library helpers.
Caesar shift (small letters): (char) ((ch - 'a' + k) % 26 + 'a').
Table lookup. Keep the target-language values in an array and use the code or digit as the index.
String w[] = {"zero","one","two","three","four","five","six","seven","eight","nine"};Digit characters of 407 are looked up as w[4], w[0], w[7]. Local-language digits can be produced by adding the digit to the code of the first digit of the script: (char) ('\u0966' + d) gives the Devanagari digit for d.
Source files. Save as UTF-8 and compile with javac -encoding UTF-8 when they contain non-ASCII letters.
Characters in Java: char, Unicode, Character Arithmetic and Table Lookup: key facts
- char facts
- char = 16 bits, 0 to 65,535
- Promoted to int in expressions
- (char) restores a character
- Unicode value.
- Digit to value
- value = ch - '0' | ch = (char) (value + '0')
- Digit characters.
- Caesar shift
- (char) ((ch - 'a' + k) % 26 + 'a') | Wraps around z to a
- Small letters.
How to answer Characters in Java: char, Unicode, Character Arithmetic and Table Lookup questions
Remember that char arithmetic gives int unless cast.
- 1Write the codes of the characters involved.
- 2Do the arithmetic as int.
- 3Cast to char only when a character is wanted.
- 4For lookup, compute the index and read the table.
Characters in Java: char, Unicode, Character Arithmetic and Table Lookup in real life
The idea, met in an everyday situation. Answer the question in your own words, then compare with the model answer. The one-line hook is what to remember.
Table lookup
Suppose a program must print an amount of 305 rupees as words digit by digit, such as three zero five.
How is each digit turned into a word?
Try it first, then show the solutionSolution
- Extract each digit character and convert it: d = ch - '0'.
- Use w[d] from the array of digit names.
Answer: Use w[ch - '0'] for each digit.
Memory hook: The digit is the index.
Tricks to remember Characters in Java: char, Unicode, Character Arithmetic and Table Lookup
Short memory aids. Say each one aloud once, then use it on a question the same day.
char + int = int
Cast back with (char).
Use it when: Arithmetic.
ch minus zero char
ch - '0' gives the digit value.
Use it when: Digits.
u plus four hex
\u0915 is a Unicode escape.
Use it when: Escapes.
Characters in Java: char, Unicode, Character Arithmetic and Table Lookup: solved examples
Ordered from board level to HOTS. Try each one before you open the solution.
Example 1: char arithmetic · 1 mark · MCQBoard level
The output of: char k = 'K'; k++; System.out.println(k); is:
(a) 76 (b) L (c) K1 (d) 75
Try it first, then show the solutionSolution
- k++ keeps the type char: the next character after 'K' is 'L'.
Answer: (b) L
Example 2: Output tracing · 5 marks · Short answerBoard level
Give the output:
char c = 'A';
c += 2;
System.out.println(c);
System.out.println(c + 1);
System.out.println((char) (c + 1));
System.out.println('a' + 'b');
System.out.println("" + 'a' + 'b');(5 marks)
Try it first, then show the solutionSolution
- c becomes 'C' (code 67).
- c + 1 = 68 (int).
- (char) 68 = 'D'.
- 'a' + 'b' = 97 + 98 = 195.
- "" + 'a' + 'b' = "ab".
Answer: C
68
D
195
ab
Example 3: Caesar shift · 6 marks · Long answerHarder board level
Write a code segment to encode the word "xyz" by shifting each small letter forward by 3 (wrapping z to a) and give the output. Decode "abc" by the reverse shift. (6 marks)
Try it first, then show the solutionSolution
- For each ch compute (ch - 'a' + 3) % 26 + 'a'.
- x: (23 + 3) % 26 = 0 gives 'a'; y: 24 + 3 = 27 % 26 = 1 gives 'b'; z: 25 + 3 = 28 % 26 = 2 gives 'c'.
- Output "abc". Decoding "abc" with (ch - 'a' - 3 + 26) % 26 gives x, y, z.
Answer:
Output: abcString s = "xyz", r = "";
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
r = r + (char) ((ch - 'a' + 3) % 26 + 'a');
}
System.out.println(r);
Example 4: Table lookup: digits to words · 5 marks · Long answerHarder board level
Write a program segment that prints each digit of the number 407 in words using the array w[] = {"zero", ..., "nine"} and give the output. (5 marks)
Try it first, then show the solutionSolution
- Convert the number to a string: String s = "" + 407;
- For each character ch, index = ch - '0'.
- 4 gives four, 0 gives zero, 7 gives seven.
Answer:
Output: four zero sevenString s = "" + 407;
for (int i = 0; i < s.length(); i++)
System.out.print(w[s.charAt(i) - '0'] + " ");
Example 5: Local-language digits · 5 marks · Long answerHOTS / competency
The Devanagari digit zero has the code U+0966. Write the expression that gives the Devanagari digit for a value d, evaluate the code for d = 5 (decimal), and explain why the source file should be saved as UTF-8 when a Hindi string is typed directly. (5 marks)
Try it first, then show the solutionSolution
- Expression: (char) ('\u0966' + d).
- U+0966 = 9 x 256 + 6 x 16 + 6 = 2,406; for d = 5 the code is 2,411 (U+096B).
- A char holds a 16-bit Unicode value so the Hindi digit can be stored.
- Typed Hindi text in the source needs UTF-8 and javac -encoding UTF-8, or the compiler may read the bytes with the wrong encoding.
Answer: (char) ('\u0966' + d); for d = 5 the code is 2,411 (U+096B). UTF-8 is needed so the compiler reads the non-ASCII characters correctly.
Where students lose marks in Characters in Java: char, Unicode, Character Arithmetic and Table Lookup
The slips examiners see most often, and what to write instead.
Mistake: Printing a char sum as a character
Missing the cast.
Wrong working
System.out.println('a' + 'b') gives ab
Right working
195
Do this: char + char is int.
Usually costs: 2 marks.
Mistake: Forgetting the wrap-around in a shift
Direct addition.
Wrong working
'z' + 3 = '}'
Right working
(25 + 3) % 26 = 2 gives c
Do this: Use % 26.
Usually costs: 2 marks.
Mistake: Using the char as an index directly
Not subtracting '0'.
Wrong working
w[ch]
Right working
w[ch - '0']
Do this: Convert the digit first.
Usually costs: 2 marks.
Exam tips
- Write the codes of the characters you use.
- Cast to char whenever a character result is wanted.
- Mention UTF-8 when non-ASCII text is typed in a source file.
Practice questions on Characters in Java: char, Unicode, Character Arithmetic and Table Lookup
Write each answer on paper first. Open the hint only if you are stuck, and the answer only when you have finished.
Q1 · 1 mark · MCQBoard level
The value of (int) 'B' is:
(a) 66 (b) 65 (c) 98 (d) 2Hint
A is 65.
Answer
(a) 66
Q2 · 3 marks · Short answerBoard level
Give the output of: char x = '8'; int n = x - '0'; System.out.println(n * 2); System.out.println(x + 1); System.out.println((char) (x + 1)); (3 marks)
Hint
'8' has code 56.
Answer
16
57
9Q3 · 4 marks · Short answerHarder board level
Write a segment to count the digits and the capital letters in a string s = "Room 21 at ISC" and give both counts. (4 marks)
Hint
Character.isDigit and isUpperCase.
Answer
Loop over s with Character.isDigit(ch) and ch >= 'A' && ch <= 'Z'. Digits: 2 and 1 = 2. Capital letters: R, I, S, C = 4. Output: 2 4
Q4 · 4 marks · Short answerHarder board level
A program converts a string to its "next letter" form: each small letter is replaced by the next letter and z becomes a. Write the code and the output for "hello zoo". (4 marks)
Hint
Keep the space.
Answer
For each ch, if ch >= 'a' && ch <= 'z' append (char) ((ch - 'a' + 1) % 26 + 'a'), else append ch. "hello zoo" becomes "ifmmp app".
Q5 · 3 marks · Short answerHOTS / competency
A student sees the output 98 for System.out.println('a' + 1) and expects b. Explain, then state the changes needed to print b and the output of System.out.println((char) ('a' + 1) + 1). (3 marks)
Hint
char + int is int.
Answer
'a' + 1 is an int expression (97 + 1 = 98). Use (char) ('a' + 1) to print b. In the second case (char) ('a' + 1) is 'b' and adding 1 again gives the int 99, so the output is 99.
Test your recall
Answer in your head or on paper first, then open the card. Pulling an answer out of memory makes it stick far better than re-reading.
1.char size in Java?Show answer
16 bits.
2.char + int gives?Show answer
int.
3.Digit char to value?Show answer
ch - '0'.
4.Unicode escape form?Show answer
\u followed by four hex digits.
5.Default char value?Show answer
\u0000.
Characters in Java: char, Unicode, Character Arithmetic and Table Lookup: frequently asked questions
Is char signed?
No, it is unsigned.
Can char hold Hindi letters?
Yes.
What does Character.isDigit do?
Tests if a char is a digit.
What is javac -encoding UTF-8 for?
Reading non-ASCII source text correctly.