A Java Reference: Assorted Java Reference Material

[Pages:264]A Java Reference: Assorted Java Reference Material

Paul N. Hilfinger University of California, Berkeley

Copyright c 2001, 2002, 2004, 2006, 2008, 2011, 2012, 2013, 2014, 2018 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 Methods . . . . . . . . . . . . . . . . . . . . . . . . . 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 Catching 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. dynamic 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 structure . . . . . . . . . . . . . . . . . . . . . . . . . . 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 Interfaces . . . . . . . . . . . . . . . . . . . . . . 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

6

CONTENTS

6.6.4 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 6.6.5 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 6.7 Assignment, Increment, and Decrement Operators . . . . . . . . . . 154 6.8 Method Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 6.8.1 Static method calls . . . . . . . . . . . . . . . . . . . . . . . . 157 6.8.2 Instance Method Calls . . . . . . . . . . . . . . . . . . . . . . 158 6.9 Constant Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . 160 6.10 Reflection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 6.10.1 Type java.lang.Class . . . . . . . . . . . . . . . . . . . . . . . 161 6.11 Definite Assignment and Related Obscure Details . . . . . . . . . . . 163 6.11.1 Assignments to fields . . . . . . . . . . . . . . . . . . . . . . . 164 6.11.2 Blank final variables . . . . . . . . . . . . . . . . . . . . . . . 165

7 Statements

167

7.1 Sequencing, Blocks, and Empty Statements . . . . . . . . . . . . . . 168

7.2 Conditional Control . . . . . . . . . . . . . . . . . . . . . . . . . . . 169

7.2.1 If Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . 169

7.2.2 Switch Statements . . . . . . . . . . . . . . . . . . . . . . . . 170

7.3 About Statement Format . . . . . . . . . . . . . . . . . . . . . . . . 173

7.4 Iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174

7.4.1 While Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . 174

7.4.2 Traditional For Loops . . . . . . . . . . . . . . . . . . . . . . 175

7.4.3 For Loops for Collections and Arrays . . . . . . . . . . . . . . 178

7.5 Jump Statements and Labels . . . . . . . . . . . . . . . . . . . . . . 179

7.5.1 The `break' Statement . . . . . . . . . . . . . . . . . . . . . . 180

7.5.2 The `continue' Statement . . . . . . . . . . . . . . . . . . . . 182

7.5.3 The `return' Statement . . . . . . . . . . . . . . . . . . . . . 183

8 Exception Handling

185

8.1 Throw Statements and Exception Types . . . . . . . . . . . . . . . . 186

8.2 Catching Exceptions: Try Statements . . . . . . . . . . . . . . . . . 186

8.2.1 Simple example: single handler . . . . . . . . . . . . . . . . . 188

8.2.2 Multiple handlers. . . . . . . . . . . . . . . . . . . . . . . . . 189

8.2.3 Using the Caught Exception. . . . . . . . . . . . . . . . . . . 189

8.2.4 The Finally Clause . . . . . . . . . . . . . . . . . . . . . . . . 190

8.3 Declaring Thrown Exceptions . . . . . . . . . . . . . . . . . . . . . . 191

8.4 Exceptions in the java.lang Package . . . . . . . . . . . . . . . . . . 194

9 Strings, Streams, and Patterns

197

9.1 Bytes and Characters . . . . . . . . . . . . . . . . . . . . . . . . . . 198

9.1.1 ASCII and Unicode . . . . . . . . . . . . . . . . . . . . . . . 198

9.1.2 The class Character . . . . . . . . . . . . . . . . . . . . . . . 201

9.2 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201

9.2.1 Constructing Strings . . . . . . . . . . . . . . . . . . . . . . . 202

9.2.2 String Accessors . . . . . . . . . . . . . . . . . . . . . . . . . 207

CONTENTS

7

9.2.3 String Comparisons, Tests, and Searches . . . . . . . . . . . . 209 9.2.4 String Hashing . . . . . . . . . . . . . . . . . . . . . . . . . . 210 9.3 StringBuilders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210 9.4 Readers, Writers, Streams, and Files . . . . . . . . . . . . . . . . . . 211 9.4.1 Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 9.4.2 Readers and InputStreams . . . . . . . . . . . . . . . . . . . 217 9.4.3 Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218 9.4.4 PrintStreams and PrintWriters . . . . . . . . . . . . . . . . . 218 9.5 Regular Expressions and the Pattern Class . . . . . . . . . . . . . . 219 9.6 Scanner . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223

10 Generic Programming

227

10.1 Simple Type Parameters . . . . . . . . . . . . . . . . . . . . . . . . . 228

10.2 Type Parameters on Methods . . . . . . . . . . . . . . . . . . . . . . 230

10.3 Restricting Type Parameters . . . . . . . . . . . . . . . . . . . . . . 231

10.4 Wildcards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232

10.5 Generic Programming and Primitive Types . . . . . . . . . . . . . . 232

10.6 Formalities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234

10.7 Caveats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236

11 Multiple Threads of Control

239

11.1 Creating and Starting Threads . . . . . . . . . . . . . . . . . . . . . 242

11.2 A question of terminology . . . . . . . . . . . . . . . . . . . . . . . . 243

11.3 Synchronization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245

11.3.1 Mutual exclusion . . . . . . . . . . . . . . . . . . . . . . . . . 246

11.3.2 Wait and notify . . . . . . . . . . . . . . . . . . . . . . . . . . 248

11.3.3 Volatile storage . . . . . . . . . . . . . . . . . . . . . . . . . . 250

11.4 Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251

Index

255

8

CONTENTS

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download