A Java Reference: Assorted Java Reference Material

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

1.1 Basic Program Structure . . . . . . . . . . . . . . .

1.2 Compilation and Execution . . . . . . . . . . . . .

1.3 Simple Values and Expressions . . . . . . . . . . .

1.3.1 Writing Numbers . . . . . . . . . . . . . . .

1.3.2 Arithmetic . . . . . . . . . . . . . . . . . .

1.3.3 Comparisons and Logical Operations . . . .

1.3.4 Strings . . . . . . . . . . . . . . . . . . . . .

1.3.5 Static Methods: Abstracting Computation .

1.4 Conditional Execution . . . . . . . . . . . . . . . .

1.4.1 If statements . . . . . . . . . . . . . . . . .

1.4.2 Conditional Expressions . . . . . . . . . . .

1.4.3 Case analysis and the Switch Statement . .

1.5 Arrays I: The Command Line . . . . . . . . . . . .

1.6 Example: Finding Primes . . . . . . . . . . . . . .

1.6.1 Starting from the top . . . . . . . . . . . .

1.6.2 Starting from the bottom . . . . . . . . . .

1.6.3 Meeting in the middle . . . . . . . . . . . .

1.7 Example: Pig Latin . . . . . . . . . . . . . . . . .

1.7.1 Vowels . . . . . . . . . . . . . . . . . . . . .

1.7.2 Translation . . . . . . . . . . . . . . . . . .

1.7.3 Counting Consonants . . . . . . . . . . . .

1.7.4 To the top . . . . . . . . . . . . . . . . . . .

1.8 Variables and Assignment . . . . . . . . . . . . . .

1.9 Repetition . . . . . . . . . . . . . . . . . . . . . . .

1.9.1 Indefinite iteration . . . . . . . . . . . . . .

1.9.2 Definite Iteration . . . . . . . . . . . . . . .

1.9.3 Example: Iterative Prime-Finding . . . . .

1.9.4 Example: Counting . . . . . . . . . . . . .

1.10 Arrays II: Creation . . . . . . . . . . . . . . . . . .

1.10.1 Example: Linear Interpolation . . . . . . .

1.10.2 Example: The Sieve of Eratosthenes . . . .

1.10.3 Multi-dimensional Arrays . . . . . . . . . .

1.11 Introduction to Objects . . . . . . . . . . . . . . .

3

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

9

9

11

12

12

13

14

16

17

18

19

19

19

21

22

23

23

26

28

28

29

29

31

32

34

35

37

40

41

42

43

44

46

48

4

CONTENTS

.

.

.

.

.

.

.

.

.

.

.

.

48

50

51

52

53

56

60

62

62

62

63

63

2 Describing a Programming Language

2.1 Dynamic and Static Properties . . . . . . . . . . . . . . . . . . . . .

2.2 Describing Lexical Structure and Syntax . . . . . . . . . . . . . . . .

65

65

67

3 Lexical Basics

3.1 Layout: Whitespace and Comments . . . . . . . . . . . . . . . . . .

69

69

4 Values, Types, and Containers

4.1 Values and Containers . . . . . .

4.1.1 Containers and Names . .

4.1.2 Pointers . . . . . . . . . .

4.2 Types . . . . . . . . . . . . . . .

4.2.1 Static vs. dynamic types .

4.2.2 Type denotations in Java

4.3 Environments . . . . . . . . . . .

4.4 Applying the model to Java . . .

.

.

.

.

.

.

.

.

71

72

72

72

74

75

76

77

77

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

79

80

80

82

82

85

87

87

88

89

89

90

91

92

94

94

1.12

1.13

1.14

1.15

1.11.1 Simple Object Type Definition and

1.11.2 Instance Methods . . . . . . . . . .

1.11.3 Constructors . . . . . . . . . . . .

1.11.4 Example: A Prime-Number Class .

Interfaces . . . . . . . . . . . . . . . . . .

Inheritance . . . . . . . . . . . . . . . . .

Packages and Access Control . . . . . . .

Handling Exceptional Cases . . . . . . . .

1.15.1 Built-in Exceptions . . . . . . . . .

1.15.2 What Exactly is an Exception? . .

1.15.3 Throwing Exceptions Explicitly . .

1.15.4 Catching Exceptions . . . . . . . .

5 Declarations

5.1 Scope and the Meaning of Names

5.1.1 Block structure . . . . . .

5.1.2 Selection . . . . . . . . .

5.1.3 Packages and Imports . .

5.2 Local Variables . . . . . . . . . .

5.3 Type Declarations . . . . . . . .

5.4 Class Declarations . . . . . . . .

5.4.1 Kinds of class members .

5.4.2 Inheritance . . . . . . . .

5.4.3 Class Modifiers . . . . . .

5.4.4 Fields . . . . . . . . . . .

5.5 Interface Declarations . . . . . .

5.6 Enumerated Types . . . . . . . .

5.7 Nested Classes and Interfaces . .

5.7.1 Member types . . . . . .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

Creation

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

CONTENTS

5.8

5.9

5.10

5.11

5.12

5.13

5.7.2 Local classes . . . . . . . . . . .

5.7.3 Anonymous classes . . . . . . . .

5.7.4 What on earth is this all for? . .

Method (Function) Declarations . . . .

5.8.1 Variable-Length Parameter Lists

5.8.2 Method signatures . . . . . . . .

5.8.3 Overloading and hiding methods

5.8.4 Overriding . . . . . . . . . . . .

Constructor Declarations . . . . . . . .

5.9.1 Rationale. . . . . . . . . . . . . .

5.9.2 Instance and field initializers . .

5.9.3 Some Useful Constructor Idioms

Initialization of Classes . . . . . . . . . .

Access control . . . . . . . . . . . . . . .

The Java Program . . . . . . . . . . . .

5.12.1 Executing a Program . . . . . .

Annotations . . . . . . . . . . . . . . . .

5.13.1 Deprecated . . . . . . . . . . . .

5.13.2 Override . . . . . . . . . . . . . .

5.13.3 SuppressWarnings . . . . . . . .

5

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

6 The Expression Language

6.1 Syntactic overview . . . . . . . . . . . . .

6.1.1 Prefix, Postfix, and Infix Operators

6.1.2 Literals . . . . . . . . . . . . . . .

6.2 Conversions and Casts . . . . . . . . . . .

6.2.1 Explicit conversions . . . . . . . .

6.2.2 Implicit conversions . . . . . . . .

6.2.3 Promotions . . . . . . . . . . . . .

6.3 Integers and Characters . . . . . . . . . .

6.3.1 Integral values and their literals . .

6.3.2 Modular integer arithmetic . . . .

6.3.3 Manipulating bits . . . . . . . . .

6.4 Floating-Point Numbers . . . . . . . . . .

6.4.1 Floating-Point Literals . . . . . . .

6.4.2 Floating-point arithmetic . . . . .

6.5 Booleans and Conditionals . . . . . . . . .

6.5.1 Boolean literals . . . . . . . . . . .

6.5.2 Boolean operations . . . . . . . . .

6.5.3 Comparisons and Equality . . . . .

6.5.4 Conditional expressions . . . . . .

6.6 Reference Types . . . . . . . . . . . . . .

6.6.1 Allocating class instances . . . . .

6.6.2 Field Access . . . . . . . . . . . . .

6.6.3 Testing types . . . . . . . . . . . .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

95

97

98

99

100

101

103

104

107

108

109

110

112

113

114

114

116

117

117

118

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

119

119

121

122

123

123

124

125

125

126

129

132

135

136

137

141

142

142

142

143

143

144

144

147

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

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

Google Online Preview   Download