char keyword in Java
The “char
” keyword is used to declare a variable of character type.
“char
” is a Java primitive type.
A char variable can store a single Unicode character.
For example:
char delimiter = ';'
;
char letterA = 'A'
;
char letterB = 'B'
;
char letterC = 'C'
;
The char keyword can be used as a method parameter
- public void displayLetter(char letter) {
- System.out.println("passed letter is "+letter);
- }
public void displayLetter(char letter) { System.out.println("passed letter is "+letter); }
The char keyword can be used as a return type of a method as well
- public char getLetter() {
- return 'A';
- }
public char getLetter() { return 'A'; }
The Character class includes useful static methods for dealing with char variables, including isDigit(),isLetter(), isWhitespace() and toUpperCase().