The `else` statement runs a block of code when the condition in the `if` statement is false. It is used as a catch-all when none of the previous conditions are true.
boolean isRaining = false; if (isRaining) { System.out.println("Take an umbrella."); } else { System.out.println("No need for an umbrella."); } // Prints: No need for an umbrella
`else if` statements are used to check multiple conditions one by one. As soon as one condition is true, its code block runs and the rest are skipped.
int temperature = 75; if (temperature >= 90) { System.out.println("It's very hot."); } else if (temperature >= 70) { System.out.println("It's warm."); } else if (temperature >= 50) { System.out.println("It's cool."); } else { System.out.println("It's cold."); } // Prints: It's warm.
An `if` statement checks a condition and runs code only if the condition is true. You can have multiple `else if` statements to check additional conditions.
if (5 > 3) { System.out.println("5 is greater than 3."); } // Prints: 5 is greater than 3.
Nested conditionals are `if` statements inside other `if` statements. The outer `if` is checked first, and if it's true, the inner `if` is checked next.
boolean hasMoney = true; boolean wantsToBuyIceCream = true; if (hasMoney) { System.out.println("You have money!"); if (wantsToBuyIceCream) { System.out.println("You can buy ice cream!"); } else { System.out.println("Maybe next time."); } } // Prints: You have money! // Prints: You can buy ice cream!
The `NOT` operator `!` inverts the value of a boolean expression. If the expression is true, `!` makes it false, and vice versa.
boolean isWeekend = true; System.out.println(!isWeekend); // Prints: false
The `AND` operator `&&` checks if both conditions are true. If both are true, the result is true; otherwise, it's false.
boolean isSunny = true; boolean isWarm = true; System.out.println(isSunny && isWarm); // Prints: true
The `OR` operator `||` checks if at least one of the conditions is true. If at least one is true, the result is true.
boolean isWeekend = false; boolean isHoliday = true; System.out.println(isWeekend || isHoliday); // Prints: true
When using multiple logical operators, the order of evaluation is important: Parentheses first, then `NOT`, then `AND`, and finally `OR`.
boolean result = true && (!false || true); // true /* First, evaluate inside parentheses: (!false || true). Then, !false becomes true, making it (true || true). Finally, true && true results in true. */
You can compare object references using `==` to check if they point to the same object. For comparing values, use `equals()` method.
String a = new String("hello"); String b = new String("hello"); System.out.println(a == b); // Prints: false (different objects) System.out.println(a.equals(b)); // Prints: true (same value)
Comparing primitive values (like numbers or characters) is straightforward using `==` or `!=` to check if they are equal or not.
int x = 10; int y = 20; System.out.println(x == y); // Prints: false System.out.println(x != y); // Prints: true
Aliases refer to multiple references pointing to the same object. You can compare these references using `==`.
String a = new String("apple"); String b = new String("banana"); String c = b; System.out.println(a == b); // Prints: false System.out.println(b == c); // Prints: true
You can check if an object reference is `null` to avoid errors when trying to access its methods or properties.
String name = null; if (name != null && name.contains("a")) { System.out.println(name + " contains an 'a'."); } else { System.out.println("No 'a' found or name is null."); } // Prints: No 'a' found or name is null.
You can define your own `equals` method in a class to compare objects based on their attributes instead of their reference.
class Person { String name; int age; public Person(String name, int age) { this.name = name; this.age = age; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null || getClass() != obj.getClass()) return false; Person person = (Person) obj; return age == person.age && name.equals(person.name); } } Person p1 = new Person("Alice", 30); Person p2 = new Person("Alice", 30); System.out.println(p1.equals(p2)); // Prints: true
DeMorgan's Laws help simplify complex boolean expressions by transforming `AND` to `OR` and vice versa.
int x = 4; int y = 5; boolean result1 = !(x > y && x == y); // Rewritten as: ! (x > y) || !(x == y) boolean result2 = !(x < y || x != y); // Rewritten as: ! (x < y) && !(x != y)
You can use DeMorgan's Laws to rewrite boolean expressions into simpler forms.
int a = 1; int b = 2; boolean exp1 = !(a == b && b >= a); boolean exp2 = !(a == b) || !(b >= a); boolean exp3 = a != b || a < b; System.out.println(exp1); // Prints: true System.out.println(exp2); // Prints: true System.out.println(exp3); // Prints: true
Welcome to our comprehensive collection of programming language cheatsheets! Whether you're a seasoned developer or a beginner, these quick reference guides provide essential tips and key information for all major languages. They focus on core concepts, commands, and functions—designed to enhance your efficiency and productivity.
ManageEngine Site24x7, a leading IT monitoring and observability platform, is committed to equipping developers and IT professionals with the tools and insights needed to excel in their fields.
Monitor your IT infrastructure effortlessly with Site24x7 and get comprehensive insights and ensure smooth operations with 24/7 monitoring.
Sign up now!