How to Declare Char Variable in Java?

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:


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


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);


char1 and char2: X Y

See also:

Leave a Comment