The Artima Developer Community
Sponsored Link

Java Buzz Forum
Java Tutorial : Java Object Class(equals method override -book)

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Ram N

Posts: 2777
Nickname: ramram
Registered: Jul, 2014

Ram N is Java Programmer
Java Tutorial : Java Object Class(equals method override -book) Posted: Feb 4, 2016 1:50 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Ram N.
Original Post: Java Tutorial : Java Object Class(equals method override -book)
Feed Title: JAVA EE
Feed URL: http://ramj2ee.blogspot.com/feeds/posts/default?alt=rss
Feed Description: This blog has viedo tutorials and Sample codes related to below Technologies. 1.J2EE 2.Java 3.Spring 4.Hibernate 5.Database 6.Oracle 7.Mysql 8.Design Patterns
Latest Java Buzz Posts
Latest Java Buzz Posts by Ram N
Latest Posts From JAVA EE

Advertisement

Click here to watch in Youtube :
https://www.youtube.com/watch?v=7_s5ZAEU_2E&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
Java Tutorial : Java Object Class(equals method override -book)
Java Tutorial : Java Object Class(equals method override -book)
Book.java
public class Book
{
private String name;
private String isbnNumber;

public Book(String name, String isbnNumber)
{
super();
this.name = name;
this.isbnNumber = isbnNumber;
}

public String getName()
{
return name;
}

public void setName(String name)
{
this.name = name;
}

public String getIsbnNumber()
{
return isbnNumber;
}

public void setIsbnNumber(String isbnNumber)
{
this.isbnNumber = isbnNumber;
}

@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result
+ ((isbnNumber == null) ? 0 : isbnNumber.hashCode());
return result;
}

@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Book other = (Book) obj;
if (isbnNumber == null)
{
if (other.isbnNumber != null)
return false;
}
else if (!isbnNumber.equals(other.isbnNumber))
return false;
return true;
}
}
ObjectClassTest.java
public class ObjectClassTest
{

public static void main(String[] args)
{
Book childrenStoryBook = new Book("Children Story",
"2000");
Book KidStoryBook = new Book("Kids Story", "2000");

System.out.println("childrenStoryBook.hashCode = "+childrenStoryBook.hashCode());
System.out.println("KidStoryBook.hashCode = "+KidStoryBook.hashCode());

/*
* This program displays objects are equal even
* though childrenStoryBook and KidStoryBook
* reference two distinct objects. They are
* considered equal because the objects compared
* contain the same ISBN number.
*/

if (childrenStoryBook.equals(KidStoryBook))
{
System.out.println("objects are equal");
}
else
{
System.out.println("objects are not equal");
}

}

}
Output
childrenStoryBook.hashCode = 1537245
KidStoryBook.hashCode = 1537245
objects are equal
Click the below link to download the code:
https://sites.google.com/site/javaee4321/java/ObjectClassDemo_equals_override_book_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/ObjectClassDemo_equals_override_book_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/f82b5a337c881ba48c0d1163c2b3b206ee915253/BasicJava/ObjectClassDemo_equals_override_book_App/?at=master

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Read: Java Tutorial : Java Object Class(equals method override -book)

    Topic: Java Tutorial : Java Object Class - Playlist Previous Topic   Next Topic Topic: Citrine borrows from Ruby, JavaScript, C for object-oriented programming

    Sponsored Links



    Google
      Web Artima.com   

    Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use