ASCII Value of a Character
Java coding interview problem for Character Problems: ASCII Value of a Character.
Finding the ASCII value of a character is one of the most fundamental Java interview questions.
Although it looks very simple, it tests your understanding of:
- Character Encoding
- ASCII
- Unicode
- Type Casting
- Primitive Data Types
- Character APIs
- Memory Representation
Many advanced String algorithms internally depend on ASCII or Unicode values.
Examples include:
- Toggle Case
- Character Frequency
- Caesar Cipher
- Encryption
- Compression Algorithms
- Lexicographical Comparison
Understanding ASCII makes many String interview questions much easier.
What is ASCII?
ASCII stands for
American Standard Code for Information Interchange
It is a character encoding standard that assigns a unique numeric value to every English letter, digit, and special symbol.
Instead of storing
A
the computer stores
65
Instead of storing
a
the computer stores
97
Computers process numbers internally.
ASCII provides a mapping between characters and numbers.
History of ASCII
ASCII was introduced in
1963
by the
American National Standards Institute (ANSI)
Its purpose was to create a common standard for communication between computers.
Originally,
ASCII defined
128 Characters
Numbered
0
↓
127
These include
- English Letters
- Digits
- Symbols
- Control Characters
Later,
Unicode extended ASCII to support almost every language in the world.
Why is this Question Asked in Interviews?
Interviewers ask this question to evaluate:
- Character Encoding Knowledge
- Primitive Type Conversion
- Type Casting
- Unicode Basics
- Character Manipulation
- Java Internals
Although easy,
it serves as the foundation for many advanced String problems.
Real-World Applications
ASCII values are used in many applications.
Encryption
Caesar Cipher
A
↓
65
Shift
+3
Result
68
↓
D
Password Validation
Determine
- Uppercase
- Lowercase
- Digits
using ASCII ranges.
Character Sorting
Lexicographical comparison depends on character values.
Networking
Protocols often transmit bytes representing ASCII values.
File Formats
Text files internally store characters using ASCII or Unicode encoding.
Compiler Design
Compilers identify
- Letters
- Digits
- Operators
using character codes.
Problem Statement
Given a character,
print its corresponding ASCII value.
Example 1
Input
A
Output
65
Example 2
Input
a
Output
97
Example 3
Input
1
Output
49
Example 4
Input
@
Output
64
ASCII Table
Uppercase Letters
| Character | ASCII |
|---|---|
| A | 65 |
| B | 66 |
| C | 67 |
| D | 68 |
| E | 69 |
| F | 70 |
| ... | ... |
| Z | 90 |
Lowercase Letters
| Character | ASCII |
|---|---|
| a | 97 |
| b | 98 |
| c | 99 |
| d | 100 |
| e | 101 |
| ... | ... |
| z | 122 |
Digits
| Character | ASCII |
|---|---|
| 0 | 48 |
| 1 | 49 |
| 2 | 50 |
| 3 | 51 |
| 4 | 52 |
| 5 | 53 |
| 6 | 54 |
| 7 | 55 |
| 8 | 56 |
| 9 | 57 |
Common Special Characters
| Character | ASCII |
|---|---|
| Space | 32 |
| ! | 33 |
| " | 34 |
| # | 35 |
| $ | 36 |
| % | 37 |
| & | 38 |
| ' | 39 |
| ( | 40 |
| ) | 41 |
| * | 42 |
| + | 43 |
| , | 44 |
| - | 45 |
| . | 46 |
| / | 47 |
| : | 58 |
| ; | 59 |
| < | 60 |
| = | 61 |
| > | 62 |
| ? | 63 |
| @ | 64 |
Understanding Character Encoding
Suppose we have
A
Computer stores
65
Character
B
Stored as
66
Character
a
Stored as
97
Character
1
Stored as
49
Everything inside the computer is ultimately represented as numbers.
ASCII Visualization
Character
A
↓
65
Character
Z
↓
90
Character
a
↓
97
Character
0
↓
48
Character
@
↓
64
Mathematical Concept
Uppercase letters
A
↓
65
B
↓
66
Difference
1
Lowercase letters
a
↓
97
b
↓
98
Difference
1
Digits
0
↓
48
9
↓
57
Range
48
↓
57
Dry Run
Input
C
Read
Character
↓
C
Type Cast
(int)'C'
Result
67
Output
67
Input
9
Cast
(int)'9'
Output
57
Approach 1 — Using Type Casting (Recommended)
Every char in Java internally stores a numeric Unicode value.
For ASCII characters,
this numeric value is identical to the ASCII code.
We can obtain it simply by casting the character to an int.
Algorithm
- Read the character.
- Cast it to
int. - Print the numeric value.
Java Program
public class ASCIIValueUsingCasting {
public static void main(String[] args) {
char ch = 'A';
int ascii = (int) ch;
System.out.println("Character : " + ch);
System.out.println("ASCII Value : " + ascii);
}
}
Output
Character : A
ASCII Value : 65
Step-by-Step Code Explanation
Declare a character.
char ch = 'A';
Convert it into an integer.
int ascii = (int) ch;
Print the result.
System.out.println(ascii);
The cast returns the Unicode code point for the character. For standard ASCII characters (0–127), this value matches the ASCII code exactly.
Dry Run of Type Casting
Input
B
Cast
(int)'B'
Result
66
ASCII Value = 66
Advantages
- Simplest solution.
- Fastest approach.
- No additional methods required.
- Preferred interview solution.
- Constant time complexity.
Drawbacks
- Beginners may not understand type casting initially.
- Returns the Unicode code point for non-ASCII characters, not an ASCII value.
Approach 2 — Using Character.codePointAt()
The Character class provides methods to work with Unicode code points.
Although primarily designed for Unicode,
it also works for ASCII characters.
Algorithm
- Store the character in a string.
- Call
codePointAt(0). - Print the returned value.
Java Program
public class ASCIIUsingCodePoint {
public static void main(String[] args) {
String text = "A";
int ascii = text.codePointAt(0);
System.out.println("Character : " + text.charAt(0));
System.out.println("ASCII Value : " + ascii);
}
}
Output
Character : A
ASCII Value : 65
Step-by-Step Code Explanation
Create a string.
String text = "A";
Read the Unicode code point.
text.codePointAt(0);
Store the result.
int ascii = text.codePointAt(0);
Print the value.
System.out.println(ascii);
Time & Space Complexity
| Approach | Time | Extra Space |
|---|---|---|
| Type Casting | O(1) | O(1) |
| codePointAt() | O(1) | O(1) |
Comparison of Approaches
| Feature | Type Casting | codePointAt() |
|---|---|---|
| Interview Friendly | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐ |
| Performance | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐⭐ |
| Easy to Understand | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐ |
| Supports Unicode | ✅ | ✅ |
| Best Use Case | Single Character | Strings & Unicode |
Advantages
- Both approaches run in O(1) time.
- Both work for ASCII characters.
- Both also return Unicode code points for non-ASCII characters.
- Type Casting is the simplest interview solution.
Drawbacks
- Neither approach validates whether the character belongs to the ASCII range.
codePointAt()requires aStringand is slightly more verbose.
Approach 3 — Using Integer.valueOf()
Java automatically converts a char to its corresponding integer value.
We can use Integer.valueOf() after casting the character to an integer.
Although this approach is less common, it demonstrates Java wrapper class usage.
Algorithm
- Read a character.
- Cast it to an integer.
- Pass it to
Integer.valueOf(). - Print the value.
Java Program
public class ASCIIUsingIntegerValueOf {
public static void main(String[] args) {
char ch = 'A';
Integer ascii = Integer.valueOf((int) ch);
System.out.println("Character : " + ch);
System.out.println("ASCII Value : " + ascii);
}
}
Output
Character : A
ASCII Value : 65
Advantages
- Demonstrates Wrapper Classes.
- Useful when Integer objects are required.
- Easy to understand.
Drawbacks
- Extra object creation.
- No advantage over simple casting.
- Rarely used in interviews.
Approach 4 — Using Unicode Code Point APIs
Java internally uses Unicode rather than ASCII.
The Character class provides APIs for working with Unicode code points.
These APIs are especially useful when handling multilingual text.
Why Unicode APIs?
ASCII supports only
128 Characters
Unicode supports
Millions of Characters
including
English
Hindi
Chinese
Japanese
Emoji
Java Program
public class UnicodeCodePointExample {
public static void main(String[] args) {
char ch = 'A';
int codePoint = Character.codePointAt(
new char[]{ch}, 0);
System.out.println("Character : " + ch);
System.out.println("Code Point : " + codePoint);
}
}
Output
Character : A
Code Point : 65
Advantages
- Unicode-aware.
- Production-ready.
- Handles international text.
Drawbacks
- Slightly more verbose.
- Overkill for ASCII-only problems.
Approach 5 — Display ASCII Table Programmatically
Sometimes interviewers ask:
"Can you print the entire ASCII table?"
This is a common follow-up question.
Algorithm
- Iterate from 0 to 127.
- Convert each integer into a character.
- Print both values.
Java Program
public class PrintASCIITable {
public static void main(String[] args) {
System.out.printf("%-10s %-10s%n",
"ASCII", "Character");
for (int i = 0; i <= 127; i++) {
System.out.printf("%-10d %-10c%n",
i, (char) i);
}
}
}
Sample Output
ASCII Character
65 A
66 B
67 C
68 D
69 E
...
97 a
98 b
99 c
Advantages
- Demonstrates loops.
- Shows character conversion.
- Useful for learning ASCII.
Drawbacks
- Not intended for production use.
- Prints control characters (0–31), which may not be visible.
ASCII vs Unicode
| Feature | ASCII | Unicode |
|---|---|---|
| Character Count | 128 | Over 1.1 Million Code Points |
| English Support | ✅ | ✅ |
| International Languages | ❌ | ✅ |
| Emoji Support | ❌ | ✅ |
| Used by Java | Partially | ✅ |
Example
ASCII
A
↓
65
Unicode
😊
↓
128522 (U+1F60A)
Important: Java
charis a UTF-16 code unit. Characters outside the Basic Multilingual Plane (such as many emojis) require twocharvalues (a surrogate pair). Use Unicode code point APIs when working with such characters.
UTF-8 vs UTF-16 Overview
| Feature | UTF-8 | UTF-16 |
|---|---|---|
| Encoding Size | 1–4 Bytes | 2 or 4 Bytes |
| ASCII Compatible | ✅ | No |
| Storage Efficiency | Better for English | Better for many Asian languages |
| Java String Storage | Internally uses UTF-16 code units | ✅ |
Edge Cases
| Input | Output |
|---|---|
'A' |
65 |
'a' |
97 |
'0' |
48 |
'@' |
64 |
' ' |
32 |
'\n' |
10 |
'é' |
233 (Unicode code point; also part of extended Latin, not standard ASCII) |
'😊' |
Requires Unicode code point handling (cannot be represented by a single char) |
Time & Space Complexity
| Approach | Time | Extra Space |
|---|---|---|
| Type Casting | O(1) | O(1) |
| codePointAt() | O(1) | O(1) |
| Integer.valueOf() | O(1) | O(1) |
| Unicode APIs | O(1) | O(1) |
| ASCII Table Generation | O(128) | O(1) |
Since 128 is a constant, printing the ASCII table is also considered O(1) with respect to input size.
Comparison of All Approaches
| Approach | Interview Friendly | Performance | Unicode Support | Best Use Case |
|---|---|---|---|---|
| Type Casting | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐⭐ | ✅ | Recommended interview solution |
| codePointAt() | ⭐⭐⭐⭐ | ⭐⭐⭐⭐⭐ | ✅ | Working with strings |
| Integer.valueOf() | ⭐⭐⭐ | ⭐⭐⭐⭐ | ✅ | Wrapper class demonstration |
| Unicode APIs | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐⭐ | ✅ | International applications |
| ASCII Table Program | ⭐⭐⭐⭐ | ⭐⭐⭐⭐ | N/A | Learning and debugging |
Common Interview Mistakes
Mistake 1
Thinking Java stores characters using ASCII.
Java stores characters using Unicode (UTF-16 code units).
ASCII values match Unicode only for characters in the range 0–127.
Mistake 2
Forgetting type casting.
Wrong
char ch = 'A';
System.out.println(ch);
Correct
System.out.println((int) ch);
Mistake 3
Confusing digits with numbers.
'5'
ASCII
53
Numeric value
5
They are different.
Mistake 4
Assuming every Unicode character fits in one char.
Many emojis require two UTF-16 code units (a surrogate pair).
Mistake 5
Using ASCII ranges for all languages.
ASCII range checks work only for English characters.
Interview Follow-up Questions
Q1. What is ASCII?
Q2. What is Unicode?
Q3. Why does Java use Unicode instead of ASCII?
Q4. What is the ASCII value of 'A'?
Q5. What is the difference between '5' and 5?
Q6. How do you convert an ASCII value back into a character?
Q7. What is UTF-8?
Q8. What is UTF-16?
Q9. Can Java store emojis in a single char?
Q10. How do you print the complete ASCII table?
Related Problems
- Toggle Case
- Character Frequency
- Count Uppercase and Lowercase Characters
- Count Digits and Special Characters
- Check Unique Characters
- Caesar Cipher
- Reverse a String
- Character Classification
- Unicode Code Point Handling
Key Takeaways
- ASCII assigns numeric values to the first 128 characters.
- Java stores characters using Unicode (UTF-16 code units).
- Type Casting is the simplest and most recommended interview solution.
codePointAt()and Unicode APIs are useful for multilingual applications.Integer.valueOf()is mainly useful for demonstrating wrapper classes.- Understanding ASCII is essential for many String and Character interview questions.
Frequently Asked Interview Questions
Q1. Which solution is best for interviews?
Simple type casting is the preferred approach because it is concise, fast, and demonstrates an understanding of Java's char type.
Q2. Is Java based on ASCII?
No.
Java uses Unicode. ASCII is simply a subset of Unicode covering characters 0–127.
Q3. Why does (int) 'A' return 65?
The character 'A' has the Unicode code point 65, which is identical to its ASCII value.
Q4. What is the difference between 'A' and 65?
'A'is a character.65is an integer.- Casting converts the character to its numeric code point.
Q5. How do you convert an ASCII value into a character?
int value = 65;
char ch = (char) value;
System.out.println(ch);
Output
A
Interview Tip
If an interviewer asks:
"How do you find the ASCII value of a character in Java?"
Start with the type casting approach because it is the simplest and most commonly expected answer.
Then explain that Java actually uses Unicode, so for ASCII characters the Unicode code point and ASCII value are the same.
You can further impress the interviewer by discussing:
- Type Casting (recommended)
codePointAt()for strings- Unicode APIs for multilingual applications
- Printing the ASCII table programmatically
- The differences between ASCII, Unicode, UTF-8, and UTF-16
This demonstrates not only Java programming skills but also a solid understanding of character encoding, which is valuable for text processing and internationalization.