Loading...

Java Booleans

boolean ripe = true; stores the word true, and System.out.println(ripe) writes true. The screen is one line. There are no quotes around true, and there is no digit on that line.

The word you write is true or false

Open Mint.java. The file name stays Mint.java, because public class Mint is still the name of the file. This page does not rename the file, and it does not change public class Mint. Delete int count if that line is still there, including 40, -4, and 040. Delete String plant if it is still there. Delete plant.length, delete charAt, and delete every line that asks a question after a dot. Delete any import line, any reader line, and any nextLine line. The file you keep has no import, no Scanner, no nextLine, no plant, no count, and no digit. An unused import does not by itself make javac fail, and it does not write true either. The word is written only by the writing line that uses the name.

Inside main, the storing line is boolean ripe = true;. boolean is the kind word. It says this name holds one of two words, true or false, and nothing else. ripe is the name, and it starts with a small r. The = sign stores the word. The word is true, every letter small: t, r, u, e. There is no capital T. There are no quotes around true. There is no digit. The semicolon ends the line. This line stores the word. It does not write the screen. Storing is not writing. The screen stays blank through this line. true is not a short way to write the number 1. false is not a short way to write 0. Those digits were the previous page. This page does not store them.

The two words are the whole of this kind. You do not invent a third word. boolean ripe = yes; treats yes as a name. This file does not have a name yes. javac writes an error. boolean ripe = no; does the same. The words you are allowed to write, with this kind word and with no quotes, are true and false. The file you keep uses true.

The writing line is the bare name: System.out.println(ripe);. No quotes, and no dot. Java reads the name ripe, writes the word that name holds, and ends the line. For the file you keep, that word is true. The letters of the name, r, i, p, e, are not on the screen. boolean is not on the screen. System is not on the screen. The semicolon is not on the screen. The word true is on the screen, as one line. Save the file.

The file you keep

Save this as Mint.java. It is the file this page keeps. One kind word, boolean. One name, ripe. One word, true, all small letters, no quotes, no digit. One writing line, and that line uses the bare name.

the file you keep

java
public class Mint {
    public static void main(String[] args) {
        boolean ripe = true;
        System.out.println(ripe);
    }
}

Read it from the top. public class Mint names the file. It does not write the screen. public static void main(String[] args) is where Java starts. The word String on that main line is still not a storing line. There is no String plant line in the file you keep, and there is no int line. boolean ripe = true; stores the word. It writes nothing. System.out.println(ripe); writes true and ends the line. One line on the screen. No quotes. No second line.

What the screen shows

In the folder that contains this Mint.java, type javac Mint.java. When the file is fine, that command writes no line. The word true after boolean does not make javac write a line, and it does not make javac fail. Then type java Mint. Do not type .java after java. The same two commands are what you type on Windows and on macOS. This page does not quote the shell prompt. The program writes true. It does not wait for you to type. The word was already in the file. If java Mint waits, nextLine is still in the file. Delete that line and keep boolean ripe = true; and the one writing line. The program finishes after the writing line.

The screen shows true. One line. No quotes. If the screen shows ripe, the writing line has quotes around the name: System.out.println("ripe");. Those quotes mark the letters r, i, p, e, so the stored word is not written. If the screen shows 40, or 4, or -4, or 32, the numbers file is still in place. Delete the int line. The file you keep has no digit. If the screen shows leaf, plant is still in the file. If the screen shows mint, you are still on an older file. If the screen shows false, the storing line says false, which is the other word, not the file you keep. You do not reinstall Java for a screen that shows the wrong line.

A screen that shows true does not, by itself, prove the kind. System.out.println("true"); with no boolean line at all would also write true, because the quotes hold the letters t, r, u, e. The screen can look the same. The file does not. The file you keep has boolean, and it has true with no quotes. When you judge this page, look for the kind word and look for the absence of quotes. Do not judge the kind from the screen alone.

Quotes around the word are letters, not this kind. boolean ripe = "true"; asks a boolean name to hold letters. javac writes an error. It does not turn those letters into the word true. It does not stay quiet. There is no screen to read, because you do not type java Mint while javac is writing an error. You do not need the exact words of that error. Delete the quotes so the line is boolean ripe = true; again. The same mismatch is boolean ripe = "false";. Quotes make letters. This kind does not hold letters.

