In Java, an object is an instance of a class. A class is a blueprint or template for creating objects, and it defines the properties and methods of the objects created from it.The Object class is defined in the java.lang package and it is automatically imported in all Java programs.
An object has three characteristics:
State: The data stored in the object, also known as its properties or attributes.
Identity: A unique value that identifies the object, also known as its memory address.
Behavior: The actions that the object can perform, also known as its methods.
To know more about Object toString in java read the complete article.
Object equals() method.
In Java, the equals method is used to determine if two objects are equal. The method is defined in the Object class, which is the superclass of all classes in Java. By default, the equals method compares the memory addresses of the two objects, which is not usually what is desired.
When overriding the equals method, the following guidelines should be followed:
The equals method should be reflexive: for any non-null reference value x, x.equals(x) should return true.
The equals method should be symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
Returns true if this object is the same as the obj argument; false otherwise.
Notes:The equals method for class Object implements the most discriminating possible equivalence relation on objects
The merge() method in Java’s HashMap class takes three parameters:
key: The key of the entry you want to insert or merge. It is of type K, where K is the type of keys in the HashMap.
value: The value of the entry you want to insert or merge. It is of type V, where V is the type of values in the HashMap.
remappingFunction: A BiFunction that takes in the current value associated with the key and the new value that you are trying to insert or merge. This function is used to compute the new value for the key. The function takes two parameters of type V and returns a value of type V.
Let’s look at a object-related Java program where the Java object equalsMethod is used to perform an operation on the given object.
class Main {
public static void main(String[] args) {
// equals() with String objects
// create objects of string
String obj1 = new String();
String obj2 = new String();
// check if obj1 and obj2 are equal
System.out.println(obj1.equals(obj2)); // true
// assign values to objects
obj1 = "PrepInsta ";
obj2 = "PrepInsta Prime";
// again check if obj1 and obj2 are equal
System.out.println(obj1.equals(obj2)); // false
}
}
Output
true
false
Explanation:In this example, two String objects, obj1 and obj2, are created without any values assigned to them. Then the program uses the equals() method to check if the two objects are equal. Since both objects were created without any values assigned, they are equal, so the equals() method returns true.
Then, the program assigns the value "PrepInsta " to obj1 and the value "PrepInsta Prime" to obj2. The program then uses the equals() method again to check if the two objects are equal. Since the values assigned to the objects are different, the equals() method returns false.
public class Main {
public static void main(String[] args) {
// get an integer, which is an object
Integer x = new Integer(50);
// get a float, which is an object as well
Float y = new Float(50f);
// check if these are equal,which is
// false since they are different class
System.out.println("" + x.equals(y));
// check if x is equal with another int 50
System.out.println("" + x.equals(50));
}
}
Output
false
true
Explanation:In this example, Integer object x is created and assigned the value of 50 and Float object y is created and assigned the value of 50.0f. Then the program uses the equals() method to check if the x and y are equal. Since x is an Integer object and y is a Float object, they are not the same class, so the equals() method returns false for the first call.
Then, the program calls the equals() method with x and the integer value 50. In this case, the Integer class has an implementation of the equals() method that compares the int value of the Integer object to the int value passed as an argument, so the method returns true for this call.
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
HashMap map1 = new HashMap<>();
map1.put("A", 1);
map1.put("B", 2);
//map1 -> {"A": 1, "B": 2}
HashMap map2 = new HashMap<>();
map2.put("B", 3);
map2.put("C", 4);
//map2 -> {"B": 3, "C": 4}
//Merge map2 into map1
map2.forEach((key, value) -> map1.merge(key, value, Integer::sum));
//map1 -> {"A": 1, "B": 5, "C": 4}
System.out.println(map1);
}
}
Output
{A=1, B=5, C=4}
Explanation:In this example, the forEach() method is used to iterate over the entries of map2, and for each entry, the merge() method is called on map1, passing in the key and value of the entry. The Integer::sum function is used to compute the new value for the key. If the key already exist in the map1, the function will merge the value by adding the existing value and the new value , otherwise it will insert the new key value pair.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription