Java char variable declaration example.

How to Declare Char Variable in Java?

  • Java
  • 1 min read

In Java, all variables must be declared before used in the program. To declare a char variable in Java, specify the char followed by the variable name and optionally assign a value. Below are the syntax and the examples:

Syntax

char identifier [ = value ], identifier [ = value ]...];

 Examples

char cityName = 'Delhi';

char char1, char2;

char char1 = 'x', char2;

Java Program Example Using char Variable

class CharVarDemo
{
  
public static void main (String args[])
  {
    
char char1, char2;
     
char1 = 88; // ascii code for X
char2 = 'Y';
     
System.out.print ("char1 and char2: ");
     
System.out.println (char1 + " " + char2);
 
} 
}

Output

char1 and char2: X Y

See also: