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

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

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

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

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

• 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

Core Java Tutor

Every java defined class is overriding equals and hashCode method of the object class. Equals method provides the way to equate 2 string objects.. If you see the implementation of equals method of String class, you will find character by character matching of string characters... hashCode method of Object...
read more
Every java defined class is overriding equals and hashCode method of the object class. Equals method provides the way to equate 2 string objects.. If you see the implementation of equals method of String class, you will find character by character matching of string characters... hashCode method of Object class is a native method.. hashCode implemetation provides the way to reach the specified object on heap.. read less
Comments

Tech Mentor

Java allows two ways to compare objects. 1) equals 2) ==. Both are different and in case of String, its very much different. Two string needs to be compared based on equals and hashcode method meaning there state is same i.e. there value and not just where the pointer is in the heap space allocated by...
read more
Java allows two ways to compare objects. 1) equals 2) ==. Both are different and in case of String, its very much different. Two string needs to be compared based on equals and hashcode method meaning there state is same i.e. there value and not just where the pointer is in the heap space allocated by java. String class does not give dfeault implementations for equals and hashcode and hence we need to override it. Equals and hashcode also leads to faster searching where hashing method is used for searching e.g. collection classes. read less
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

IT Trainer

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

View 13 more Answers

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

Ask a Question

Related Lessons

How to create Rest web services in Java
Web services are web application components that lets two different applications to communicate over the network.Let if an application which in written java provides web services can be communicated through...

Software Testing
Software testing is a process of executing a program or application with the intent of finding the software bugs. It can also be stated as the process of validating and verifying that a software program...

Try to clear up the basics, if basics are clear then you can go ahead with any difficult problem
Hey guys, To all the students i just want to convey that just clear up your basics so that they can help you solve anyu problem and you would achieve a great success. Regards, Ishani Chakraborty

Introduction to Programming Languages
What is a Programming Language? A programming language is a formal computer language or constructed language designed to communicate instructions to a machine, particularly a computer. Programming languages...

History Of Java
Java history is interesting to know. The history of java starts from Green Team. Java team members (also known as Green Team), initiated a revolutionary task to develop a language for digital devices such...
V

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