A Java Reference:
Assorted Java Reference Material
Paul N. Hilfinger
Univer sity of California, Berkeley
Copyright
c
2001, 2002, 2004, 2006, 2008, 2011, 2012, 2013, 2014 by Paul N.
Hilfinger. All rights reserved.
Acknowledgments. Many thanks to Soheil Golshan, Ashwin Iyengar, Kegan
Kawamura, Benson Limketkai, Vadim Perelman, Barath Raghavan, Chen Tang,
Michael Yang, and Asa Daniel Zernik for finding many errors in previous editions
of this document. Remaining errors are, of course, my own.
Contents
1 Java Overview 9
1.1 Basic Program Structure . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.2 Compilation and Execution . . . . . . . . . . . . . . . . . . . . . . . 11
1.3 Simple Values and Expressions . . . . . . . . . . . . . . . . . . . . . 12
1.3.1 Writing Numbers . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.3.2 Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.3.3 Comparisons and Logical Operations . . . . . . . . . . . . . . 14
1.3.4 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
1.3.5 Static Methods: Abstracting Computation . . . . . . . . . . . 17
1.4 Conditional Execution . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1.4.1 If statements . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.4.2 Conditional Expressions . . . . . . . . . . . . . . . . . . . . . 19
1.4.3 Case analysis and the Switch Statement . . . . . . . . . . . . 19
1.5 Arrays I: The Command Line . . . . . . . . . . . . . . . . . . . . . . 21
1.6 Example: Finding Primes . . . . . . . . . . . . . . . . . . . . . . . . 22
1.6.1 Starting from the top . . . . . . . . . . . . . . . . . . . . . . 23
1.6.2 Starting from the bottom . . . . . . . . . . . . . . . . . . . . 23
1.6.3 Meeting in the middle . . . . . . . . . . . . . . . . . . . . . . 26
1.7 Example: Pig Latin . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
1.7.1 Vowels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
1.7.2 Translation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
1.7.3 Counting Consonants . . . . . . . . . . . . . . . . . . . . . . 29
1.7.4 To the top . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
1.8 Variables and Assignment . . . . . . . . . . . . . . . . . . . . . . . . 32
1.9 Repetition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
1.9.1 Indefinite iteration . . . . . . . . . . . . . . . . . . . . . . . . 35
1.9.2 Definite Iteration . . . . . . . . . . . . . . . . . . . . . . . . . 37
1.9.3 Example: Iterative Prime-Finding . . . . . . . . . . . . . . . 40
1.9.4 Example: Counting . . . . . . . . . . . . . . . . . . . . . . . 41
1.10 Arrays II : Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
1.10.1 Example: Linear Interpolation . . . . . . . . . . . . . . . . . 43
1.10.2 Example: The Sieve of Eratosthenes . . . . . . . . . . . . . . 44
1.10.3 Multi-dimensional Arrays . . . . . . . . . . . . . . . . . . . . 46
1.11 Introduction to Objects . . . . . . . . . . . . . . . . . . . . . . . . . 48
3
4 CONTENTS
1.11.1 Simple Object Type Definition and Creation . . . . . . . . . 48
1.11.2 Instance Metho ds . . . . . . . . . . . . . . . . . . . . . . . . . 50
1.11.3 Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
1.11.4 Example: A Prime-Number Class . . . . . . . . . . . . . . . . 52
1.12 Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
1.13 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
1.14 Packages and Access Control . . . . . . . . . . . . . . . . . . . . . . 60
1.15 Handling Exceptional Cases . . . . . . . . . . . . . . . . . . . . . . . 62
1.15.1 Built-in Exceptions . . . . . . . . . . . . . . . . . . . . . . . . 62
1.15.2 What Exactly is an Exception? . . . . . . . . . . . . . . . . . 62
1.15.3 Throwing Exceptions Explicitly . . . . . . . . . . . . . . . . . 63
1.15.4 Catch ing Exceptions . . . . . . . . . . . . . . . . . . . . . . . 63
2 Describing a Programming Language 65
2.1 Dynamic and Static Properties . . . . . . . . . . . . . . . . . . . . . 65
2.2 Describing Lexical Structure and Syntax . . . . . . . . . . . . . . . . 67
3 Lexical Basics 69
3.1 Layout: Whitespace and Comments . . . . . . . . . . . . . . . . . . 69
4 Values, Types, and Containers 71
4.1 Values and Containers . . . . . . . . . . . . . . . . . . . . . . . . . . 72
4.1.1 Containers and Names . . . . . . . . . . . . . . . . . . . . . . 72
4.1.2 Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
4.2 Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
4.2.1 Static vs. d y namic types . . . . . . . . . . . . . . . . . . . . . 75
4.2.2 Type denotations in Java . . . . . . . . . . . . . . . . . . . . 76
4.3 Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
4.4 Applying the model to Java . . . . . . . . . . . . . . . . . . . . . . . 77
5 Declarations 79
5.1 Scope and the Meaning of Names . . . . . . . . . . . . . . . . . . . . 80
5.1.1 Block s tructure . . . . . . . . . . . . . . . . . . . . . . . . . . 80
5.1.2 Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
5.1.3 Packages and Imports . . . . . . . . . . . . . . . . . . . . . . 82
5.2 Local Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
5.3 Type Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
5.4 Class Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
5.4.1 Kinds of class members . . . . . . . . . . . . . . . . . . . . . 88
5.4.2 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
5.4.3 Class Modifiers . . . . . . . . . . . . . . . . . . . . . . . . . . 89
5.4.4 Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
5.5 Interface Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . 91
5.6 Enumerated Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
5.7 Nested Classes and Interf aces . . . . . . . . . . . . . . . . . . . . . . 94
5.7.1 Member types . . . . . . . . . . . . . . . . . . . . . . . . . . 94
CONTENTS 5
5.7.2 Local classes . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
5.7.3 Anonymous classes . . . . . . . . . . . . . . . . . . . . . . . . 97
5.7.4 What on earth is this all for? . . . . . . . . . . . . . . . . . . 98
5.8 Method (Function) Declarations . . . . . . . . . . . . . . . . . . . . 99
5.8.1 Variable-Length Parameter Lists . . . . . . . . . . . . . . . . 100
5.8.2 Method signatures . . . . . . . . . . . . . . . . . . . . . . . . 101
5.8.3 Overloading and hiding methods . . . . . . . . . . . . . . . . 103
5.8.4 Overriding . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
5.9 Constructor Declarations . . . . . . . . . . . . . . . . . . . . . . . . 107
5.9.1 Rationale. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
5.9.2 Instance and field initializers . . . . . . . . . . . . . . . . . . 109
5.9.3 Some Useful Constructor Idioms . . . . . . . . . . . . . . . . 110
5.10 Initialization of Classes . . . . . . . . . . . . . . . . . . . . . . . . . . 112
5.11 Access control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
5.12 The Java Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
5.12.1 Executing a Program . . . . . . . . . . . . . . . . . . . . . . 114
5.13 Annotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
5.13.1 Deprecated . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
5.13.2 Override . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
5.13.3 SuppressWarnings . . . . . . . . . . . . . . . . . . . . . . . . 118
6 The Expression Language 119
6.1 Syntactic overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
6.1.1 Prefix, Postfix , and Infix Operators . . . . . . . . . . . . . . . 121
6.1.2 Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
6.2 Conversions and Casts . . . . . . . . . . . . . . . . . . . . . . . . . . 123
6.2.1 Explicit conversions . . . . . . . . . . . . . . . . . . . . . . . 123
6.2.2 Implicit conversions . . . . . . . . . . . . . . . . . . . . . . . 124
6.2.3 Promotions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
6.3 Integers and Characters . . . . . . . . . . . . . . . . . . . . . . . . . 125
6.3.1 Integral values and their literals . . . . . . . . . . . . . . . . . 126
6.3.2 Modular integer arithmetic . . . . . . . . . . . . . . . . . . . 129
6.3.3 Manipulating bits . . . . . . . . . . . . . . . . . . . . . . . . 132
6.4 Floating-Point Numbers . . . . . . . . . . . . . . . . . . . . . . . . . 135
6.4.1 Floating-Point Literals . . . . . . . . . . . . . . . . . . . . . . 136
6.4.2 Floating-point arithmetic . . . . . . . . . . . . . . . . . . . . 137
6.5 Booleans and Conditionals . . . . . . . . . . . . . . . . . . . . . . . . 141
6.5.1 Boolean literals . . . . . . . . . . . . . . . . . . . . . . . . . . 142
6.5.2 Boolean operations . . . . . . . . . . . . . . . . . . . . . . . . 142
6.5.3 Comparisons and Equality . . . . . . . . . . . . . . . . . . . . 142
6.5.4 Conditional expressions . . . . . . . . . . . . . . . . . . . . . 143
6.6 Reference Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
6.6.1 Allocating class instances . . . . . . . . . . . . . . . . . . . . 144
6.6.2 Field Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
6.6.3 Testing types . . . . . . . . . . . . . . . . . . . . . . . . . . . 147