//java architect /
Conclusion
We conducted an empirical
study to investigate the use
of covariant arrays in real-world Java programs. Our
corpus was composed of 64
open source programs from
the Qualitas Corpus, ranging from games to system
software and libraries. As we
have shown with the help of
an example, covariant arrays
in Java can lead to type
errors that are not caught
by the compiler but cause
runtime exceptions.
In our empirical study, we found relatively few uses of covariance given the
large size of our corpus: 324 covariant
array uses in more than 5 million lines of
code. More importantly, almost 75 percent of these uses were caused by calls to
Java library methods. This demonstrates
that covariant arrays are used very infrequently by Java developers in practice.
Since Java SE 5, generics and wildcards provide a type-safe alternative to
covariant arrays. All of the uses of covariant arrays we found in our corpus could
be transformed and made completely
type safe using these Java features.
The existence of generics as a better
and safer solution and the infrequent
use of covariant arrays in Java lead us to
suggest that covariant arrays should not
be included in future programming languages and should be avoided whenever
possible in Java development.
We intend to extend our work by
investigating the use of wildcards in
Figure 3
DefaultTableModel
Arrays.toString
JTable()
JList.setListData
Arrays.equals
Arrays.binarySearch
JList()
JComboBox()
Arrays.sort
Vector.copyInto
DefaultComboBoxMode
JOptionPane
(Vector/List).toArray
34
17
15
8
6
4
3
3
2
2
1
1
146
COMMUNITY
JAVA IN ACTION
Java programs. Wildcards and generics
provide a better alternative to covariant arrays, and we are interested to
see how frequently they are used in
practice. </article>
Register Now
We would like to thank Alan Mycroft
and Luke Church for their encouragement and useful advice and suggestions
during our work. We would like to thank
our wider research group, the Cambridge
Programming Research Group, as well
as attendees of our talk at the FOSDEM
conference for valuable feedback and
suggestions. Further, Raoul-Gabriel
Urma wishes to thank Qualcomm and
Janina Voigt wishes to thank Rutherford
Foundation of the Royal Society of New
Zealand for supporting this work through
their studentships.
SAVE $400
With Early Registration
ABOUT US
blog
LEARN MORE
;; Types and Programming Languages by
Benjamin Pierce (MIT Press, 2002)
47
ORACLE.COM/JAVAMAGAZINE /////////////////////////////////////////////// MAY/JUNE 2012