What is Instance, Empty Constructor in Java

  • Context: Comp Sci 
  • Thread starter Thread starter tking88
  • Start date Start date
  • Tags Tags
    Empty Java
Click For Summary
SUMMARY

The discussion focuses on the behavior of the `equals` method in Java, specifically when comparing instances of a custom class, `Teacher`. The default implementation of `equals` compares object references, leading to a return value of false when comparing two distinct `Teacher` objects with the same name. In contrast, the `String` class has a specific implementation of `equals` that compares the actual string values, resulting in true for identical strings. To achieve the expected behavior when comparing custom objects, it is essential to override the `equals` method in the class definition.

PREREQUISITES
  • Understanding of Java object-oriented programming concepts
  • Familiarity with the `equals` method and its default behavior
  • Knowledge of how to override methods in Java
  • Basic understanding of instance variables and constructors in Java
NEXT STEPS
  • Learn how to override the `equals` method in Java classes
  • Explore the implementation of the `hashCode` method for custom objects
  • Study Java object comparison best practices
  • Investigate the use of `instanceof` in type checking within overridden methods
USEFUL FOR

Java developers, software engineers, and computer science students looking to deepen their understanding of object comparison and method overriding in Java.

tking88
Messages
2
Reaction score
0
What is Instance, Empty Constructor ... in Java

How come will return false I put in 2 same name it should return true ?

Code:
Teacher p1= new Teacher("Nelson"); 
System.out.println(p1); 
Teacher p2= new Teacher("Nelson"); 
System.out.println(p2); 

System.out.println(p1.equals(p2));

it print out
Code:
false


I do testing on below it give me ture how come the above not give me true

Code:
p1 = "Nelson";
p2 = "Nelson";
System.out.println(p1.equals(p2));
 
Last edited:
Physics news on Phys.org


Without knowing much about Java (i.e. I could be wrong):

The equals method probably has a default implementation that just compares the two pointers and returns true or false depending on it.

so:

Code:
Teacher p1= new Teacher("Nelson"); 
System.out.println(p1); 
Teacher p2= new Teacher("Nelson"); 
System.out.println(p2);

p1.equals(p2)

actually calls (say)

Code:
bool object.equals(object p2)
{
  return p2==this;
}

Whereas... in your second example you had strings and string will have a specific equals method that compares strings as you expect.

So... write an equals that overrides the default one.
 


K I did something like this and all ok now it return the true compare value

Code:
public boolean equals(Object o)
{
System.out.println(o instanceof Moof);
System.out.println(((Moof)o).getMoofValue());
System.out.println(this.moofValue); //XXX
if ((o instanceof Moof) && (((Moof)o).getMoofValue() == this.moofValue))
{
return true;
}
else
{
return false;
}
 

Similar threads

  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K