Skip to lesson
MentorNxt home

Java Programming

Variables & Data Types

A char holds a single character, like 'A'. A boolean holds a yes/no value: true or false.

char: one character

Your grade is A and your section is B: each is exactly one letter. A char holds one character (a letter, a digit or a symbol), written in single quotes: char grade = 'A';.

Example 1 · Grade and section

Result.javaJava
public class Result {    public static void main(String[] args) {        char grade = 'A';        char section = 'B';        char symbol = '@';        System.out.println("Grade: " + grade);        System.out.println("Section: " + section);        System.out.println("Symbol: " + symbol);    }}
Output
Grade: A
Section: B
Symbol: @

What this code does

  • Each char holds one character in single quotes: a letter like 'A' or a symbol like '@'. A digit such as '7' is a character too, not the number 7.
  • A char prints as the character itself, without the quotes.

Single quotes take exactly one character, so 'AB' is an error. Double quotes make a String, so char grade = "A"; does not compile either.

Every char is a number underneath

How does a computer that only knows 0s and 1s store the letter A? It gives every character a number and stores the number. 'A' is 65, 'B' is 66 and 'a' is 97.

This numbering is called Unicode. It has a number for characters in many scripts, so a char can hold 'अ' too. Put (int) in front of a char to see its number, and add 1 to move to the next character; Type conversion and casting (opens in a new tab) explains the brackets.

Experiment

Character numbers

Type one character. See its Unicode number and the character after it.

char c = 'A';
System.out.println((int) c);
System.out.println((char) (c + 1));
its number
65
the next character
B
printed with a label
Grade: A
Some characters, such as many emoji, need two chars. Use String for general text.

boolean: true or false

Has the student passed? Are the fees paid? Each answer is only yes or no. A boolean holds one of two values, true or false, written in small letters and without quotes.

Example 2 · Change a boolean

Fees.javaJava
public class Fees {    public static void main(String[] args) {        boolean hasPaidFees = false;        boolean hasBacklog = false;        System.out.println("Fees paid: " + hasPaidFees);        hasPaidFees = true;        System.out.println("Fees paid: " + hasPaidFees);        System.out.println("Backlog: " + hasBacklog);    }}
Output
Fees paid: false
Fees paid: true
Backlog: false

What this code does

  • A boolean is a variable, so its value can change: here hasPaidFees goes from false to true.
  • true and false print as the words, without quotes.

"true" with quotes is text, and 1 or 0 is a number; neither can go in a boolean. In Conditional statements (opens in a new tab), boolean values help a program make decisions.

Good names for boolean variables

boolean paid = true; leaves a question: who paid what? A good boolean name reads like a yes/no question.

Java
boolean isPassed = true;boolean hasPaidFees = false;boolean isOnline = true;

Start it with is or has: isPassed, hasPaidFees, isOnline. Then the code reads like English: "is passed: true".

Next step

Check your understanding

6 quick questions