Answers for "equals method java"

15

java == vs equals

In general both equals() and == operator in Java are used to compare 
objects to check equality but here are some of the differences between the two:

1) .equals() and == is that one is a method and other is operator.
2) We can use == operator for reference comparison (address comparison) 
and .equals() method for content comparison. 
 -> == checks if both objects point to the same memory location 
 -> .equals() evaluates to the comparison of values in the objects.
3) If a class does not override the equals method, then by default it 
uses equals(Object o) method of the closest parent class 
that has overridden this method.

// Java program to understand  
// the concept of == operator 
public class Test { 
    public static void main(String[] args) 
    { 
        String s1 = new String("HELLO"); 
        String s2 = new String("HELLO"); 
        System.out.println(s1 == s2); 
        System.out.println(s1.equals(s2)); 
    } 
} 
Output:
false
true
  
Explanation: Here we are creating two (String) objects namely s1 and s2.
Both s1 and s2 refers to different objects.
 -> When we use == operator for s1 and s2 comparison then the result is false 
 as both have different addresses in memory.
 -> Using equals, the result is true because its only comparing the 
 values given in s1 and s2.
Posted by: Guest on July-01-2020
2

override equals java

class Complex { 

	private double re, im; 

	public Complex(double re, double im) { 
		this.re = re; 
		this.im = im; 
	} 

	// Overriding equals() to compare two Complex objects 
	@Override
	public boolean equals(Object o) { 

		// If the object is compared with itself then return true 
		if (o == this) { 
			return true; 
		} 

		/* Check if o is an instance of Complex or not 
		"null instanceof [type]" also returns false */
		if (!(o instanceof Complex)) { 
			return false; 
		} 
		
		// typecast o to Complex so that we can compare data members 
		Complex c = (Complex) o; 
		
		// Compare the data members and return accordingly 
		return Double.compare(re, c.re) == 0
				&& Double.compare(im, c.im) == 0; 
	} 
} 

// Driver class to test the Complex class 
public class Main { 

	public static void main(String[] args) { 
		Complex c1 = new Complex(10, 15); 
		Complex c2 = new Complex(10, 15); 
		if (c1.equals(c2)) { 
			System.out.println("Equal "); 
		} else { 
			System.out.println("Not Equal "); 
		} 
	} 
}
Posted by: Guest on July-18-2020
0

overriding equals method in java

1.	public class Test
2.	{
3.		private int num;
4.		private String data;
5.
6.		public boolean equals(Object obj)
7.		{
8.			if(this == obj)
9.				return true;
10.			if((obj == null) || (obj.getClass() != this.getClass()))
11.				return false;
12.			// object must be Test at this point
13.			Test test = (Test)obj;
14.			return num == test.num &&
15.			(data == test.data || (data != null && data.equals(test.data)));
16.		}
26.		// other methods
27.	}
Posted by: Guest on December-08-2020
4

equals example java

import java.util.Scanner;

public class YourProjectName {

	public static void main(String[] args) {
		Scanner keyboard = new Scanner(System.in);
		
		//D
		
		String password1;
		String password2;
		String msg;
		
		//E
		
		System.out.println("Enter password: ");
			password1 = keyboard.nextLine();
		
		System.out.println("Repeat password: ");
			password2 = keyboard.nextLine();
		
		//P
		
		if (password1.equals(password2)) {
			msg = "Password matching!";
		} else {
			msg = "Password not match";
		}
		
		//S
		
		System.out.println(msg);
		
	}

}
Posted by: Guest on October-15-2020
0

java equal method

if(bob.equals("Sam")){ // Checks if strings are equal.
  // code
}
Posted by: Guest on March-04-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language