📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
- The char keyword is used to declare character variable.
- char is a Java primitive type.
- In Java, the data type used to store characters is char. The char data type is a single 16-bit Unicode character.
- It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).
- The Wrapper Character class represents char primitive type as an object.
char Java Keyword Examples
// Demonstrate char data type.
class CharDemo {
public static void main(String args[]) {
char ch1, ch2;
ch1 = 88; // code for X
ch2 = 'Y';
System.out.print("ch1 and ch2: ");
System.out.println(ch1 + " " + ch2);
}
}
ch1 and ch2: X Y
// char variables behave like integers.
class CharDemo {
public static void main(String args[]) {
char ch1;
ch1 = 'X';
System.out.println("ch1 contains " + ch1);
ch1++; // increment ch1
System.out.println("ch1 is now " + ch1);
}
}
ch1 contains X
ch1 is now Y
Summary
- The following char constants are available:
- \b − Backspace
- \f − Form feed
- \n − Newline
- \r − Carriage return
- \t − Horizontal tab
- ' − Single quote
- " − Double quote
- " − Backslash
- The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).
- The Wrapper Character class represents char primitive type as an object and which includes useful static methods for dealing with char variables, including isDigit(), isLetter(), isWhitespace() and toUpperCase().
Comments
Post a Comment
Leave Comment