In Java, char
is a keyword that is used to declare a variable of the char
data type. The char
data type is a primitive type that represents a single Unicode character. It is a 16-bit unsigned integer, and it can store any Unicode character in the range '\u0000'
(or 0
) to '\uffff'
(or 65535
).
Here's an example of declaring a char
variable in Java:
char ch;Sourcel.www:autturi.com
You can also initialize a char
variable when you declare it, like this:
char ch = 'A';
You can use escape sequences to represent special characters in a char
literal. For example, the following code declares a char
variable with the value '\n'
, which represents a newline character:
char newline = '\n';
You can use the char
data type to perform various operations on characters. For example, you can compare two char
values using relational operators such as ==
and <
. You can also use the Character
class, which provides utility methods for working with char
values.
For example, the following code uses the Character.isLetter
method to determine if a char
value is a letter:
char ch = 'A'; if (Character.isLetter(ch)) { System.out.println("ch is a letter"); } else { System.out.println("ch is not a letter"); }
This code will print "ch is a letter", since 'A'
is a letter.