Angelika Langer - Training & Consulting
HOME | COURSES | TALKS | ARTICLES | GENERICS | LAMBDAS | IOSTREAMS | ABOUT | CONTACT | Twitter | Lanyrd | Linkedin
HOME 


  UPCOMING
  PREVIOUS
  MATERIALS
 
GENERICS 
LAMBDAS 
IOSTREAMS 
ABOUT 
CONTACT 
Java Idioms: Copying and Comparing Objects

Java Idioms: Copying and Comparing Objects  
ABSTRACT
 
The programming language Java was introduced to the IT community as an “easy-to-learn” and “easy-to-use” language (typically referring to C++ for comparison). While this is certainly true, it turns out that even this supposedly easy language offers a lot of choices and comes with many programming idioms that must be understood thoroughly in order to produce high-quality software implemented in Java. 

In this tutorial we focus on the implications of reference semantics on object copying and comparison.  By default Java uses reference semantics; value semantics are optional and can be provided for each new class in form of implementations of clone(), equals(), and hashCode().  These functions are particularly important in case of superclasses, since no correct implementation of a subclass can be provided if the superclass already lacks support for value semantics. 
Correct and symmetric implementation of clone(), equals(), and hashCode() is a non-trivial task. Questionable implementations of these functions can be found galore (in the JDK, in popular textbooks, even in commercial applications). In this tutorial we go into the details of correct and complete implementations.

 
 
OUTLINE
 
Introduction
  • Recap of reference semantics vs. value semantics
Implementing equals()
  • The semantic meaning of equals() 
  • The problem of asymmetric implementations in class hierarchies
  • Discussion of conceivable solutions
  • Implementing equals() using reflection
Implementing hashCode()
    A corresponding implementation of hashCode()
Implementing clone()
  • The semantic meaning of clone()
  • Shallow vs. deep copying 
  • Cloning objects in generic collections
  • Problems with base classes that do not support clone()
  • final fields and clone()
  • Inner classes and clone() 
    •  
 
 
PREREQUISITES

 
Level: intermediate
Duration: 3 hours
Prerequisites: Attendants should be familiar with the basic concepts of Java.
Presented at: DevWeek 2001 , London, UK, February 2001

 
 

If you are interested to hear more about this and related topics you might want to check out the following seminar or skim through some further reading:
Seminars
Effective Java
4-day seminar (open enrollment and on-site)
 
Paper
Really Understanding Java -
Myths and Truths Comparing C++ and Java
short white paper (providing first insights)
 

 
  © Copyright 1995-2005 by Angelika Langer.  All Rights Reserved.    URL: < http://www.AngelikaLanger.com/Conferences/Abstracts/CloneAndEquals.htm  last update: 7 Nov 2005