UrbanPro

Learn Java Training from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

why string class is override in euals()/ hashCode Hethods?

Asked by Last Modified  

15 Answers

Learn Java

Follow 0
Answer

Please enter your answer

• In the above program we compared two string using equals() method and it returns true.and comparing using == operator returns false. • Basically equal() will also return false on comparing those two strings because default functionality of equal() method is to compare references and two strings are...
read more
• In the above program we compared two string using equals() method and it returns true.and comparing using == operator returns false. • Basically equal() will also return false on comparing those two strings because default functionality of equal() method is to compare references and two strings are created using new operator so both references are different. • But String class overriding equals() method and in that equals method it comparing content of the strings and returning true if both are having same content false if not. • Lets see what happen if we compare two stringBuffer objects using equals() method. • package com.instanceofjava; • • class StringBufferEqualsDemo{ • • public static void main(String [] args){ • • StringBuffer fstr= new StringBuffer("Javatutorials"); • StringBuffer sstr= new StringBuffer("Javatutorials"); • • System.out.println(fstr.equals(sstr)); • System.out.println(fstr==sstr); • • System.out.println(fstr.hashCode()); • System.out.println(sstr.hashCode()); • } • } OUTPUT • false • false • 1704856573 • 705927765 • If you observe above java program when we are comparing two stringBuffer objects equal() method returning false even content is same. Because StringBuffer class not overriding equals() and hashcode() methods. • StringBuilder is also not overriding equals() method? lets see a program and clarify. • • package com.instanceofjava; • • class StringBuilderEqualsDemo{ • • public static void main(String [] args){ • • StringBuilder fstr= new StringBuilder("Javatutorials"); • StringBuilder sstr= new StringBuilder("Javatutorials"); • • System.out.println(fstr.equals(sstr)); • System.out.println(fstr==sstr); • • System.out.println(fstr.hashCode()); • System.out.println(sstr.hashCode()); • } • } OUTPUT 1. false 2. false 3. 1704856573 4. 705927765 5. So now its cleared that StringBuffer and StringBuilder classes not overriding equals() and hashCode() methods. 6. But Why? 7. Why StringBuffer and StringBuilder classes not overriding equals() method and hashcode() method where as String class is overriding these two methods. 8. Basically Strings are Immutable means Whenever we try to change the value of string result will be new string. So string content wont change. 9. StringBuffer main use is mutable means when we append a string to it it will add to existing object. 10. When the content changes the hashcode will changes. 11. Lets see a program on adding elements to hashmap. read less
Comments

Bcoz it is method of Object Class & String is final class which extends Object class to override equals & hashcode . equals() method checks whether the content of String is same or not. & hashcode() method returns ref address of it.
Comments

Trainer

By defining equals() and hashCode() consistently, you can improve the usability of your classes as keys in hash-based collections. As the API doc for hashCode explains: "This method is supported for the benefit of hashtables such as those provided by java.util.Hashtable."
Comments

Java developer

Not only string, even wrapper classes also overwritten these 2 methods. To store these immutable objects in a same hashbacket.
Comments

Java Trainer

Equals method and hash code methods compare two objects based the memory location occupied by objects. Two different objects always occupy two different locations in heap memory. So equals and hashCode methods are overriden in string to compare contents of two strings instead of memory location of he...
Comments

Trainer

Equals and hashcode methods are override when we check for object equality or when we want to check and restrict duplicates in hashmap .
Comments

Tutor

Because, In Object class equals method implementation was comparing two objects with '==' operator. That means if two objects(String, or any type) are referring same address location then only true, otherwise it returns false, so in String class they had override the equals and changed the method implementation....
read more
Because, In Object class equals method implementation was comparing two objects with '==' operator. That means if two objects(String, or any type) are referring same address location then only true, otherwise it returns false, so in String class they had override the equals and changed the method implementation. In current String equal method will check the String value based on that it will return the true/false with irrespective of the memory location. And it was good practice to override hashcode() whenever you override the equals(). hashcode() method was used when object was storing some collections which will follow hashing algorithm(Ex: HashMap, HashSet) For mpre information go through: http://stackoverflow.com/questions/2265503/why-do-i-need-to-override-the-equals-and-hashcode-methods-in-java read less
Comments

