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

why can't we create an object for abstract class? why wait,notify,notifyall methods were in object class but not in thread class?
The Generalized object can be a abstract class in the sense. Consider we have three object Employee, Manager, and Engineer we can establish the relationship like Manager extends Employee , Engineer extends...
Jilani
Should we learn DBMS and RDBMS without any Java training?
java or i can say any programming language is not required to learn DBMS or RDBMS
Karthik
0 0
6
While teaching IT Student (MCA / BCA / BTech), I found most of the students are not having good programming skills but they are still running behind to learn .NET / PHP / PYTHON / ANDROID / JAVA. Why is it so? What we should do better to improve a sound programming skills among most of IT Students?
on college days they r learning c,c++ and following faculties are also giving a road map of "programming means" thease languages only , even these are the languages are familier , where you justify them...
Amit Kumar
What are the difference between abstract class and Interface?
Abstraction : Hiding unnecessary details of object and shows only essential features of Object to communicate. abstract class : partially defined Object interface :Complete specification of Object class : Complete definition of Object
Ashish

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

Ask a Question

Related Lessons

Java Advantages
In this video, learn about Java and its advantages. Also, check out the difference between Java and C++, Java development kit, Java Run Time Environment (JRE) with a proper demonstration program for better clarity.

Spring - Dependency Injection (DI)
Spring - Dependency Injection (DI) DI is a framework which provides loose coupling in code. Here loose coupling means no hard coding of the object. Instead of hard coding, we will be injecting these object...

What is a Programming Language
What is a Language? Language is a communication system of human. What is a programming Language? A programming Language is a formal constructed language design to communicate...

Priority in TestNG
public class Priority { @Test (priority=1)public void login() {System.out.println("login");} @Testpublic void email1() {System.out.println("email1");} @Test (priority=-2)public void email2() {System.out.println("email2");} //I...
S

Sarthak C.

0 0
0

Android : Application Launch time improvements.
For any standard android application, below 3 components play important roles to show 1st interface to user, so that he/she can interact with the app. 1. Custom Application class : Intialize the components...

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