What is the difference between equals() and “==” ?

Equals is intended to check logical equality and == checks if both references point to same object. (Thanks Sandeep) a == b;// Compares references, not values. a.equals(b);// Compares values for equality. In Core Java, the difference between equals() and == lies in their functionality and what they compare: equals() method: equals() is a method defined … Read more

What is the difference between JDK and JRE?

Java Development Kit (JDK) is the most widely used Java Software Development Kit. Java Runtime Environment (JRE) is an implementation of the Java Virtual Machine which executes Java programs. In Java development, JDK (Java Development Kit) and JRE (Java Runtime Environment) are two essential components, and they serve distinct purposes: JDK (Java Development Kit): Definition: … Read more

What is the purpose of serialization?

Serialization is the conversion of an object to a series of bytes, so that the object can be easily saved to persistent storage or streamed across a communication link. The byte stream can then be deserialised – converted into a replica of the original object. In Core Java, serialization refers to the process of converting … Read more

What if you have a specific scenario where you want to first sort by rank and then alphabetically if the ranks are same?

Any number of Comparator classes can be created to sort them differently as shown below. import java.util.Comparator;   public class JavaTechnologyComparator implements Comparator<JavaTechnology> {   @Override public int compare(JavaTechnology t1, JavaTechnology t2) {   //handle null values here Integer rank1 = t1.getRank(); Integer rank2 = t2.getRank(); int rankVal = rank1.compareTo(rank2); int nameVal = t1.getName().toLowerCase().compareTo(t2.getName().toLowerCase());   … Read more

Can you write a simple program that compares two objects to return if they are equal or not? This method will be handy in defining your own equals( ) method.

This is usefull in domain or value object class to compare different object fields in an equals method public class DomainObject {   //protected because only inheriting domain classes can use it protected boolean isPropertyEqual(Object compare1, Object compare2) { // go here if compare1 is null, i.e. test cases 1 & 3 if (compare1 == … Read more