Computer professional

to check logical equality of objects
Comments

We may come across the requirement to check for object equality. To check whether both objects contains same properties value or not. This can be done by override the Equals method. While overriding the Equals method, you should consider the followings If object argument is null, return false ...
read more
We may come across the requirement to check for object equality. To check whether both objects contains same properties value or not. This can be done by override the Equals method. While overriding the Equals method, you should consider the followings If object argument is null, return false If type of object argument is different than type of this, return false If object argument is neither null nor its type is different check for the values of the instance fields. If they are equal return true else return false read less
Comments

Tutor

It is not always necessary to override hash code and equals. But if you think you need to override one, then you need to override both of them
Comments

View 13 more Answers

Related Questions

Can you use abstract and final both with a method? Why?
No because we have to provide an abstract method with implementation in the subclass (the child class of the parent class in which the abstract method was declared). And for providing this implementation...
Sushil
What is the difference between core java and advanced java?
core java is base for Advance java. Means whatever you want to develop it needs core java with features in Adv java
Anurag
0 0
8
How do I learn Java? From book or internet or a coaching?
According to me....u should start from basic Java..that is core Java... . Find coaching , who teaches Java for beginners.....then later u can go for advance java.
Suresh
0 0
7
What equipment do you need to teach online Java classes?
Team Viewer is the best tool for the same.
Imran
0 0
5
What is a singleton class and when do you use it?
Singleton class have a private constructor. while we can't create an object from outside of class, we can create an object only once.
Prakash

Now ask question in any of the 1000+ Categories, and get Answers from Tutors and Trainers on UrbanPro.com

Ask a Question

Related Lessons

What Is Java? Explain The History Of Java
i. Ovierview: Java programming language was originally developed by Sun Microsystems which was initiated by James Gosling and released in 1995 as core component of Sun Microsystems' Java platform (Java...

2.1. Reverse a singly linked list.
class Node { int data; Node next; Node(int data) { this.data = data; this.next = null; }} public class LinkedList { Node head; public void reverse() { Node prev = null; Node curr = head; Node next =...

Mastering in Java Programming
Core java topics What is Java?Execution Model Of JavaBytecodeHow to Get Java?A First Java ProgramCompiling and Interpreting ApplicationsThe JDK Directory StructureUsing Eclipse Data types and Variables What...

Final, Finally And Finalize In Java
Final: 1. It is a non access modifier that can be applied to variables,methods and to class. 2. Final means non-changable. Final variable: 1. If a variable is declared as final it can't be re-assigned. 2....

Java : Compile-time Versus Runtime optimization
While designing and development, one should think in terms of compile-time and run-time.It helps in understanding language basics in a better way.Let's understand this with a question below : What...
S

Recommended Articles

Designed in a flexible and user-friendly demeanor, Java is the most commonly used programming language for the creation of web applications and platform. It allows developers to “write once, run anywhere” (WORA). It is general-purpose, a high-level programming language developed by Sun Microsystem. Initially known as an...

Read full article >

Java is the most commonly used popular programming language for the creation of web applications and platform today. Integrated Cloud Applications and Platform Services Oracle says, “Java developers worldwide has over 9 million and runs approximately 3 billion mobile phones”.  Right from its first implication as java 1.0...

Read full article >

Before we start on the importance of learning JavaScript, let’s start with a short introduction on the topic. JavaScript is the most popular programming language in the world, precisely it is the language - for Computers, the Web, Servers, Smart Phone, Laptops, Mobiles, Tablets and more. And if you are a beginner or planning...

Read full article >

Java is the most famous programming language till date. 20 years is a big time for any programming language to survive and gain strength. Java has been proved to be one of the most reliable programming languages for networked computers. source:techcentral.com Java was developed to pertain over the Internet. Over...

Read full article >

Looking for Java Training Classes?

Learn from the Best Tutors on UrbanPro

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you
X

Looking for Java Training Classes?

The best tutors for Java Training Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Java Training with the Best Tutors

The best Tutors for Java Training Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more