Loading...

Java Logical Operators

boolean both = true && true; stores the word true, and System.out.println(both) writes true. The screen is one line. The two ampersands sit between the two true words in the file, and they are not on that line.

Two ampersands need both sides true

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 boolean match if that line is still there, including 4 == 4, 4 == 9, and every ask mark between two whole numbers. Delete int total if that line is still there. Delete String[] plants if that line is still there. Delete boolean ripe if that line is still there with a bare true or bare false and no combining mark. Delete any import line, any reader line, and any nextLine line. The file you keep has no import, no Scanner, no nextLine, no match, no total, no plants, and no bare ripe line. 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 that holds the combined answer.

Inside main, the storing line is boolean both = true && true;. boolean is the kind word. It says this name holds one of two words, true or false. both is the name, and it starts with a small b. The first = sign stores. On the right of that first = sit two true or false words and one combining mark. The first word is true, every letter small, with no quotes. The combining mark is two ampersands written together with no space between them: &&. The second word is true, every letter small, with no quotes. The semicolon ends the line. This line stores one word. It does not write the screen. Storing is not writing. The screen stays blank through this line. The combining mark does not ask whether two whole numbers match. It does not make a third whole number. It looks at two answers that are already true or false, and it makes one answer from them.

Two ampersands mean both sides must be true. When the left side is true and the right side is true, the combined word is true. When either side is false, the combined word is false. boolean both = true && false; stores false. boolean both = false && true; stores false. boolean both = false && false; stores false. The file you keep uses true && true, so the stored word is true.

A combining mark is not the same work as an ask mark. The comparison page put == between two whole numbers. That ask made true or false from the numbers. This page puts && between two values that are already true or false. The operators page put + between two whole numbers to make a third whole number. && does not make a third whole number. If you write boolean both = 4 && 9;, javac writes an error on this page's lesson shape, because the sides this page keeps are true or false, not bare whole numbers. Keep true and false on both sides for the file you keep.

The writing line is the bare name: System.out.println(both);. No quotes, and no combining mark on that line. Java reads the name both, writes the word that name holds, and ends the line. For the file you keep, that word is true. The letters of the name, b, o, t, h, are not on the screen. boolean is not on the screen. The two ampersands are not on the screen. The separate true words are not on the screen as two lines. 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, both. One storing line that writes true, then two ampersands, then true, with no quotes around either word. One writing line, and that line uses the bare name. There is no match line, no total line, and no second writing line.

the file you keep

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

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. The brackets on that main line belong to args, and this page does not read args. boolean both = true && true; stores the word true. It writes nothing. System.out.println(both); writes true and ends the line. One line on the screen. No quotes. No second line. There is no boolean match line in the file you keep, and there is no line that asks whether 4 matches 4.

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. Two ampersands between two true words do not make javac write a line, and they do 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 two true words and the combining mark were already in the file. If java Mint waits, nextLine is still in the file. Delete that line and keep boolean both = true && 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 both, the writing line has quotes around the name: System.out.println("both");. Those quotes mark letters, so the stored word is not written. If the screen shows true from boolean match = 4 == 4;, the comparison file is still in place. Delete the match line. If the screen shows false, one side of && is false, which is allowed to check, and which is not the file you keep. If the screen shows true && true as letters, the writing line has quotes around those characters. You do not reinstall Java for a screen that shows the wrong line.

A screen that shows true does not, by itself, prove the combining mark. boolean both = true; also writes true. The screen can look the same. The file does not. The file you keep has true, then &&, then true. When you judge this page, look for the two ampersands between the two true words. Do not judge the file from the screen alone.

Spaces around the combining mark are allowed. boolean both = true&&true; with no spaces still stores true, and javac writes no line. boolean both = true && true; with spaces still stores true. The file you keep uses spaces around the mark, because the mark is easier to see that way. The spaces are not a second value. A space between the two ampersands is not allowed. boolean both = true & & true; makes javac write an error. Keep the two ampersands together as &&. One ampersand alone is not the mark this page keeps. boolean both = true & true; is not the file you keep. javac may write an error, or the line is not the lesson this page teaches. Write two ampersands.

