Java Programming Idioms
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 look into the less obvious
limitations of some concepts and discuss some fundamental, yet typical
misunderstandings.
Due to the time frame the discussion cannot be exhausting and we will
focus on two language features that exist in Java since its advent, namely
the implications of reference semantics on object copying and comparison
and the use of exceptions for error indication and error handling.
COPYING AND COMPARISON.
Regarding
object copying and comparison in Java we examine the semantics of clone()
and equals(). Questionable implementations of these functions can be found
galore (in the JDK, in popular textbooks, even in commercial applications).
It turns out that correct and symmetric implementation of these two functions
is a non-trivial task. We will look into the details.
EXCEPTION HANDLING.
Use
of exceptions is mandatory in Java and cannot be avoided, yet in practice
they are often misunderstood. We explain why and when one would want to
handle exceptions at all (a common misunderstanding is that every potential
exception must be caught.) We discuss common problems such as exception
hiding, improper use of throws specification, and resource leaks and inconsistent
object state. |
|
Copying and Comparison of Objects (ca. 90 min)
Recap of reference semantics vs. value semantics
The semantic meaning of equals()
The problem of asymmetric implementations in class hierarchies
Discussion of conceivable solutions
The semantic meaning of clone()
Shallow vs. deep copying
Discussion of various solutions and related issues
Exception Handling (ca. 90 min)
Recap of the language means
finally and its impact on control flow
Problematic throws clause
Hiding of exceptions
Maintaining valid object state and avoiding resource leaks
Reasons for catching and handling an exception
|
|
PREREQUISITES
|
|
Level:
|
intermediate |
Duration:
|
1.5 - 3.5 hours |
Prerequisites:
|
Basic knowledge of Java. |
Presented at:
|
OOP 2000
,
Munich, Germany, Janauray 2000
DevWeek Europe
, London,
UK, February 2000
TOOLS USA
, Santa
Barbara, CA, August 2000
TOOLS Europe 2001
,
Zürich, CH, March 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
|
Paper
|
|