Character Variable in Java Programming
Java Basic Concepts
In this lesson, we will learn about Character variable in Java programming with the help of some examples. We will also play a quiz on it at the end.
What is Character Variable
A Character variable in Java programming can store any character enclosed within single quotes. To declare a variable of type character we use the keyword char (pronounced as kar).
Syntax of Declaring Character Variable in Java
char variable_name;
Here char is used for declaring Character data type and variable_name is the name of variable (you can use any name of your choice for example: a, b, c, alpha, etc.) and ; is used for line terminator (end of line).
Now let's see some examples for more understanding.
Example 1
Declare a character variable x.
char x;
Example 2
Declare 3 character variables x, y, and z to assign 3 different characters in it.
char x='A', y='5', z='#';
Note: A character can be anything, it can be an alphabet or digit or any other symbol, but it must be single in quantity.
Example 3
Declare a character variable x and assign the character '$' and change it value to @ in the next line.
char x = '$';
x = '@'; //now the new value of x is @
Test Your Knowledge
Attempt the multiple choice quiz to check if the lesson is adequately clear to you.