Comparison Operators in Java
Swipe to show menu
You'll start teaching Java how to compare values and make simple decisions based on them. Comparison operators let Java ask simple questions and return either true or false — a result type we already know as boolean.
Operators
>— greater than: checks if the left value is bigger than the right;<— less than: checks if the left value is smaller than the right;>=— greater than or equal: checks if the left value is bigger or exactly equal to the right;<=— less than or equal: checks if the left value is smaller or exactly equal to the right;==— equal to: checks if two values are exactly the same;!=— not equal: checks if two values are different.
Example
Main.java
12345678910111213141516171819202122232425262728293031323334353637383940package com.example; public class Main { public static void main(String[] args) { int lattePrice = 6; int cappuccinoPrice = 5; boolean result; // Greater than (>) result = lattePrice > cappuccinoPrice; System.out.println("latte > cappuccino: " + result); // Less than (<) result = lattePrice < cappuccinoPrice; System.out.println("latte < cappuccino: " + result); // Greater than or equal (>=) result = lattePrice >= 6; System.out.println("latte >= 6: " + result); // Less than or equal (<=) result = cappuccinoPrice <= 5; System.out.println("cappuccino <= 5: " + result); // Equal to (==) result = lattePrice == cappuccinoPrice; System.out.println("latte == cappuccino: " + result); // Not equal (!=) result = lattePrice != cappuccinoPrice; System.out.println("latte != cappuccino: " + result); // String comparison String customerName = "Anna"; result = customerName == "Anna"; System.out.println("customerName == Anna: " + result); } }
You declare two prices and a boolean result variable, then apply each comparison operator one by one. Each operator asks a yes/no question about the values and stores true or false into result, which is then printed. The last example shows that comparison operators also work with String values.
Сomparing Strings correctly requires a different approach — you'll learn how in the Strings section.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Comparison Operators in Java
You'll start teaching Java how to compare values and make simple decisions based on them. Comparison operators let Java ask simple questions and return either true or false — a result type we already know as boolean.
Operators
>— greater than: checks if the left value is bigger than the right;<— less than: checks if the left value is smaller than the right;>=— greater than or equal: checks if the left value is bigger or exactly equal to the right;<=— less than or equal: checks if the left value is smaller or exactly equal to the right;==— equal to: checks if two values are exactly the same;!=— not equal: checks if two values are different.
Example
Main.java
12345678910111213141516171819202122232425262728293031323334353637383940package com.example; public class Main { public static void main(String[] args) { int lattePrice = 6; int cappuccinoPrice = 5; boolean result; // Greater than (>) result = lattePrice > cappuccinoPrice; System.out.println("latte > cappuccino: " + result); // Less than (<) result = lattePrice < cappuccinoPrice; System.out.println("latte < cappuccino: " + result); // Greater than or equal (>=) result = lattePrice >= 6; System.out.println("latte >= 6: " + result); // Less than or equal (<=) result = cappuccinoPrice <= 5; System.out.println("cappuccino <= 5: " + result); // Equal to (==) result = lattePrice == cappuccinoPrice; System.out.println("latte == cappuccino: " + result); // Not equal (!=) result = lattePrice != cappuccinoPrice; System.out.println("latte != cappuccino: " + result); // String comparison String customerName = "Anna"; result = customerName == "Anna"; System.out.println("customerName == Anna: " + result); } }
You declare two prices and a boolean result variable, then apply each comparison operator one by one. Each operator asks a yes/no question about the values and stores true or false into result, which is then printed. The last example shows that comparison operators also work with String values.
Сomparing Strings correctly requires a different approach — you'll learn how in the Strings section.
Thanks for your feedback!