Unit 2.6: Comparing Objects
Equivalent Boolean Expressions
Two Boolean expressions are equivalent if they evaluate to the same result for all possible values. For example, !(a == b) is equivalent to a != b.
De Morgan's Laws
De Morgan's Laws provide a way to find the logical equivalent of a negated compound expression.
- De Morgan's Laws state that the negation of a conjunction is the disjunction of the negations, and vice-versa. Effectively: "Flip the operands and the operator inside the parentheses."
!(a && b)is equivalent to!a || !b!(a || b)is equivalent to!a && !b
Applying De Morgan's Laws
Task: Identifying equivalent logic using De Morgan's Laws.
boolean a = true;
boolean b = false;
// Case 1: !(a && b) is the same as !a || !b
System.out.println(!(a && b)); // prints true
System.out.println(!a || !b); // prints true
// Practical Example: Check if a score is INVALID (valid range is 0 to 100)
int score = 105;
// Option A: Negate the valid range
if (!(score >= 0 && score <= 100)) {
System.out.println("Invalid (Option A)");
}
// Option B: Apply De Morgan's to logic (score < 0 OR score > 100)
if (score < 0 || score > 100) {
System.out.println("Invalid (Option B)");
}
Comparing Objects
Using == on reference types (like String) compares their memory addresses, not their contents. It checks if two references point to the exact same object.
Reference vs. Content Comparison
It's crucial to understand when to use == versus .equals().
Task: Comparing interning versus new object construction.
String s1 = "Hello";
String s2 = "Hello";
String s3 = new String("Hello");
// s1 and s2 both point to the same "interned" string literal in memory.
System.out.println(s1 == s2); // true
// s3 is a completely NEW object in memory, even though content is same.
System.out.println(s1 == s3); // false
// .equals() checks the CONTENT, so it is true for all.
System.out.println(s1.equals(s3)); // true
Task: Visualizing aliasing with reference assignment.
String a = new String("Apple");
String b = new String("Apple");
String c = a; // c now points to the SAME object as a (aliasing)
System.out.println(a == b); // false (different objects)
System.out.println(a == c); // true (same object reference)
System.out.println(a.equals(b)); // true (same content)
Comparing with null
You can use == or != to check if a reference variable is null, meaning it does not point to any object in memory. This is a common way to prevent NullPointerException errors.
Task: Using == and != for safe null checks.
public class NullCheck {
public static void main(String[] args) {
String myString = null;
if (myString == null) {
System.out.println("The string reference is null.");
}
myString = "Hello";
if (myString != null) {
System.out.println("The string is no longer null.");
}
}
}
The .equals() Method
To compare the contents of two objects, you must use the .equals() method. For Strings, this method checks if the sequences of characters are identical.
Exclusion Statement: Overriding the
equalsmethod is outside the scope of the AP Computer Science A course and exam.
Task: Comparing character sequences with .equals().
class StringComparison {
public static void main(String[] args) {
String firstString = "java";
String secondString = new String("java");
// '==' compares memory addresses. This is usually false for different objects.
System.out.println("Using == : " + (firstString == secondString));
// '.equals()' compares the actual character content. This is true.
System.out.println("Using .equals() : " + (firstString.equals(secondString)));
}
}
Note: Always use .equals() to compare the content of Strings and other objects. Use == only to check if two references point to the exact same object in memory.