Java

What Is Char Keyword In Java

One form of data is the term “char.” A character like “A,” “a,” etc. can be stored in the char variable.

Important Information

  • Single quotes are used to specify the value allocated to a char data type, such as “Q,” “h,” etc.
  • Additionally, you can supply char variables with integer values in the range of 0 to 65,535, which are handled as ASCII characters. If you print the value of a char type variable with a 72 value, for instance, it will output “H.”
  • There is only one 16-bit Unicode character in it. The minimum value is “u0000” (or 0), whereas the maximum value is “uffff” (or 65,535).

Example of char Keyword

public class JavaExample {
  public static void main(String[] args) {
    char ch1 = 'A';
    char ch2 = 'p';
    System.out.println(ch1);
    System.out.println(ch2);
  }
}

Output:

A
p

Example 2: Passing ASCII value to char type variable

public class JavaExample {
  public static void main(String[] args) {
    char ch1 = 72;
    char ch2 = 125;
    System.out.println(ch1); //prints 'H'
    System.out.println(ch2); //prints '}'
  }
}

Output:

char keyword example output

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button