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

Related Questions

Hi, We provide online instructor-led training. I am looking for Java Teacher/Instructor who can teach java via online. If anyone interested then contact me. I have to start the class from tomorrow onwards.
Hi, If you haven't finalised yet, I am interested in being an instructor. I will be teaching the newest version of Java, Java 10. Experience - 3 yrs as Software Engineer, BTech CSE, teaching Java to school and college students since 3 yrs.
Ujjwal Rana
Which is a good Java training and placement institute for a fresher in Bangalore?
I know an institute in HYderabad which is giving 100% job guarantee or 100% fee refund. They have office in bangalore also, not sure whether they are offering this program in Bangalore. Please visit their...
Raja
0 0
5
What are the technologies to learn for a Java backend developer?
For a Java Backend Developer learn 1. Java EE or Spring Framework 2. DBMS(Database Management System) 3. Spring Boot 4. Web Services 5. ORM( Object-Relational Mapping) 6. Version Control Systems(like...
Ankita
0 0
5
Is JAVA training only for technical students or a non-technical student also may join?
Any Body can Learn Programming Language.It All depends upon the learning of basics and all.Basics actually makes the thing different.
Vikrant
0 0
6
How to handle Circular dependency in Spring?
What I would like to do in this post is suggest 3 possible techniques for dealing with this problem. 1. Refactor your code This is probably the simplest and quickest way to fix this problem. Look at...
Saroj

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

Ask a Question

Related Lessons

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...

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...

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

Why We Should Go With Java?
Java is widely used in the Desktop application, web application, mobile development, games etc. Java is an object-oriented programming language. Its follow all the oops concept Like Object, Class,...
B

Bhupendra Patidar

0 0
0

Hibernate 5
The Hibernate team published the first Hibernate 5 release quite a while ago, and since then they introduced a bunch of new features. I explained several of them here on the blog, and it’s about...
M

Mohammed Shahnawaz Akhter

2 0
0

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