The name has to exist before a line uses it. If System.out.println(ripe); is written above boolean ripe = true;, javac writes an error. The writing line names ripe before ripe exists. Move the storing line above the writing line. This is the same order as count on the numbers page and as plant on the letter pages. The file you keep already has that order. Do not write boolean a second time for the same name. boolean ripe = false; placed under a line that already created ripe is a different error. javac writes an error and does not stay quiet. The name is created once. The file you keep has one storing line.

boolean ripe; with no = true, followed by the writing line, makes javac write an error. The name would exist as a kind, and it would hold no word yet. Put = true on the creating line, the way the file you keep has it. boolean ripe = truefalse; treats truefalse as one name. This file does not have that name. javac writes an error. The word is true, and a space or a missing space does not invent a third word. boolean ripe = true ; with a space before the semicolon is still the word true. javac writes no line, and the screen is still true. The file you keep has no extra space, but that space is not a second value.

The other word is false

false is the other word this kind holds. It is not a second kind. The kind word stays boolean. The name can stay ripe. Only the stored word changes. boolean ripe = false; stores false, every letter small: f, a, l, s, e. No quotes. No digit. The writing line is still System.out.println(ripe);. The screen shows false. One line. The name ripe is not on the screen. This is not a letter count, and it is not the number 0.

This is the second file. It is not the file you keep. The file you keep stores true, and its screen is true. Save this second file as Mint.java when you want to see false, then put boolean ripe = true; back.

the other word

java
public class Mint {
    public static void main(String[] args) {
        boolean ripe = false;
        System.out.println(ripe);
    }
}

Type javac Mint.java. The file is fine, so javac writes no line. The word false after boolean does not make javac fail. Then type java Mint. The screen shows false. One line. No quotes. If the screen shows true, the storing line still says true, which is the file you keep. If the screen shows 0, an int line is still in the file. If the screen shows ripe, the writing line has quotes around the name. A later line can change the word without writing boolean again, once ripe already exists: ripe = false;. That line only replaces the word the name holds. It is allowed. It is not required for the file you keep, and it is not this second file. The second file stores false on the creating line. Do not write boolean on that later line. After you have seen false, put the kept file back: boolean ripe = true; and one System.out.println(ripe);.

A capital letter is not the word. boolean ripe = True; treats True as a name, because the first letter is a capital T. This file does not have a name True. javac writes an error. It does not stay quiet. The screen does not show True, because you do not run java Mint while javac is writing an error. The word in the file you keep is true, all small. boolean ripe = False; is the same mistake for the other word. False is a name this file does not have. The word is false, all small. Put the small letters back.

A digit does not fit

A digit is a whole number, the kind from the previous page. boolean ripe = 1; asks this kind to hold the number 1. That number does not fit. javac writes an error. It does not turn 1 into true. boolean ripe = 0; does not turn 0 into false. javac writes an error for that line too. boolean ripe = 40; is the same mismatch, a whole number in a name that holds only true or false. Delete the digits and write true, with no quotes, when you are repairing the file you keep. You do not need the exact words of these errors. Each of them is a line from javac, so you do not run java Mint until the storing line is boolean ripe = true; again and javac writes no line.

int count = true; is the mismatch the other way. int holds a whole number. true is not a digit you can store under int. javac writes an error. The file you keep does not use int at all. If both lines are in the file, boolean ripe = true; and int count = 40;, each name has its own kind, and each writing line shows its own value. That is allowed. It is not the file you keep. The file you keep is one name, ripe, and one word, true. Two kinds in one file was already the work of an earlier page. Here the proof is the screen word true, from boolean, with no digit beside it.

You can now store one of two words in a name and write that word without quotes and without a digit. boolean ripe = true; stores true. System.out.println(ripe); writes true and ends the line. boolean ripe = false; writes false, and that is the other word, not a second kind. Quotes around the word, a digit, and a capital T each make javac write an error. The next page leaves these two words. It looks again at a whole number you write in the file, and at a kind word that does not already match that value.

The screen is the word you stored

The file you keep writes true. The name on the writing line is ripe, and ripe is not on the screen. There are no quotes in that file. One println is one line. false, when you use the second file, is the other word on that line. A digit does not belong in the file you keep.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
5
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top