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

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

Sarthak C. | 22/11/2021

0 0
0

Example of DependsOnMethod in TestNG
public class dependsonM { @Test public void login() { System.out.println("login"); } @Test (dependsOnMethods = {"login"}) public void email() { //Intentionally I am failing this testcase Assert.assertTrue(false);...

Sarthak C. | 18/11/2021

0 0
0

TestNG Annotations and its sequence
public class TestNGAnnotations { @BeforeMethod public void beforeM() { System.out.println("Before Method"); } @AfterMethod public void afterMethod() { System.out.println("After Method"); } @BeforeClass...

Sarthak C. | 18/11/2021

0 0
0

ListIterator Example
public class ListIteratorEx { public static void main(String args) { List<String> li = new ArrayList<String>(); li.add("java1"); li.add("java2"); li.add("java3"); li.add("java4"); ListIterator...

Sarthak C. | 18/11/2021

0 0
0

doWhile example in Java
public class doWhilePracticleEx { public void test() { } public static void main(String args) { String q1 = "Who is PM of India?"; String a1 = "Sonia Gandhi"; String a2 = "Rahul Gandhi"; String a3...

Sarthak C. | 18/11/2021

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

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 >

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 >

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

Book a Free Demo

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

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