3.7 Comparing Objects
short-circuited evaluation:
expression can be determined by only looking at the first operand.
function isEven(num) {
return num % 2 === 0;
}
function isPositive(num) {
return num > 0;
}
let number = 10;
// Short-circuit with &&
if (isEven(number) && isPositive(number)) {
console.log(number + " is even and positive.");
} else {
console.log(number + " does not meet the criteria.");
}
Short-Circuit Behavior:
- && (Logical AND): The expression isEven(number) && isPositive(number) is evaluated.
- JavaScript first evaluates isEven(number). - If this returns false, the whole expression will short-circuit, and isPositive(number) will not be evaluated.
- If isEven(number) is true, then isPositive(number) is evaluated.
Comparing Objects
In java there are two different methods to compare objects but there is a difference between the == operator and the equals() method.
## == operator
This operator verifies if two references refer to the same object in memory, without evaluating the objects' values or attributes.
Integer num1 = 100;
Integer num2 = 100;
Integer num3 = num1;
Integer num4 = new Integer(100);
System.out.println(num1 == num3); // true (same reference)
System.out.println(num1 == num2); // true (cached integers between -128 and 127)
System.out.println(num1 == num4); // false (different references)
This compares the integer as num1 == num3 because they both equal the same integer so it's true. num1 == num2 because when they are both assigned the value 100 they point to the same reference so they become true. num1 == num4 even though they have the same values they are different because it's a new integer so it becomes false because they don't have the same reference point.
equals() method
This method checks the values or attributes of two objects. In custom classes, it is commonly overridden to offer a meaningful comparison based on the class’s specific attributes. It focuses on value rather reference points.
Integer num1 = 100;
Integer num2 = 100;
Integer num3 = new Integer(100);
System.out.println(num1.equals(num2)); // true (same value)
System.out.println(num1.equals(num3)); // true (same value)
This compares the values by their objects, not their references. num1.equals(num2) checks if the values are the same between the 2 but since they both have a value of 100 they are equal so it becomes true. This ignores if the objects have the same or different reference point. num1.euals(num3) even though it has a new integer and it’s different from num1 they are still the same because they have the same value so it makes it true.
Popcorn hack
Would the sharons house and my house be the same?
class House {
private String color;
private int size;
public House(String color, int size) {
this.color = color;
this.size = size;
}
// Override equals method to compare House objects by content
@Override
public boolean equals(Object obj) {
if (this == obj) return true; // Check if same object reference
if (obj == null || getClass() != obj.getClass()) return false; // Check if same class
House house = (House) obj;
return size == house.size && color.equals(house.color); // Compare attributes
}
}
public class Main {
public static void main(String[] args) {
House myHouse = new House("green", 150);
House anikasHouse = new House("green", 150);
House sharonsHouse = new House("green", 150);
// Correct comparison using equals()
System.out.println(myHouse.equals(sharonsHouse)); // This should return true
}
}
Yes since both myHouse and sharonsHouse have the same color and size, so the equals method would return true