char
About
Definition:
charis a Java primitive data type used to store single 16-bit Unicode characters.Size: Occupies 2 bytes (16 bits) of memory.
This is because Java uses the UTF-16 encoding to support a wide range of Unicode characters.
Value Range:
charcan represent any unsigned value between0and65535('\u0000'to'\uffff').Default Value: The default value for
charis'\u0000'(null character).Wrapper Class: The wrapper class for
charisCharacter, located injava.lang.
Characteristics
Single Character Representation:
Can represent any character, including alphabets, digits, symbols, or special Unicode characters.
E.g.,
'A','1','#','\n'.
Integral Type (Unsigned): Internally treated as an unsigned integer and can participate in arithmetic operations.
Immutable: Once assigned, the value of a
charcannot be altered unless explicitly reassigned.Unicode Compatibility:
Fully supports 16-bit Unicode for global character representation.
Allows the use of escape sequences like
'\uXXXX'to define characters.
Integral Representation: Each
charis a numeric value. For example:char c = 'A'; // 65 in ASCII char d = (char) (c + 1); // 'B'Control Characters: Special characters like
'\n'(newline),'\t'(tab), and'\r'(carriage return).Memory Usage: Fixed at 2 bytes because it uses UTF-16 encoding.
Char Arrays: Arrays of
charare commonly used for string manipulation.
Operations with char
charOperation
Example
Description
Arithmetic Operations
'A' + 1
Results in 'B' (Numeric addition).
Comparison Operations
'A' > 'a'
Compares the Unicode values of characters.
Type Casting to Int
(int) 'A'
Converts char to its Unicode integer value.
Escape Sequences
'\n', '\t', '\u0041'
Represents special or Unicode characters.
Conversion Methods
Conversion
Method
Example
char to String
Character.toString(c) or String.valueOf(c)
String.valueOf('A') → "A"
String to char
string.charAt(index)
"Hello".charAt(0) → 'H'
char to int
(int) c
(int) 'A' → 65
int to char
(char) i
(char) 65 → 'A'
Upper/Lower Case Conversion
Character.toUpperCase(c) / toLowerCase(c)
Character.toLowerCase('A') → 'a'
Wrapper Class Character
CharacterThe Character class provides utilities for working with char.
Key Features:
Constants:
Character.MIN_VALUE:'\u0000'.Character.MAX_VALUE:'\uffff'.
Static Methods:
Character.isDigit(char c): Checks if the character is a digit.Character.isLetter(char c): Checks if the character is a letter.Character.isUpperCase(char c): Checks if it’s uppercase.Character.isWhitespace(char c): Checks if it’s a whitespace character.
Common Mistakes
Using Strings Instead of
char:'A'(char) ≠"A"(String).
Casting Beyond Valid Range: Casting large integers to
charcan lead to unexpected results due to wrapping.Treating
charas Boolean: Avoid using characters like'Y'or'N'as substitutes fortrue/false
Examples
Basic example
Using char with Loops
char with LoopsWorking with Unicode Characters
Using char in Arrays
char in ArraysString Manipulation with char
charUsing char with Streams
char with StreamsCustom Character Validation
Last updated