Quotes around true turn it into letters. boolean both = "true" && true; asks Java to combine letters with a true or false word in a way this page does not teach. javac writes an error. Delete the quotes. Both sides of &&, on this page, are the words true or false with no quotes. System.out.println(true && true); with no both name at all also writes true. The screen can look the same. The file you keep stores the combined answer under a name first, then writes that name.

The name has to exist before a line uses it. If System.out.println(both); is written above boolean both = true && true;, javac writes an error. The writing line names both before both exists. Move the storing line above the writing line. Do not write boolean a second time for the same name. A later line can replace the word without writing boolean again, once both already exists: both = true && true;. That form is allowed. It is not required. The file you keep creates the name and stores the combined answer on one line.

When one side is false

Change only the right word. Keep the kind word boolean. Keep the name both. Keep the mark as two ampersands. boolean both = true && false; has a true left side and a false right side. Two ampersands need both sides true. The combined word is false. The writing line is still System.out.println(both);. The screen shows false. One line. The name both is not on the screen. The separate words true and false are not on the screen as two lines.

Type javac Mint.java. javac writes no line. Then type java Mint. The screen shows false. After you have seen false, put the kept file back: boolean both = true && true; and one System.out.println(both);. The kept screen is true, one line. This false run is a check. It is not the file you keep.

Two pipes need one side true

A different combining mark uses two pipe marks written together: ||. Two pipes mean at least one side must be true. boolean either = true || false; stores true. boolean either = false || true; stores true. boolean either = true || true; stores true. boolean either = false || false; stores false. The name can be either. The kind word stays boolean.

the pipe mark

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

This is the second file. It is not the file you keep. Type javac Mint.java. javac writes no line. Then type java Mint. The screen shows true. One line. It does not show false. It does not show true || false as letters. If the screen shows false, both sides may still be false, as in boolean either = false || false;. That line is allowed, and it is not this second file. After you have seen true from true || false, put the kept file back: boolean both = true && true; and System.out.println(both);.

Two ampersands and two pipes are not the same work. && is strict. Both sides must be true. || is open. One true side is enough. The file you keep teaches &&. The second file only shows ||. Do not leave || in the kept file. Do not write one pipe alone. boolean either = true | false; is not the mark this page keeps for the open combine.

A bang flips the word

A bang mark sits against one true or false word and flips it. The mark is !. boolean flipped = !true; stores false. boolean flipped = !false; stores true. There is no second side. The bang is not && and it is not ||. It does not sit between two words. It sits against one word.

the bang mark

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

This is the third file. It is not the file you keep. Type javac Mint.java. javac writes no line. Then type java Mint. The screen shows false. One line. It does not show true. It does not show !true as letters. If the screen shows true, the storing line may be boolean flipped = !false;, which flips false into true. That line is allowed, and it is not this third file. After you have seen false from !true, put the kept file back.

Writing the combining mark as letters does not combine. System.out.println("true && true"); writes the characters of that text. The screen shows true && true. That is not the word true from the kept combine. javac stays quiet. The quotes are what stop the mark from working as a mark. The file you keep has no quotes around the true words, and the writing line has no quotes around the name.

You can now store one true or false answer made from two true or false answers, and write that word. boolean both = true && true; stores true. System.out.println(both); writes true and ends the line. true && false writes false. true || false writes true. !true writes false. The next page leaves these combining marks on their own. It looks at if and else, which choose a path from a true or false answer.

The screen is the combined word

The file you keep writes true. The name on the writing line is both, and both is not on the screen. The two ampersands are in the file, and they are not on the screen. The two true words are in the file, and they are not on the screen as separate lines. The pipe mark and the bang mark are not the file you keep. Quotes around the mark write letters, not the combined word.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

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

❓
6
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