[Go to site: main page, start]

0% found this document useful (0 votes)
14 views341 pages

Java Programming

This document is a comprehensive guide to Java programming, detailing its history, features, and installation processes. It includes sections on the Java platform, IDEs, and language fundamentals, making it suitable for both beginners and experienced programmers. The document also addresses licensing information and provides resources for further participation and learning.

Uploaded by

Vaibhav dave
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views341 pages

Java Programming

This document is a comprehensive guide to Java programming, detailing its history, features, and installation processes. It includes sections on the Java platform, IDEs, and language fundamentals, making it suitable for both beginners and experienced programmers. The document also addresses licensing information and provides resources for further participation and learning.

Uploaded by

Vaibhav dave
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Programming

[Link]
June 28, 2024

On the 28th of April 2012 the contents of the English as well as German Wikibooks and Wikipedia
projects were licensed under Creative Commons Attribution-ShareAlike 3.0 Unported license. A
URI to this license is given in the list of figures on page 325. If this document is a derived work
from the contents of one of these projects and the content was still licensed by the project under
this license at the time of derivation this document has to be licensed under the same, a similar or a
compatible license, as stated in section 4b of the license. The list of contributors is included in chapter
Contributors on page 313. The licenses GPL, LGPL and GFDL are included in chapter Licenses on
page 331, since this book and/or parts of it may or may not be licensed under one or more of these
licenses, and thus require inclusion of these licenses. The licenses of the figures are given in the list of
figures on page 325. This PDF was generated by the LATEX typesetting software. The LATEX source
code is included as an attachment ([Link]) in this PDF file. To extract the source from
the PDF file, you can use the pdfdetach tool including in the poppler suite, or the [Link]
[Link]/tools/pdftk-the-pdf-toolkit/ utility. Some PDF viewers may also let you save
the attachment to a file. After extracting it from the PDF file you have to rename it to source.7z.
To uncompress the resulting archive we recommend the use of [Link] The LATEX
source itself was generated by a program written by Dirk Hünniger, which is freely available under
an open source license from [Link]
Contents

1 Overview 3

2 Preface 5
2.1 Are you new to programming? . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2 Programming with Java™ . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.3 What can Java not do? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

3 About This Book 7


3.1 Who should read this book? . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.2 How to use this book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.3 How can you participate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

4 History 11
4.1 Earlier programming languages . . . . . . . . . . . . . . . . . . . . . . . . . 11
4.2 The Green team . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
4.3 Reshaping thought . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
4.4 The demise of an idea, birth of another . . . . . . . . . . . . . . . . . . . . 14
4.5 Versions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.6 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

5 Java Overview 21
5.1 Object orientation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
5.2 Platform dependence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
5.3 Standardization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
5.4 Secure execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
5.5 Error handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
5.6 Networking capabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
5.7 Dynamic class loading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
5.8 Automatic memory garbage collection . . . . . . . . . . . . . . . . . . . . . 28
5.9 Applet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
5.10 Forbidden bad practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
5.11 Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

6 The Java Platform 31


6.1 Java Runtime Environment (JRE) . . . . . . . . . . . . . . . . . . . . . . . 31
6.2 Java Development Kit (JDK) . . . . . . . . . . . . . . . . . . . . . . . . . . 35
6.3 Similar concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39

7 Getting started 43
7.1 Understanding systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
7.2 The process of abstraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44

III
Contents

7.3 The process of encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . 49

8 Installation 51

9 Compilation 59
9.1 Quick compilation procedure . . . . . . . . . . . . . . . . . . . . . . . . . . 59
9.2 Automatic Compilation of Dependent Classes . . . . . . . . . . . . . . . . . 61
9.3 Packages, Subdirectories, and Resources . . . . . . . . . . . . . . . . . . . . 61
9.4 Filename Case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
9.5 Compiler Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
9.6 The JIT compiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

10 Execution 65
10.1 JSE code execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
10.2 J2EE code execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
10.3 Jini . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71

11 Understanding a Java Program 73


11.1 The Distance Class: Intent, Source, and Use . . . . . . . . . . . . . . . . . . 73
11.2 Detailed Program Structure and Overview . . . . . . . . . . . . . . . . . . . 74
11.3 Whitespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
11.4 Indentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85

12 Java IDEs 87
12.1 What is a Java IDE? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
12.2 Eclipse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
12.3 NetBeans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
12.4 JCreator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
12.5 Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
12.6 BlueJ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
12.7 Kawa . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
12.8 JBuilder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
12.9 DrJava . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
12.10Other IDEs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89

13 Language Fundamentals 91
13.1 The Java programming syntax . . . . . . . . . . . . . . . . . . . . . . . . . 91

14 Statements 93
14.1 Variable declaration statement . . . . . . . . . . . . . . . . . . . . . . . . . 93
14.2 Assignment statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
14.3 Assertion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
14.4 Program Control Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
14.5 Statement Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
14.6 Branching Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
14.7 Return statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
14.8 Iteration Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
14.9 The continue and break statements . . . . . . . . . . . . . . . . . . . . . . . 103
14.10Throw statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103

IV
Contents

14.11try/catch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104

15 Conditional blocks 105

16 Loop blocks 111


16.1 Break and continue keywords . . . . . . . . . . . . . . . . . . . . . . . . . . 115
16.2 Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
16.3 Try... catch blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
16.4 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122

17 Boolean expressions 125


17.1 Comparative operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
17.2 Boolean operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126

18 Variables 129
18.1 Variables in Java programming . . . . . . . . . . . . . . . . . . . . . . . . . 129
18.2 Kinds of variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
18.3 Creating variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
18.4 Assigning values to variables . . . . . . . . . . . . . . . . . . . . . . . . . . 132
18.5 Grouping variable declarations and assignment operations . . . . . . . . . . 132
18.6 Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
18.7 Naming conventions for identifiers . . . . . . . . . . . . . . . . . . . . . . . 134
18.8 Literals (values) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135

19 Primitive Types 137


19.1 Numbers in computer science . . . . . . . . . . . . . . . . . . . . . . . . . . 138
19.2 Integer types in Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
19.3 Integer numbers and floating point numbers . . . . . . . . . . . . . . . . . . 141
19.4 Data conversion (casting) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
19.5 Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143

20 Arithmetic expressions 145


20.1 Using bitwise operators within Java . . . . . . . . . . . . . . . . . . . . . . 149

21 Literals 151
21.1 Boolean Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
21.2 Numeric Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
21.3 String Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
21.4 null . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
21.5 Mixed Mode Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154

22 Methods 157
22.1 Parameter passing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
22.2 Variable argument list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
22.3 Return parameter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
22.4 Special method, the constructor . . . . . . . . . . . . . . . . . . . . . . . . . 163
22.5 Static methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164

V
Contents

23 API/[Link] 167
23.1 Immutability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
23.2 Concatenation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
23.3 Using StringBuilder/StringBuffer to concatenate strings . . . . . . . . . . . 169
23.4 Comparing Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
23.5 Splitting a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
23.6 Substrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
23.7 String cases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
23.8 See also . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175

24 Classes, Objects and Types 177


24.1 Instantiation and constructors . . . . . . . . . . . . . . . . . . . . . . . . . . 178
24.2 Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
24.3 Autoboxing/unboxing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
24.4 Methods in the Object class . . . . . . . . . . . . . . . . . . . . . . . . . . . 181

25 Keywords 185
25.1 abstract . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
25.2 assert . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
25.3 boolean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
25.4 break . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
25.5 byte . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
25.6 case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
25.7 catch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
25.8 char . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
25.9 class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
25.10const . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
25.11continue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
25.12See also . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
25.13default . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
25.14do . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
25.15double . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
25.16else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
25.17enum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
25.18extends . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
25.19final . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
25.20For a variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
25.21For a class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
25.22For a method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
25.23Interest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
25.24finally . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
25.25float . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
25.26for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
25.27goto . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
25.28if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
25.29implements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
25.30import . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
25.31instanceof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203

VI
Contents

25.32int . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
25.33interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
25.34long . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
25.35native . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
25.36See also . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
25.37new . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
25.38package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
25.39private . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
25.40protected . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
25.41public . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
25.42return . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
25.43short . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
25.44static . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
25.45Interest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
25.46strictfp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
25.47super . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
25.48switch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
25.49synchronized . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
25.50Singleton example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
25.51this . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
25.52throw . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
25.53See also . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
25.54throws . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
25.55transient . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
25.56try . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
25.57void . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
25.58volatile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
25.59while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223

26 Packages 225
26.1 Package declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
26.2 Import and class usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
26.3 Wildcard imports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
26.4 Package convention . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
26.5 Importing packages from .jar files . . . . . . . . . . . . . . . . . . . . . . . . 227
26.6 Class loading/package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227

27 Arrays 229
27.1 Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
27.2 Two-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
27.3 Multidimensional Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232

28 Mathematical functions 233


28.1 Math constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
28.2 Math methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
28.3 Functions dealing with floating-point representation . . . . . . . . . . . . . 235
28.4 Rounding number example . . . . . . . . . . . . . . . . . . . . . . . . . . . 236

VII
Contents

29 Large numbers 249


29.1 BigInteger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
29.2 BigDecimal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250

30 Random numbers 251


30.1 Truly random numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252

31 Unicode 253
31.1 Unicode escape sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
31.2 International language support . . . . . . . . . . . . . . . . . . . . . . . . . 254
31.3 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254

32 Comments 255
32.1 Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
32.2 Comments and unicode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
32.3 Javadoc comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258

33 Coding conventions 259

34 Classes and Objects 261


34.1 Classes and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261

35 Defining Classes 263


35.1 Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
35.2 Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
35.3 Initializers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266

36 Inheritance 269
36.1 The Object class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
36.2 The super keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270

37 Interfaces 273
37.1 Interest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
37.2 Extending interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275

38 Overloading Methods and Constructors 279


38.1 Method overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
38.2 Variable Argument . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
38.3 Constructor overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
38.4 Method overriding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285

39 Object Lifecycle 287


39.1 Creating object with the new keyword . . . . . . . . . . . . . . . . . . . . . 287
39.2 Creating object by cloning an object . . . . . . . . . . . . . . . . . . . . . . 288
39.3 Re-creating an object received from a remote source . . . . . . . . . . . . . 290
39.4 Destroying objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
39.5 Class loading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292

40 Scope 293
40.1 Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293

VIII
Contents

40.2 Access modifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294


40.3 Utility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
40.4 Field encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295

41 Nested Classes 299


41.1 Inner classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
41.2 Anonymous Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300

42 Generics 303
42.1 Generic class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
42.2 Generic method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
42.3 Wildcard Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
42.4 Class<T> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
42.5 Motivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
42.6 Note for C++ programmers . . . . . . . . . . . . . . . . . . . . . . . . . . . 310

43 Contributors 313

List of Figures 325

44 Licenses 331
44.1 GNU GENERAL PUBLIC LICENSE . . . . . . . . . . . . . . . . . . . . . 331
44.2 GNU Free Documentation License . . . . . . . . . . . . . . . . . . . . . . . 332
44.3 GNU Lesser General Public License . . . . . . . . . . . . . . . . . . . . . . 333

1
1 Overview

3
2 Preface

The beautiful thing about learning is nobody can take it away from you.
Learning a computer programming language is like a toddler's first steps. You stumble, and
fall, but when you start walking, programming becomes second nature. And once you start
programming, you never cease evolving or picking up new tricks. Learn one programming
language, and you will ”know” them all — the logic of the world will begin to unravel around
you.

2.1 Are you new to programming?

Figure 1 This stunning image of the sunset on planet Mars


wouldn't have been possible without Java.

If you have chosen Java as your first programming language, be assured that Java is also
the first choice for computer science programs in many universities. Its simple and intuitive
syntax1 , or grammar, helps beginners feel at ease with complex programming constructs
quickly.
However, Java is not a basic programming language. In fact, NASA used Java as the driving
force (quite literally) behind its Mars Rover missions. Robots, air traffic control systems
and the self-checkout barcode scanners in your favorite supermarkets can all be programmed
in Java.

2.2 Programming with Java™


By now, you might truly be able to grasp the power of the Java programming language.
With Java, there are many possibilities. Yet not every programmer gets to program appli-
cations that take unmanned vehicles onto other planets. Software that we encounter in our

1 [Link]

5
Preface

daily life is somewhat humble in that respect. Software in Java, however, covers a vast area
of the computing ecosphere. Here are just a few examples of the ubiquitous nature of Java
applications in real-life:
• OpenOffice.org2 , a desktop office management suite that rivals the Microsoft Office suite
has been written in Java.
• The popular building game Minecraft3 is written in Java.
• Online browser-based games like Runescape4 , a 3D massively multi-player online role
playing game (MMORPG), run on graphics routines, 3D rendering and networking ca-
pabilities powered by the Java programming language.
• Two of the world's renowned digital video recorders, TiVo5 and BSkyB's Sky+6 use built-
in live television recording software to record, rewind and play your favorite television
shows. These applications make extensive use of the Java programming language.
The above mentioned applications illustrate the reach and ubiquity of Java applications.
Here's another fact: almost 80% of mobile phone vendors adopt Java as their primary
platform for the development of applications. The most widely used mobile-based operat-
ing system, Android7 , uses Java as one of its key application platforms — developers are
encouraged to develop applications for Android in the Java programming language.

2.3 What can Java not do?


Well, to be honest, there is nothing that Java can't do, at least for application programming.
Java is a ”complete” language; the only limits are programmer imagination and ability. This
book aims to get you acquainted with the basics of the language so you can create the
software masterpiece of your dreams. The one area where Java can't be used is for direct
interaction with computer hardware. If you want to write an operating system, you will
need to look elsewhere!

2 [Link]
3 [Link]
4 [Link]
5 [Link]
6 [Link]
7 [Link]

6
3 About This Book

The Java Programming Wikibook is a shared effort in amassing a comprehensive guide


of the complete Java platform — from programming advice and tutorials for the desktop
computer to programming on mobile phones. The information presented in this book has
been conceptualised with the combined efforts of various contributors1 , and anonymous
editors.
The primary purpose of this book is to teach the Java programming language to an audience
of beginners, but with its progressive layout of tutorials increasing in complexity, it can be
just as helpful for intermediate and experienced programmers. Thus, this book is meant to
be used as:
• a collection of tutorials building upon one another in a progressive manner;
• a guidebook for efficient programming with the Java programming language; and,
• a comprehensive manual resource for the advanced programmer.
This book is intended to be used in conjunction with various other online resources, such
as:
• the Java platform API documentation2 ;
• the official Java website3 ; and,
• active Java communities online, such as Java.net4 and JavaRanch5 , etc.

3.1 Who should read this book?


Everything you would need to know to write computer programs would be explained in
this book. By the time you finish reading, you will find yourself proficient enough to tackle
just about anything in Java and programs written using it. This book serves as the first
few stepping stones of many you would need to cross the unfriendly waters of computer
programming. We have put a lot of emphasis in structuring this book in a way that lets
you start programming from scratch, with Java as your preferred language of choice. This
book is designed for you if any one of the following is true.
• You are relatively new to programming and have heard how easy it is to learn Java.
• You had some BASIC6 or Pascal7 in school, and have a grasp of basic programming and
logic.

1 [Link]
2 [Link]
3 [Link]
4 [Link]
5 [Link]
6 [Link]
7 [Link]

7
About This Book

• You already know and have been introduced to programming in earlier versions of Java.
• You are an experienced developer and know how to program in other languages like
C++8 , Visual Basic9 , Python10 , Ruby11 , etc.
• You've heard that Java is great for web applications and web services programming.
Although this book is generally meant to be for readers who are beginning to learn program-
ming, it can be highly beneficial for intermediate and advanced programmers who may have
missed out on some vital information. After completing this book you should be able to
solve many complicated problems using the Java skills presented in the following chapters.
Once you finish, you are also encouraged to undertake ambitious programming projects of
your own.
This book assumes that the reader has no prior knowledge of programming in Java, or for
that matter, any object-oriented programming language. Practical examples and exercises
following each topic and module make it easy to understand the software development
methodology. If you are a complete beginner, we suggest that you move slowly through this
book and complete each exercise at your own pace.

3.2 How to use this book


This book is a reference book of the Java language and its related technologies. Its goal
is to give a complete picture of Java and its technologies. While the book can be read
from the beginning to end, it is also designed to have individual sections that can be read
independently. To help find information quickly, navigation boxes are given in the online
version for access to individual topics.
This book is divided into sections. Pages are grouped together into section topics. To make
this book expandable in the future via the addition of new sections, the sections navigation-
wide are independent from each other. Each section can be considered as a mini book by
itself. Pages that belong to the same topic can be navigated by the links on the right hand
side.

3.3 How can you participate


Content is constantly being updated and enhanced in this book as is the nature of wiki-
based content. This book is therefore in a constant state of evolution. Any Wikibooks users
can participate in helping this book to a better standard as both a reader, or a contributor.

3.3.1 As a reader
If you are interested in reading the content present in this book, we encourage you to:

8 [Link]
9 [Link]
10 [Link]
11 [Link]

8
How can you participate

• share comments about the technical accuracy, content, or organization of this book by
telling the contributors in the Discussion section for each page. You can find the link
Discussion on each page in this book leading you to appropriate sections for discussion.
Leave a signature when providing feedback, writing comments, or giving suggestion on
the Discussion pages. This can be achieved by appending -- ~~~~ to your messages.
Do not add your signatures to the Book pages, they are only meant for the Discussion
pages.
• share news about the Java Programming Wikibook with your family and friends and let
them know about this comprehensive Java guide online.
• become a contributing author, if you think that you have information that could fill in
some missing gaps in this book.

3.3.2 As a contributor
If you are intent on writing content for this book, you need to do the following:
• When writing content for this book, you can always pose as an anonymous contributor,
however we recommend you sign-in into the Wikibooks website when doing so. It becomes
easier to track and acknowledge changes to certain parts of the book. Furthermore, the
opinions and views of logged-in users are given precedence over anonymous users.
• Once you have started contributing content for this book, make sure that you add your
name to the contributor list12 .
• Be bold13 and try to follow the conventions14 for this Wikibook. It is important that the
conventions for this book be followed to the letter to make content consistent and reliable
throughout.

12 [Link]
13 [Link]
14 [Link]

9
4 History

On 23 May 1995, John Gage1 , the director of the Science Office of the Sun Microsystems2
along with Marc Andreesen3 , co-founder and executive vice president at Netscape4 an-
nounced to an audience of SunWorldTM that Java technology wasn't a myth and that it was
going to be incorporated into Netscape Navigator5 .6
At the time the total number of people working on Java was less than 30.8 This team
would shape the future in the next decade and no one had any idea as to what was in store.
From running an unmanned vehicle on Mars to serving as the operating environment of
most consumer electronics, e.g. cable set-top boxes, VCRs, toasters and PDAs10 ,11 Java
has come a long way from its inception. Let's see how it all began.

4.1 Earlier programming languages


Before Java emerged as a programming language, C++13 was the dominant player in the
trade. The primary goal of the creators of Java was to create a language that could tackle
most of the things that C++ offered while getting rid of some of the more tedious tasks
that came with the earlier languages.
Computer hardware went through a performance and price revolution from 1972 to 1991.
Better, faster hardware was available at ever lower prices, and the demand for big and
complex software exponentially increased. To accommodate the demand, new development
technologies were invented.
The C language developed in 1972 by Dennis Ritchie14 had taken a decade to become
the most popular language amongst programmers working on PCs and similar platforms
(other languages, like COBOL15 and FORTRAN16 , dominated the mainframe market). But,
with time programmers found that programming in C became tedious with its structural

1 [Link]
2 [Link]
3 [Link]
4 [Link]
5 [Link]
6 Java Technology: The Early Years 7 . Sun Microsystems . Retrieved 9 May 2008
8 Java Technology: The Early Years 9 . Sun Microsystems . Retrieved 9 May 2008
10 [Link]
11 History of Java 12 . Lindsey, Clark S. . Retrieved 7 May 2008
13 [Link]
14 [Link]
15 [Link]
16 [Link]

11
History

syntax.17 Although people attempted to solve this problem, it would be later that a new de-
velopment philosophy was introduced, one named Object-Oriented Programming18 (OOP).
With OOP, one can write code that can be reused later without needing to rewrite the code
over and over again. In 1979, Bjarne Stroustrup19 developed C++, an enhancement to the
C language with included OOP fundamentals and features. Sun generated revenue from
Java through the selling of licenses for specialized products such as the Java Enterprise
System20 .

4.2 The Green team

Figure 2 James Goslinga , architect and


designer of the compiler for the Java
technology
a [Link]

In December 1990, a project was initiated behind closed doors with the aim to create
a programming tool that could render obsolete the C and C++ programming languages.
Engineer Patrick Naughton21 had become extremely frustrated with the state of Sun's C++
and C APIs (Application Programming Interfaces22 ) and tools. While he was considering to
move towards NeXT23 , he was offered a chance to work on new technology and the Stealth
Project was started, a secret nobody but he knew.

17 Structural syntax is a linear way of writing code. A program is interpreted usually at the first line of the
program's code until it reaches the end. One cannot hook a later part of the program to an earlier one.
The flow follows a linear top-to-bottom approach.
18 [Link]
19 [Link]
20 [Link]
21 [Link]
22 [Link]
23 [Link]

12
Reshaping thought

This Stealth Project was later named the Green Project when James Gosling24 and Mike
Sheridan joined Patrick.25 As the Green Project teethed, the prospects of the project
started becoming clearer to the engineers working on it. No longer did it aim to create a
new language far superior to the present ones, but it aimed to target devices other than the
computer.
Staffed at 13 people, they began work in a small office on Sand Hill Road in Menlo Park27 ,
California. This team came to be called the Green Team henceforth in time. The project
they underwent was chartered by Sun Microsystems to anticipate and plan for the ”next
wave” in computing. For the team, this meant at least one significant trend, that of the
convergence of digitally controlled consumer devices and computers.28

4.3 Reshaping thought


The team started thinking of replacing C++ with a better version, a faster version, a
responsive version. But the one thing they hadn't thought of, as of yet, was that the
language they were aiming for had to be developed for an embedded system30 with limited
resources. An embedded system is a computer system scaled to a minimalistic interface
demanding only a few functions from its design. For such a system, C++ or any successor
would seem too large as all the languages at the time demanded a larger footprint than
what was desired. The team thus had to think in a different way to go about solving all
these problems.
Co-founder of Sun Microsystems, Bill Joy, envisioned a language combining the power
of Mesa and C in a paper named Further he wrote for the engineers at Sun. Gathering
ideas, Gosling began work on enhancing C++ and named it ”C++ ++ –”, a pun on the
evolutionary structure of the language's name. The ++ and – meant, putting in and taking
out stuff. He soon abandoned the name and called it Oak31 after the tree that stood outside
his office.

Table 1: Who's who of the Java technology33


Has worked for GT (Green Team), FP (FirstPerson) and JP (Java Products Group)
Name GT FP JP Details
Lisa Friendly Yes Yes FirstPerson employee and member
of the Java Products Group
John Gage Science Office (Director), Sun Mi-
crosystems
James Gosling Yes Yes Yes Lead engineer and key architect of
the Java technology

24 [Link]
25 Java Technology: The Early Years 26 . Sun Microsystems . Retrieved 9 May 2008
27 [Link]
28 Java Technology: The Early Years 29 . Sun Microsystems . Retrieved 9 May 2008
30 [Link]
31 Java Technology: The Early Years 32 . Sun Microsystems . Retrieved 9 May 2008

13
History

Table 1: Who's who of the Java technology33


Has worked for GT (Green Team), FP (FirstPerson) and JP (Java Products Group)
Name GT FP JP Details
Bill Joy Co-founder and VP, Sun Microsys-
tems; Principal designer of the UC
Berkeley, version of the UNIXr OS
Jonni Kanerva Yes Java Products Group employee,
author of The Java FAQ1
Tim Lindholm Yes Yes FirstPerson employee and member
Java Products Group
Scott McNealy Chairman, President, and CEO of
Sun Microsystems
Patrick Yes Yes Green Team member, FirstPerson
Naughton co-founder
George Paolini Corporate Marketing (Director),
Sun's Java Software Division
Kim Polese Yes FirstPerson product marketing
Lisa Poulson Original director of public rela-
tions for Java technology (Burson-
Marsteller)
Wayne Rosing Yes FirstPerson President
Eric Schmidt Former Sun Microsystems Chief
Technology Officer
Mike Sheridan Yes Green Team member

4.4 The demise of an idea, birth of another


By now, the work on Oak had been significant but come the year 1993, people saw the
demise of set-top boxes, interactive TV and the PDAs. A failure that completely ushered
the inventors' thoughts to be reinvented. Only a miracle could make the project a success
now. And such a miracle awaited anticipation.
National Center for Supercomputing Applications35 (NCSA) had just unveiled its new com-
mercial web browser for the internet the previous year. The focus of the team, now diverted
towards where they thought the ”next-wave” of computing would be — the internet. The
team then divulged into the realms of creating the same embeddable technology to be used
in the web browser space calling it an applet36 — a small application. Keeping all of this
in mind, the team created a list of features tackling the C++ problems. In their opinion,
the project should ...
• .. be simple and gather tested fundamentals and features from the earlier languages in it,
• .. have standard sets of APIs with basic and advanced features bundled with the language,

35 [Link]
36 [Link]

14
Versions

• .. get rid of concepts requiring direct manipulation of hardware (in this case, memory) to
make the language safe,
• .. be platform independent and may be written for every platform once (giving birth to
the WORA37 idiom),
• .. be able to manipulate network programming out-of-the-box,
• .. be embeddable in web browsers, and ...
• .. have the ability for a single program to multi-task and do multiple things at the same
time.
The team now needed a proper identity and they decided on naming the new technology
they created Java ushering a new generation of products for the internet boom. A by-
product of the project was a cartoon named ”Duke38 ” created by Joe Parlang which became
its identity then.
Finally at the SunWorldTM conference, Andreesen unveiled the new technology to the
masses. Riding along with the explosion of interest and publicity in the Internet, Java
quickly received widespread recognition and expectations grew for it to become the dominant
software for browser and consumer applications.39
Initially Java was owned by Sun Microsystems, but later it was released to open source;
the term Java was a trademark of Sun Microsystems. Sun released the source code for its
HotSpot Virtual Machine and compiler in November 2006, and most of the source code of
the class library in May 2007. Some parts were missing because they were owned by third
parties, not by Sun Microsystems. The released parts were published under the terms of
the GNU General Public License41 , a free software license.

4.5 Versions
Unlike C and C++, Java's growth is pretty recent. Here, we'd quickly go through the
development paths that Java took with age.

Figure 16 Development of Java over the years. From version 1.0 to version 1.7, Java
has displayed a steady growth.

37 [Link]
38 [Link]
39 History of Java 40 . Lindsey, Clark S. . Retrieved 7 May 2008
41 [Link]

15
History

4.5.1 Initial Release (versions 1.0 and 1.1)


Introduced in 1996 for the Solaris42 , Windows43 , Mac OS44 Classic and Linux45 , Java was
initially released as the Java Development Kit 1.0 (JDK 1.0). This included the Java
runtime (the virtual machine and the class libraries), and the development tools (e.g., the
Java compiler). Later, Sun also provided a runtime-only package, called the Java Runtime
Environment (JRE). The first name stuck, however, so usually people refer to a particular
version of Java by its JDK version (e.g., JDK 1.0).

4.5.2 Java 2 (version 1.2)


Introduced in 1998 as a quick fix to the former versions, version 1.2 was the start of a new
beginning for Java. The JDKs of version 1.2 and later versions are often called Java 2 as
well. For example, the official name of JDK 1.4 is The Java(TM) 2 Platform, Standard
Edition version 1.4.
Major changes include:
• Rewrite the event handling (add Event Listeners46 )
• Change Thread synchronizations
• Introduction of the JIT-Just in time compilers

4.5.3 Kestrel (Java 1.3)


Released on 8 May 2000. The most notable changes were:
• HotSpot JVM included (the HotSpot JVM was first released in April, 1999 for the J2SE
1.2 JVM)
• RMI was modified to support optional compatibility with CORBA
• JavaSound
• Java Naming and Directory Interface (JNDI) included in core libraries (previously avail-
able as an extension)
• Java Platform Debugger Architecture (JPDA)
• Synthetic proxy classes

4.5.4 Merlin (Java 1.4)


Released on 6 February 2002, Java 1.4 has improved programmer productivity by expanding
language features and available APIs:
• Assertion
• Regular Expression
• XML processing
• Cryptography and Secure Socket Layer (SSL)

42 [Link]
43 [Link]
44 [Link]
45 [Link]
46 [Link]

16
Versions

• Non-blocking I/O (NIO)


• Logging

4.5.5 Tiger (version 1.5.0; Java SE 5)


Released in September 2004
Major changes include:
• Generics47 - Provides compile-time type safety for collections :and eliminates the
drudgery of casting.
• Autoboxing/unboxing48 - Eliminates the drudgery of manual conversion between prim-
itive types (such as int) and wrapper types (such as Integer).
• Enhanced for - Shorten the for loop with Collections use.
• Static imports - Lets you import all the static part of a class.
• Annotation49 /Metadata - Enabling tools to generate code and deployment descriptors
from annotations in the source code. This leads to a ”declarative” programming style
where the programmer says what should be done and tools emit the code to do it.
Annotations can be inspected through source parsing or by using the additional reflection
APIs added in Java 5.
• JVM Improvements - Most of the run time library is now mapped into memory as a
memory image, as opposed to being loaded from a series of class files. Large portion of
the runtime libraries will now be shared among multiple JVM instances.

4.5.6 Mustang (version 1.6.0; Java SE 6)


Released on 11 December 2006.
What's New in Java SE 6:
• Web Services - First-class support for writing XML web service client applications.
• Scripting - You can now mix in JavaScript technology source code, useful for prototyping.
Also useful when you have teams with a variety of skill sets. More advanced developers
can plug in their own scripting engines and mix their favorite scripting language in with
Java code as they see fit.
• Database - No more need to find and configure your own JDBC database when developing
a database application. Developers will also get the updated JDBC 4.0, a well-used API
with many important improvements, such as special support for XML as an SQL datatype
and better integration of Binary Large OBjects (BLOBs) and Character Large OBjects
(CLOBs) into the APIs.
• More Desktop APIs - GUI developers get a large number of new tricks to play like the
ever popular yet newly incorporated SwingWorker utility to help you with threading in
GUI apps, JTable sorting and filtering, and a new facility for quick splash screens to quiet
impatient users.

47 Chapter 41.2 on page 302


48 Chapter 24.3 on page 180
49 [Link]

17
History

• Monitoring and Management - The really big deal here is that you don't need to do
anything special to the startup to be able to attach on demand with any of the monitoring
and management tools in the Java SE platform.
• Compiler Access - Really aimed at people who create tools for Java development and for
frameworks like JavaServer Pages (JSP) or Personal Home Page construction kit (PHP)
engines that need to generate a bunch of classes on demand, the compiler API opens up
programmatic access to javac for in-process compilation of dynamically generated Java
code. The compiler API is not directly intended for the everyday developer, but for those
of you deafened by your screaming inner geek, roll up your sleeves and give it a try. And
the rest of us will happily benefit from the tools and the improved Java frameworks that
use this.
• Pluggable Annotations allows programmer to write annotation processor so that it can
analyse your code semantically before javac compiles. For example, you could write an
annotation processor that verifies whether your program obeys naming conventions.
• Desktop Deployment - At long last, Java SE 6 unifies the Java Plug-in technology and Java
WebStart engines, which just makes sense. Installation of the Java WebStart application
got a much needed makeover.
• Security - Java SE 6 has simplified the job of its security administrators by providing
various new ways to access platform-native security services, such as native Public Key
Infrastructure (PKI) and cryptographic services on Microsoft Windows for secure authen-
tication and communication, Java Generic Security Services (Java GSS) and Kerberos
services for authentication, and access to LDAP servers for authenticating users.
• The -lities: Quality, Compatibility, Stability - Bug fixes ...

4.5.7 Dolphin (version 1.7.0; Java SE 7)


Released on 28 July 2011.
Feature additions for Java 7 include:
• JVM support for dynamic languages, following the prototyping work currently done on
the Multi Language Virtual Machine
• Compressed 64-bit pointers Available in Java 6 with -XX:+UseCompressedOops50
• Small language changes (grouped under a project named Coin):
• Strings in switch
• Automatic resource management in try-statement
• Improved type inference for generic instance creation
• Simplified varargs method declaration
• Binary integer literals
• Allowing underscores in numeric literals
• Catching multiple exception types and rethrowing exceptions with improved type check-
ing
• Concurrency utilities under JSR 166

50 [Link]

18
References

• New file I/O library to enhance platform independence and add support for metadata
and symbolic links. The new packages are [Link] and [Link]
• Library-level support for Elliptic curve cryptography algorithms
• An XRender pipeline for Java 2D, which improves handling of features specific to modern
GPUs
• New platform APIs for the graphics features originally planned for release in Java version
6u10
• Enhanced library-level support for new network protocols, including SCTP and Sockets
Direct Protocol
• Upstream updates to XML and Unicode
Lambda (Java's implementation of lambda functions), Jigsaw (Java's implementation of
modules), and part of Coin were dropped from Java 7.

4.5.8 Spider (version 1.8.0; Java SE 8)


Java 8 was released on 18 March 2014, and included some features that were planned for
Java 7 but later deferred.
Work on features was organized in terms of JDK Enhancement Proposals (JEPs).
• JSR 335, JEP 126: Language-level support for lambda expressions (officially, lambda
expressions; unofficially, closures) under Project Lambda which allow the addition of
methods to interfaces without breaking existing implementations. There was an ongoing
debate in the Java community on whether to add support for lambda expressions. Sup-
porting lambda expressions also allows the performance of functional-style operations on
streams of elements, such as MapReduce-inspired transformations on collections. Default
methods allow an author of an API to add new methods to an interface without breaking
the old code using it. Although it was not their primary intent, default methods also
allow multiple inheritance of behavior (but not state).
• JSR 223, JEP 174: Project Nashorn, a JavaScript runtime which allows developers to
embed JavaScript code within applications
• JSR 308, JEP 104: Annotation on Java Types
• Unsigned Integer Arithmetic
• JSR 337, JEP 120: Repeating annotations
• JSR 310, JEP 150: Date and Time API
• JEP 178: Statically-linked JNI libraries
• JEP 153: Launch JavaFX applications (direct launching of JavaFX application JARs)
• JEP 122: Remove the permanent generation

4.6 References

19
5 Java Overview

The new features and upgrades included into Java changed the face of programming en-
vironment and gave a new definition to Object Oriented Programming1 (OOP in short).
But unlike its predecessors, Java needed to be bundled with standard functionality and be
independent of the host platform.
The primary goals in the creation of the Java language:
• It is simple.
• It is object-oriented.
• It is independent of the host platform.
• It contains language facilities and libraries for networking.
• It is designed to execute code from remote sources securely.
The Java language introduces some new features that didn't exist in other languages like
C and C++.

5.1 Object orientation

Figure 17 Object oriented programming can be represented with


UMLa diagrams.
a [Link]

Object orientation (”OO”) refers to a method of programming and language technique. The
main idea of OO is to design software around the ”things” (i.e. objects) it manipulates,
rather than the actions it performs.

1 [Link]

21
Java Overview

As the hardware of the computer advanced, it brought about the need to create better
software techniques to be able to create ever increasing complex applications. The intent is
to make large software projects easier to manage, thus improving quality and reducing the
number of failed projects. Object oriented solution is the latest software technique.
Assembly languages
Software techniques started with the assembly languages, that were close to machine in-
struction and were easy to convert into executable code. Each hardware had its own
assembly language. Assembly language contains low level instructions like move data from
memory to hardware registers, do arithmetic operations, and move data back to memory.
Programmers had to know the detailed architecture of the computer in order to write
programs.
Procedural languages
After the assembly languages, high level languages were developed. Here the language
compiler is used to convert the high level program to machine instructions, freeing the pro-
grammers from the burden of knowing the computer hardware architecture. To promote
the re-use of code and to minimize the use of GOTO instructions, ”procedural” techniques
were introduced. This simplified the creation and maintenance of software control flow,
but left out the organization of data. It became a nightmare to debug and maintain pro-
grams having many global variables (i.e. variables that contain data that can be modified
anywhere in the application).
Object oriented languages
In OO languages, data is taken seriously with information hiding. Data that is specific to
an object can only be accessed by procedures in that object. As a result, objects contain
data as well as control flow and a program becomes a series of interactions between objects.

5.2 Platform dependence


In C or C++ programming, you start to write source code:

... you compile it to a machine code file:

22
Platform dependence

... and then you execute it:

In this situation, the machine code file and its execution are specific to the platform (Win-
dows, Linux, macOS, ...) it was compiled for, that is to say to the targeted platform:

... because the compiled file is a machine code file designed to work on a specific platform
and hardware. It would have produced different results/output for another platform. So
if you want your program to run on several platforms, you have to compile your program
several times:

It poses greater vulnerability risks. Note here that when a certain code is compiled into
an executable format, the executable cannot be changed dynamically. It would need to be
recompiled from the changed code for the changes to be reflected in the finished executable.
Modularity (dividing code into modules) is not present in Java's predecessors. If instead
of a single executable, the output application was in the form of modules, one could easily

23
Java Overview

change a single module and review changes in the application. In C/C++ on the other
hand, a slight change in code required the whole application to be recompiled.
The idea of Java is to compile the source code into an intermediate language that will be
interpreted.

The source code The intermediate file The interpreter

The intermediate language is the byte code. The interpreter is the Java Virtual Machine
(JVM). The byte code file is universal and the JVM is platform specific:

So a JVM should be coded for each platform. And that's the case. So you just have to
generate a unique byte code file (a .class file).
The first implementations of the language used an interpreted virtual machine to achieve
portability, and many implementations still do. These implementations produce programs
that run more slowly than the fully-compiled programs created by the typical C++ compiler,
so the language suffered a reputation for producing slow programs. Since Java 1.2, Java
VM produces programs that run much faster, using multiple techniques.
The first of these is to simply compile directly into native code like a more traditional
compiler, skipping bytecode entirely. This achieves great performance, but at the expense
of portability. This is not really used any more.
Another technique, the just-in-time (JIT) compiler, compiles the Java bytecode into native
code at the time the program is run, and keep the compiled code to be used again and again.
More sophisticated VMs even use dynamic recompilation, in which the VM can analyze the
behavior of the running program and selectively recompile and optimize critical parts of
the program. Both of these techniques allow the program to take advantage of the speed of
native code without losing portability.

24
Standardization

Portability is a technically difficult goal to achieve, and Java's success at that goal is a
matter of some controversy. Although it is indeed possible to write programs for the Java
platform that behave consistently across many host platforms, the large number of available
platforms with small errors or inconsistencies led some to parody Sun's ”Write once, run
anywhere” slogan as ”Write once, debug everywhere”.

5.3 Standardization
C++ was built atop the C language and as a result divergent ways of doing the same thing
manifested around the language. For instance, creating an object could be done in three
different ways in C++. Furthermore, C++ did not come with a standard library bundled
with its compilers. Instead, it relied on resources created by other programmers; code which
rarely fit together.
In Java, standardized libraries are provided to allow access to features of the host machines
(such as graphics and networking) in unified ways. The Java language also includes support
for multi-threaded programs—a necessity for many networking applications.
Platform independent Java is, however, very successful with server side applications, such
as web services, servlets, or Enterprise JavaBeans.

Figure 41 Swing does not rely on the underlying native user


interface.

25
Java Overview

Java also made progress on the client side: first it had Abstract Window Toolkit2 (AWT),
then Swing3 , and the most recent client side library is the Standard Widget Toolkit4 (SWT).
It is interesting to see how they tried to handle the two opposing consuming forces. Those
are :
Efficient, fast code; port to most popular hardware (write once, test anywhere)
Use the underlying native subroutine to create a GUI component. This approach was
taken by AWT, and SWT.
Portability to any hardware where JVM ported (write once, run anywhere)
To achieve this to the latter, the Java toolkit should not rely on the underlying native user
interface. Swing took this approach.
It is interesting to see how the approach was switched back and forth. AWT → Swing →
SWT.

5.4 Secure execution

Figure 42 The segmentation fault, one of the most recurrent


issues in C programming.

With the high-level of control built into the language to manipulate hardware, a C/C++
programmer could access almost any resource, either hardware or software on the system.
This was intended to be one of the languages' strong points, but this very flexibility led to
confusion and complex programming practices.

2 [Link]
3 [Link]
4 [Link]

26
Error handling

5.5 Error handling


The old way of error handling was to let each function return an error code then let the
caller check what was returned. The problem with this method was that if the return code
was full of error-checking codes, this got in the way of the original one that was doing the
actual work, which in turn did not make it very readable.
In the new way of error handling, functions/methods do not return error codes. Instead,
when there is an error, an exception5 is thrown. The exceptions can be handled by the
catch keyword at the end of a try block. This way, the code that is calling the function
does not need to be mangled with error checking codes, thus making the code more readable.
This new way of error handling is called Exception handling.
Exception handling was also added to C++. However, there are two differences between
Java and C++ Exception handling:
• In Java, the exception that is thrown is a Java object like any other object in Java. It
only has to implement Throwable interface.
• In Java, the compiler checks whether an exception may be caught or not. The compiler
gives an error if there is no catch block for a thrown exception.
The optional exception handling in the Java predecessors leads the developers not to care
about the error handling. As a consequence, unexpected errors often occur. Java forces the
developers to handle exceptions. The programmer must handle exception or declare that
the user must handle it. Someone must handle it.

5.6 Networking capabilities


However powerful, the predecessors of Java lacked a standard feature to network with other
computers, and usually relied on the platforms' intricate networking capabilities. With
almost all network protocols being standardized, the creators of Java technology wanted
this to be a flagship feature of the language while keeping true to the spirit of earlier
advances made towards standardizing Remote Procedure Call6 . Another feature that the
Java team focused on was its integration in the World Wide Web and the Internet.
The Java platform was one of the first systems to provide wide support for the execution
of code from remote sources. The Java language was designed with network computing7 in
mind.
An applet could run within a user's browser, executing code downloaded from a remote
HTTP server. The remote code runs in a highly restricted ”sandbox”, which protects the
user from misbehaving or malicious code; publishers could apply for a certificate that they
could use to digitally sign applets as ”safe”, giving them permission to break out of the
sandbox and access the local file system and network, presumably under user control.

5 [Link]
6 [Link]
7 [Link]

27
Java Overview

5.7 Dynamic class loading


In conventional languages like C and C++, all code had to be compiled and linked to one
executable program, before execution. In Java, classes are compiled as needed. If a class is
not needed during an execution phase, that class is not even compiled into byte code.
This feature comes in handy especially in network programming when we do not know,
beforehand, what code will be executed. A running program could load classes from the
file system or from a remote server.
Also this feature makes it theoretically possible for a Java program to alter its own code
during execution, in order to do some self-learning behavior. It would be more realistic
to imagine, however, that a Java program would generate Java code before execution, and
then, that code would be executed. With some feedback mechanism, the generated code
could improve over time.

5.8 Automatic memory garbage collection


In conventional languages like C and C++, the programmer has to make sure that all
memory that was allocated is freed. Memory leaks became a regular nuisance in instances
where the programmers had to manually allocate the system's memory resources.
Memory resources or buffers have specific modes of operation for optimal performance. Once
a buffer is filled with data, it needs to be cleaned up after there is no further use for its
content. If a programmer forgets to clean it in their code, the memory is easily overloaded.
Programming in C/C++ languages became tedious and unsafe because of these very quirks,
and programs built in these languages were prone to memory leakages and sudden system
crashes — sometimes even harming the hardware itself. Freeing memory is particularly
important in servers, since it has to run without stopping for days. If a piece of memory is
not freed after use and the server just keeps allocating memory, that memory leak can take
down the server.
In Java, freeing up memory is taken out of the programmers hands; the Java Virtual Machine
keeps track of all used memory. When memory is not used any more it is automatically freed
up. A separate task is running in the background by the JVM, freeing up unreferenced,
unused memory. That task is called Garbage Collection.
The Garbage Collector is always running. This automatic memory garbage collection feature
makes it easy to write robust server side programs in Java. The only thing the programmer
has to watch for is the speed of object creation. If the application is creating objects faster
than the Garbage Collector can free them, it can cause memory problems. Depending on
how the JVM is configured, the application either can run out of memory by throwing the
NotEnoughMemoryException, or can halt to give time for the Garbage Collector to do its
job.

5.9 Applet
The Java creators created the concept of the applet. A Java program can be run in a client
browser program. Java was released in 1995; the time when the Internet was becoming

28
Forbidden bad practices

more available and familiar to the general public. The promise of Java was in the client
browser-side in that code would be downloaded and executed as a Java applet in the client
browser program.
See also Java Programming/Applets8 .

5.10 Forbidden bad practices


Over the years, some features in C/C++ programming became abused by the programmers.
Although the language allows it, it was known as bad practices. So the creators of Java
have excluded them from the language:
• Operator overloading
• Multiple inheritance
• Friend classes (access another object's private members)
• Restrictions of explicit type casting (related to memory management)

5.11 Evaluation
In most people's opinions, Java technology delivers reasonably well on all these goals. The
language is not, however, without drawbacks. Java tends to be more high-level than sim-
ilar languages (such as C++), which means that the Java language lacks features such as
hardware-specific data types, low-level pointers to arbitrary memory addresses, or program-
ming methods like operator overloading. Although these features are frequently abused or
misused by programmers, they are also powerful tools. However, Java technology includes
Java Native Interface9 (JNI), a way to call native code from Java language code. With JNI,
it is still possible to use some of these features.
Some programmers also complain about Java's lack of multiple inheritance, a powerful fea-
ture of several other object-oriented languages, such as C++. The Java language separates
inheritance of type and implementation, allowing inheritance of multiple type definitions
through interfaces, but only single inheritance of type implementation via class hierarchies.
This allows most of the benefits of multiple inheritance while avoiding many of its dangers.
In addition, through the use of concrete classes, abstract classes, as well as interfaces, a
Java language programmer has the option of choosing full, partial, or zero implementation
for the object type they define, thus ensuring maximum flexibility in application design.
There are some who believe that for certain projects, object orientation makes work harder
instead of easier. This particular complaint is not unique to the Java language but applies
to other object-oriented languages as well.

8 [Link]
9 [Link]

29
6 The Java Platform

The Java platform is the name given to the computing platform from Oracle that helps
users to run and develop Java applications. The platform does not just enable a user to
run and develop a Java application, but also features a wide variety of tools that can help
developers work efficiently with the Java programming language.
The platform consists of two essential pieces of software:
• the Java Runtime Environment (JRE), which is needed to run Java applications
and applets; and,
• the Java Development Kit (JDK), which is needed to develop those Java applications
and applets. If you have installed the JDK, you should know that it comes equipped with
a JRE as well. So, for all the purposes of this book, you would only require the JDK.
In this section, we will explore in further detail what these two software components of the
Java platform do.

6.1 Java Runtime Environment (JRE)


Any piece of code written in the Java programming language can be run on any operating
system, platform or architecture — in fact, it can be run on any device that supports the
Java platform. Before Java, this amount of ubiquity was very hard to achieve. If a software
was written for a Unix-based system, it was impossible to run the same application on a
Windows system — in this case, the application was native only to Unix-based systems.
A major milestone in the development of the Java programming language was to develop
a special runtime environment that would execute any Java application independent of the
computer's operating system, platform or architecture.
The Java Runtime Environment (JRE) sits on top of the machine's operating system,
platform and architecture. If and when a Java application is run, the JRE acts as a liaison
between the underlying platform and that application. It interprets the Java application to
run in accordance with the underlying platform, such that upon running the application,
it looks and behaves like a native application. The part of the JRE that accomplishes this
complex liaison agreement is called the Java Virtual Machine (JVM).
Figure 1: Java applications can be written once and run anywhere. This feature of the
Java platform
is commonly abbreviated to WORA in formal Java texts.

31
The Java Platform

Figure 43

6.1.1 Executing native Java code (or byte-code)


Native Java applications are preserved in a special format called the byte-code1 . Byte-code
remains the same, no matter what hardware architecture, operating system, or software
platform it is running under. On a file-system, Java byte-code resides in files that have the
.class (also known as a class file) or the .jar (also known as a Java archive) extension.
To run byte-code, the JRE comes with a special tool (appropriately named java).
Suppose your byte-code is called [Link]. If you want to execute this
Java byte-code, you would need to use the following command in Command Prompt (on
Windows) or Terminal (on Linux or Mac OS):

Console:
Execution
$ java SomeApplication

1 [Link]

32
Java Runtime Environment (JRE)

If you want to execute a Java byte-code with a .jar extension (say, [Link]
), you would need to use the following command in Command Prompt (on Windows) or
Terminal (on Linux or Mac OS):

Console:
Execution with a jar
$ java -jar [Link]

Note:
Not all Java class files or Java archives are executable. Therefore, the java tool would
only be able to execute files that are executable. Non-executable class files and Java
archives are simply called class libraries.

6.1.2 Do you have a JRE?


Most computers come with a pre-installed copy of the JRE. If your computer doesn't have a
JRE, then the above commands would not work. You can always check what version of the
JRE is installed on the computer by writing the following command in Command Prompt
(on Windows) or Terminal (on Linux or Mac OS):

Console:
Java version
$ java -version

6.1.3 Java Virtual Machine (JVM)


Quite possibly, the most important part of the JRE is the Java Virtual Machine (JVM)
. The JVM acts like a virtual processor, enabling Java applications to be run on the local
system. Its main purpose is to interpret (read translate) the received byte-code and make
it appear as native code. The older Java architecture used this process of interpretation
to execute Java byte-code. Even though the process of interpretation brought the WORA
principle to diverse machines, it had a drawback — it consumed a lot of time and clocked
the system processor intensively to load an application.
Figure 2: A JVM interpreter translates the byte-code line-by-line to make it appear as if a
native application is being executed.

33
The Java Platform

Figure 44

Just-in-Time Compilation
Since version 1.2, the JRE features a more robust JVM. Instead of interpreting byte-code, it
down-right converts the code straight into equivalent native code for the local system. This
process of conversion is called just-in-time compilation2 or JIT-compilation. This process
only occurs when the byte-code is executed for the first time. Unless the byte-code itself is
changed, the JVM uses the compiled version of the byte-code on every successive execution.
Doing so saves a lot of time and processor effort, allowing applications to execute much
faster at the cost of a small delay on first execution.
Figure 3: A just-in-time compiler only compiles the byte-code to equivalent native code at
first execution. Upon every successive
execution, the JVM merely uses the already compiled native code to optimize performance.

Figure 45

Native optimization
The JVM is an intelligent virtual processor. It has the ability to identify areas within the
Java code itself that can be optimized for faster and better performance. Based on every
successive run of your Java applications, the JVM would optimize it to run even better.

2 [Link]

34
Java Development Kit (JDK)

Note:
There are portions of Java code that do not require it to be JIT-compiled at runtime,
e.g., the Reflection APIa ; therefore, code that uses such functions are not necessarily
fully compiled to native code.

a [Link]

Was JVM the first virtual machine?


Java was not the first virtual-machine-based platform, though it is by far the most successful
and well-known. Previous uses for virtual machine technology primarily involved emulators3
to aid development for not-yet-developed hardware or operating systems, but the JVM was
designed to be implemented entirely in software, while making it easy to efficiently port an
implementation to hardware of all kinds.

6.2 Java Development Kit (JDK)


The JRE takes care of running the Java code on multiple platforms, however as developers,
we are interested in writing pure code in Java which can then be converted into Java byte-
code for mass deployment. As developers, we do not need to write Java byte-code; rather
we write the code in the Java programming language (which is quite similar to writing C
or C++ code).
Upon downloading the JDK, a developer ensures that their system has the appropriate JRE
and additional tools to help with the development of applications in the Java programming
language. Java code can be found in files with the extension .java. These files are called
Java source files. In order to convert the Java code in these source files to Java byte-code,
you need to use the Java compiler tool installed with your JDK.

6.2.1 The Java compiler


The Java compiler tool (named javac in the JDK) is the most important utility found
with the JDK. In order to compile a Java source file (say, [Link]) to its
respective Java byte-code, you would need to use the following command in Command
Prompt (on Windows) or Terminal (on Linux or Mac OS):

Console:
Compilation
javac [Link]

This command would convert the [Link] source file into its equivalent
Java byte-code. The resultant byte-code would exist in a newly created file named
[Link]. This process of converting Java source files into their equiva-
lent byte-codes is known as compilation.

3 [Link]

35
The Java Platform

Figure 4: The basic Java compilation process

Figure 46

There are a huge array of tools available with the JDK that will all be explained in due
time as you progress with the book. These tools are briefly listed below in order of their
usage:

6.2.2 Applet development


• appletviewer — Java applets require a particular environment to execute. Typically,
this environment is provided by a browser with a Java plug-in, and a web server serving
the applet. However, during development and testing of an applet it might be more
convenient to start an applet without the need to fiddle with a browser and a web server.
In such a case, Oracle's appletviewer from the JDK can be used to run an applet.

6.2.3 Annotation processing


For more about annotation processing, read this4
In Java 1.5 (alias Java 5.0) Oracle added a mechanism called annotations. Annotations
allow the addition of meta-data to Java source code, and even provide mechanisms to carry
that meta-data into compiled .class files.
• apt — An annotation processing tool which digs through source code, finds annotation
statements in the source code and executes actions if it finds known annotations. The
most common task is to generate some particular source code. The actions apt performs
when finding annotations in the source code are not hard-coded into apt. Instead, one has
to code particular annotation handlers (in Java). These handlers are called annotation
processors. It can also be described in a simple way without the Oracle terminology:
apt can be seen as a source code preprocessor framework, and annotation processors are
typically code generators.

6.2.4 Integration of non-Java and Java code


• javah — A Java class can call native, or non-Java, code that has been prepared to
be called from Java. The details and procedures are specified in the JNI (Java Native

4 [Link]

36
Java Development Kit (JDK)

Interface). Commonly, native code is written in C (or C++). The JDK tool javah helps
to write the necessary C code, by generating C header files and C stub code.

6.2.5 Class library conflicts


• extcheck — It can be used prior to the installation of a Java extension into the JDK
or JRE environment. It checks if a particular Jar file conflicts with an already installed
extension. This tool appeared first with Java 1.5.

6.2.6 Software security and cryptography tools


The JDK comes with a large number of tools related to the security features of Java. Usage
of these tools first requires study of the particular security mechanisms. The tools are:
• keytool — To manage keys and certificates
• jarsigner — To generate and verify digital signatures of JARs (Java ARchives)
• policytool — To edit policy files
• kinit — To obtain Kerberos v5 tickets
• klist — To manage Kerberos credential cache and key table
• ktab — To manage entries in a key table

6.2.7 The Java archiver


• jar — (short for Java archiver) is a tool for creating Java archives or jar files — a file
with .jar as the extension. A Java archive is a collection of compiled Java classes and
other resources which those classes may require (such as text files, configuration files,
images) at runtime. Internally, a jar file is really a .zip file5 .

6.2.8 The Java debugger


• jdb — (short for Java debugger) is a command-line console that provides a debugging6
environment for Java programs. Although you can use this command line console, IDE's
normally provide easier to use debugging environments.

6.2.9 Documenting code with Java


As programs grow large and complex, programmers need ways to track changes and to
understand the code better at each step of its evolution. For decades, programmers have
been employing the use of special programming constructs called comments — regions that
help declare user definitions for a code snippet within the source code. But comments are
prone to be verbose and incomprehensible, let alone be difficult to read in applications
having hundreds of lines of code.

5 [Link]
6 [Link]

37
The Java Platform

• javadoc — Java provides the user with a way to easily publish documentation about
the code using a special commenting system and the javadoc tool. The javadoc tool
generates documentation about the Application Programming Interface (API) of a set of
user-created Java classes. javadoc reads source file comments from the .java source files
and generates HTML documents that are easier to read and understand without looking
at the code itself.
• javap — Where Javadoc provide a detailed view into the API and documentation of a
Java class, the javap tool prints information regarding members (constructors, methods
and variables) in a class. In other words, it lists the class' API and/or the compiled
instructions of the class. javap is a formatting disassembler for Java bytecode.

6.2.10 The native2ascii tool


native2ascii is an important, though underappreciated, tool for writing properties files
— files containing configuration data — or resource bundles — files containing language
translations of text.
Such files can contain only ASCII and Latin-1 characters, but international programmers
need a full range of character sets. Text using these characters can appear in properties
files and resource bundles only if the non-ASCII and non-Latin-^1 characters are converted
into Unicode escape sequences7 (\uXXXX notation).
The task of writing such escape sequences is handled by native2ascii. You can write
the international text in an editor using the appropriate character encoding, then use
native2ascii to generate the necessary ASCII text with embedded Unicode escape se-
quences. Despite the name, native2ascii can also convert from ASCII to native, so it
is useful for converting an existing properties file or resource bundle back to some other
encoding.
native2ascii makes most sense when integrated into a build system to automate the
conversion.

6.2.11 Remote Method Invocation (RMI) tools


6.2.12 Java IDL and RMI-IIOP Tools
6.2.13 Deployment & Web Start Tools
6.2.14 Browser Plug-In Tools
6.2.15 Monitoring and Management Tools / Troubleshooting Tools
With Java 1.5 a set of monitoring and management tools have been added to the JDK, in
addition to a set of troubleshooting tools.
The monitoring and management tools are intended for monitoring and managing the virtual
machine and the execution environment. They allow, for example, monitoring memory
usage during the execution of a Java program.

[Link]
7
20Sequences

38
Similar concepts

The troubleshooting tools provide rather esoteric insight into aspects of the virtual machine.
(Interestingly, the Java debugger is not categorized as a troubleshooting tool.)
All the monitoring and management and troubleshooting tools are currently marked as
”experimental” (which does not affect jdb). So they might disappear in future JDKs.

6.2.16 Java class libraries (JCL)


In most modern operating systems, a large body of reusable code is provided to simplify the
programmer's job. This code is typically provided as a set of dynamically loadable libraries8
that applications can call at runtime. Because the Java platform is not dependent on any
specific operating system, applications cannot rely on any of the existing libraries. Instead,
the Java platform provides a comprehensive set of standard class libraries, containing much
of the same reusable functions commonly found in modern operating systems.
The Java class libraries serve three purposes within the Java platform. Like other standard
code libraries, they provide the programmer with a well-known set of functions to perform
common tasks, such as maintaining lists of items or performing complex string parsing.
In addition, the class libraries provide an abstract interface to tasks that would normally
depend heavily on the hardware and operating system. Tasks such as network access and
file access are often heavily dependent on the native capabilities of the platform. The Java
[Link] and [Link] libraries implement the required native code internally, then provide a
standard interface for the Java applications to perform those tasks. Finally, some underlying
platforms may not support all of the features a Java application expects. In these cases,
the class libraries can either emulate those features using whatever is available, or provide
a consistent way to check for the presence of a specific feature.

6.3 Similar concepts


The success of the Java platform and the concepts of the write once, run anywhere principle
has led to the development of similar frameworks and platforms. Most notable of these is
the Microsoft's .NET framework9 and its open-source equivalent Mono10 .

6.3.1 The .NET framework


The .NET framework borrows many of the concepts and innovations of Java — their alterna-
tive for the JVM is called the Common Language Runtime (CLR)11 , while their alternative
for the byte-code is the Common Intermediate Language (CIL)12 . In fact, the .NET plat-
form had an implementation of a Java-like language called Visual J#13 (formerly known as
J++14 ).

8 [Link]
9 [Link]
10 [Link]
11 [Link]
12 [Link]
13 [Link]
14 [Link]

39
The Java Platform

J# is normally not supported with the JVM because instead of compiling it in Java byte-
code, the .NET platform compiles the code into CIL, thus making J# different from the
Java programming language. Furthermore, because J# implements the .NET Base Class
Libraries (BCL) instead of the Java Class Libraries, J# is nothing more than a non-standard
extension of the Java programming language. Due to the lack of interest from developers,
Microsoft had to withdraw their support for J#, and focused on a similar programming
language: C#.

6.3.2 Third-party compilers targeting the JVM


The word Java, by itself, usually refers to the Java programming language which was
designed for use with the Java platform. Programming languages are typically outside of
the scope of the phrase ”platform”. However, Oracle does not encourage the use of any other
languages with the platform, and lists the Java programming language as a core part of
the Java 2 platform. The language and runtime are therefore commonly considered a single
unit.
There are cases where you might want to program using a different language (say, Python15 )
and yet be able to generate Java byte-code (instead of the Python compiled code) to be
run with the JVM. Many third-party programming language vendors provide compilers
that can compile code written in their language to Java byte-code. For instance, Python
developers can use Jython16 compilers to compile Python code to the Java byte-code format
(as illustrated below).
Figure 5: Third-party JVM-targeted compilation for non-Java source compilation to Java
byte-code. Illustrated example
shows Python source being compiled to both Python compiled code and Java byte-code.

Figure 47

15 [Link]
16 [Link]

40
Similar concepts

Of late, JVM-targeted third-party programming and scripting languages have seen tremen-
dous growth. Some of these languages are also used to extend the functionalities of the
Java language itself. A few examples include the following:
• Groovy17
• Pizza18
• GJ19 (Generic Java) − later officially incorporated into Java SE 5.
• NetREXX20

17 [Link]
18 [Link]
19 [Link]
20 [Link]

41
7 Getting started

7.1 Understanding systems


We conceptualize the world around us in terms of systems. A system is a web of intercon-
nected objects working together in tandem. In the systems theory1 , a system is set out as
a single entity within a world surrounded by an environment. A system interacts with its
surrounding environment using messages of two distinct types:
• inputs: messages received from the surrounding environment; and,
• outputs: messages given back to the surrounding environment.
Figure 1: A simple system interacting with its environment using input and output
messages.

Figure 48

Life is a complicated mess of interconnected objects sending signals and messages. See the
illustration below in figure 2 demonstrating a complex system for an economic ecosphere
for a single company. Imagine what this system diagram would be like if you were to add
a few more companies and their sub-systems. Computer software systems in general are a
complex web of further interconnected sub-systems − where each sub-system may or may

1 [Link]

43
Getting started

not be divided into further sub-systems. Each sub-system communicates with others using
feedback messages − that is, inputs and outputs.
Figure 2: Example of a complex system with multiple sub-systems and interactions

Figure 49

7.2 The process of abstraction


Programming is essentially thinking of solutions to problems in real life as a system. With
any programming language, you need to know how to address real-life problems into some-
thing that could be accurately represented within a computer system. In order to begin
programming with the Java programming language (or in fact, with any programming lan-
guage), a programmer must first understand the basics of abstraction.
Abstraction is the process of representing real-life problems and objects into your pro-
grams.
Suppose a novelist, a painter and a programmer were asked to abstract (i.e., represent) a
real-life object in their work. Suppose, the real-life object that needs to be abstracted is an
animal. Abstraction for a novelist would include writing the description of the animal whilst
the painter would draw a picture of the animal − but what about a computer programmer?
The Java programming language uses a programming paradigm called object-oriented pro-
gramming (OOP)2 , which shows you exactly what a programmer needs to be doing. Ac-
cording to OOP, every object or problem in real-life can be translated into a virtual object
within your computer system.

2 [Link]

44
The process of abstraction

7.2.1 Thinking in objects


In OOP, every abstraction of a real-life object is simply called an object within your code.
An object is essentially the most basic representation of a real-life object as part of a
computer system. With Java being an object-oriented language, everything within Java is
represented as an object. To demonstrate this effect, if you were to define an abstraction of
an animal in your code, you would write the following lines of code (as you would for any
other abstraction):

class Animal { }

The code above creates a space within your code where you can start defining an object;
this space is called a class (or type) definition. All objects need to be defined using a
class definition in order for them to be used in your program. Notice the curly brackets
− anything you write within these brackets would serve as a definition or specification for
your object. In the case of the example above, we created a class definition called Animal
for objects that could serve as an abstract representation of any animal in real-life. The
way that a Java environment evaluates this code to be a class definition is by looking at
the prefix word we used to begin our class definition (i.e., class). Such predefined words
in the Java language are known as keywords and make up the grammar for the language
(known as programming syntax).

Note:
Class definitions have different names in different languages. They are sometimes called
type definitions, object specifications or templates as well

7.2.2 Understanding class definitions and types


Aristotle3 was perhaps the first person to think of abstract types or typologies of objects.
He started calling them classes − e.g., classes of birds, classes of mammals. Class definitions
therefore serve the purpose well in defining the common characteristics or types of objects
you would be creating. Upon declaring a class definition, you can create objects based on
that definition. In order to do so however, you need to write a special syntax that goes like
this:
Animal dog = new Animal();

The code above effectively creates an object called dog based on the class definition for
Animal. In non-programmer parlance, the code above would translate into something akin
to saying, ”Create a new object dog of type Animal.” A single class definition enables you
to create multiple objects as the code below indicates:
Animal dog = new Animal();
Animal cat = new Animal();
Animal camel = new Animal();

3 [Link]

45
Getting started

Basically, you just have to write the code for your class or type definition once, and then use
it to create countless numbers of objects based on that specification. Although you might
not grasp the importance of doing so, this little exercise saves you a lot of time (a luxury
that was not readily available to programmers in the pre-Java days).

7.2.3 Expanding your class definitions


Although each object you create from a class definition is essentially the same, there has to
be a way of differentiating those objects in your code. Object fields (or simply fields) are
what makes your objects unique from other objects. Let's take our present abstraction for
instance. An animal could be a dog, cat, camel or a duck but since this abstraction is of a
very generic kind, you need to define fields that are common to all of these animals and yet
makes the animals stand apart. For instance, you can have two fields: name (a common
name given to any one of these animals) and legs (the number of limbs any one of these
animals would require to walk). As you start defining your objects, they start to look like
this:

class Animal {

String name;
int legs;
}

In the code above you defined two object fields:


• a field called name of type String; and,
• a field called legs of type int.
These special pre-defined types are called data types. The String data type is used for
fields that can hold textual values like names, while the int (integer) data type is used for
fields that can hold numeric values

Note:
Fields are called different things in different languages. They may be called state
identifiers, properties or member variables in other programming language syntax.
Java uses the words fields and properties in different contexts, as would be understood
from upcoming sections.

Figure 3: In order to denote the Animal object as a system within the Java Environment,
you present it as such. Note how fields are presented.

46
The process of abstraction

Figure 50

In order to demonstrate how fields work, we will go ahead and create objects from this
amended version of our class definition as such:
Animal animal1 = new Animal();
Animal animal2 = new Animal();

[Link] = "dog";
[Link] = 4;

[Link] = "duck";
[Link] = 2;

You can access the fields of your created objects by using the . (dot) or membership
operator. In the example above, we created two objects: animal1 and animal2 of type
Animal. And since, we had established that each Animal has two fields namely name and
legs, we accessed and modified these fields for each of our objects using the membership
operator to set the two apart. By declaring different values for different objects, we can
manipulate their current state. So, for instance:
• the animal1 object is a "dog" with 4 legs to walk with; while,
• the animal2 object is a "duck" with 2 legs to walk with.
What sets the two objects apart is their current state. Both the objects have different states
and thus stand out as two different objects even though they were created from the same
template or class definition.

47
Getting started

7.2.4 Adding behavior to objects


At this point, your objects do nothing more than declare a bunch of fields. Being a system,
your objects should have the ability to interact with their environment and other systems
as well. To add this capability for interaction, you need to add interactive behavior to
your object class definitions as well. Such behavior is added to class definitions using a
programming construct called method.
In the case of the Animal, you require your virtual representation of an animal to be able
to move through its environment. Let's say, as an analogy, you want your Animal object to
be able to walk in its environment. Thus, you need to add a method named walk to our
object. To do so, we need to write the following code:

class Animal {

String name;
int legs;

void walk() { }
}

As you write this code, one thing becomes immediately apparent. Just like the class de-
scription, a method has curly brackets as well. Generally, curly brackets are used to define
an area (or scope) within your object. So the first set of curly brackets defined a scope for
your class definition called the class-level scope. This new set of curly brackets alongside
a method defines a scope for the further definition of your method called the method-level
scope.
In this instance, the name of our method is walk. Notice however that the name of our
method also features a set of round brackets as well. More than just being visual identifiers
for methods, these round brackets are used to provide our methods with additional input
information called arguments.
A method therefore enables an object to:
1. Accept input: Receive some argument(s);
2. Process information: work on the received argument(s) within its curly brackets; and,
3. Generate ouput: occasionally, return something back.
In essence, methods are what makes an object behave more like a system.
Notice the keyword void before the name of the method − this tells us that the method
walk returns nothing. You can set a method to return any data type − it can be a String
or an int as well.

Note:
Methods are known by different names in different programming language. They might
be called functions, procedures, routines or behaviors.

48
The process of encapsulation

Figure 4: The Animal object can now be denoted as having an interaction behavior within
the Java Environment
as illustrated here. Note the difference between the presentation of fields and methods.

Figure 51

7.3 The process of encapsulation


By now, we thoroughly understand that any object can interact with its environment and in
turn be influenced by it. In our example, the Animal object exposed certain fields − name
and legs, and a method − walk() to be used by the environment to manipulate the object.
This form of exposure is implicit. Using the Java programming language, a programmer
has the power to define the level of access other objects and the environment have on a
certain object.

7.3.1 Using access modifiers


Alongside declaring and defining objects, their fields and methods, a programmer also has
the ability to define the levels of access on those elements. This is done using keywords
known as access modifiers.
Let's modify our example to demonstrate this effect:
class Animal {

public String name;


public int legs;

public void walk() { }

49
Getting started

By declaring all fields and methods public, we have ensured that they can be used outside
the scope of the Animal class. This means that any other object (other than Animal) has
access to these member elements. However, to restrict access to certain member elements
of a class, we can always use the private access modifier (as demonstrated below).
class Animal {

private String name;


private int legs;

public void walk() { }


}

In this example, the fields name and legs can only be accessed within the scope of the
Animal class. No object outside the scope of this class can access these two fields. However,
since the walk() method still has public access, it can be manipulated by actors and
objects outside the scope of this class. Access modifiers are not just limited to fields or
methods, they can be used for class definitions as well (as is demonstrated below).
public class Animal {

private String name;


private int legs;

public void walk() { }


}

The following list of keywords show the valid access modifiers that can be used with a Java
program:

keyword description
public Opens access to a certain field or method to be used outside the scope
of the class.
private Restricts access to a certain field or method to only be used within the
scope of the class.
protected Access to certain field or methods is reserved for classes that inherit
the current class.
More on this would be discussed in the section on inheritance.

50
8 Installation

In order to make use of the content in this book, you would need to follow along each and
every tutorial rather than simply reading through the book. But to do so, you would need
access to a computer with the Java platform installed on it — the Java platform is the
basic prerequisite for running and developing Java code, thus it is divided into two essential
pieces of software:
• the Java Runtime Environment (JRE), which is needed to run Java applications
and applets;
• the Java Development Kit (JDK), which is needed to develop those Java applications
and applets.
However as a developer, you would only require the JDK which comes equipped with a JRE
as well.
As Java is just a programming language that allows you to program the computer, it
has multiple implementations available. The most popular implementation of JDK and
JRE are the ”Oracle Java SE” (formerly known as Sun JDK), maintained by Oracle as a
commercial release. However another similarly popular implementation is the OpenJDK,
with the benefit of being free software that could distribute freely under GPL v2 without
the requirement of accepting the ”Oracle Binary Code License Agreement for the Java SE
Platform Products and JavaFX”. The third option - the GCJ, as part of the GNU Compiler
Collection, would also supply the JDK and JRE.
Given below are installation instruction for the Oracle Java SE JDK for various operating
systems:

Availability check for JRE


The Java Runtime Environment (JRE) is necessary to execute Java programs. To check
which version of Java Runtime Environment (JRE) you have, follow the steps below.

1. For Windows Vista or Windows 7, click Start › Control Panel › System and
Maintenance › System.
For Windows XP, click Start › Control Panel › System.
For Windows 2000, click Start › Settings › Control Panel › System.
Alternatively, you can also press Win + R to open the Run dialog. With the
dialog open, type cmd at the prompt:

51
Installation

2. In the command window with black background graced with white text, type
the following command:
Console:
JRE availability check
java -version

If you get an error, such as:


Console:
Other output error
Bad command or file name

..then the JDK may not be installed or it may not be in your path.

To learn more about the Command Prompt syntax, take a look at this MS-DOS tutorial1 .
You may have other versions of Java installed; this command will only show the first in your
PATH. You will be made familiar with the PATH environment variable later in this text.
For now, if you have no idea what this is all about. Read through towards the end and we
will provide you with a step-by-step guide on how to set your own environment variables.
You can use your system's file search utilities to see if there is a [Link] executable
installed. If it is, and it is a recent enough version (Java 1.4.2 or Java 1.5, for example), you
should put the bin directory that contains javac in your system path. The Java runtime,
java, is often in the same bin directory.
If the installed version is older (i.e. it is Java 1.3.1 or Java 1.4.2 and you wish to use the
more recent Java 5 release), you should proceed below with downloading and installing a
JDK.
It is possible that you have the Java runtime (JRE), but not the JDK. In that case the
javac program won't be found, but the java -version will print the JRE version number.

Availability check for JDK


Some Windows based systems come built-in with the JRE, however for the purposes of
writing Java code by following the tutorials in this book, you would require the JDK nev-
ertheless. The Java Development Kit (JDK) is necessary to build Java programs. First,
check to see if a JDK is already installed on your system. To do so, first open a command
window and execute the command below.

Console:
Availability check
javac -version

If the JDK is installed and on your executable path, you should see some output which
tells you the command line options. The output will vary depending on which version is
installed and which vendor provided the Java installation.

1 [Link]

52
The process of encapsulation

Advanced availability check options on Windows platform


On a machine using the Windows operating system, one can invoke the Registry Editor
utility by typing REGEDIT in the Run dialog. In the window that opens subsequently,
if you traverse through the hierarchy HKEY_LOCAL_MACHINE2 > SOFTWARE > JavaSoft >
Java Development Kiton the left-hand.
The resultant would be similar to figure 1.23 , with the only exception being the version
entries for the Java Development Kit. At the time of writing this manuscript, the latest
version for the Java Development Kit available from the Internet was 1.7 as seen in the
Registry entry. If you see a resultant window that resembles the one presented above, it
would prove that you have Java installed on your system, otherwise it is not.

Note:
Caution should be exercised when traversing through the Registry Editor. Any changes
to the keys and other entries may change the way your Windows operating system
normally works. Even minor changes may result into catastrophic failures of the normal
working of your machine. Better that you don't modify or tend to modify anything
whilst you are in the Registry Editor.

Download instructions
To acquire the latest JDK (version 7), you can manually download the Java software4 from
the Oracle website.
For the convenience of our readers, the following table presents direct links to the latest
JDK for the Windows operating system.

Operating sys- Setup Installer License


tem
Windows x86 Download5 Oracle Binary Code License
Agreement6
Windows x64 Download7 Oracle Binary Code License
Agreement8

You must follow the instructions for the setup installer wizard step-by-step with the de-
fault settings to ensure that Java is properly installed on your system. Once the setup is
completed, it is highly recommended to restart your Windows operating system.
If you kept the default settings for the setup installer wizard, your JDK should
now be installed at C:\Program Files\Java\jdk1.7.0_01. You would require

2 [Link]
3 Chapter 8 on page 54
4 [Link]
5 [Link]
[Link]
6
[Link]
7 [Link]
[Link]
8
[Link]

53
Installation

the location to your bin folder at a later time — this is located at C:\Program
Files\Java\jdk1.7.0_01\bin It may be a hidden file, but no matter. Just don't use
Program Files (x86)\ by mistake unless that's where the files were installed by Java.

Updating environment variables


In order for you to start using the JDK compiler utility with the Command Prompt, you
would need to set the environment variables that points to the bin folder of your recently
installed JDK. To set permanently your environment variables, follow the steps below.

1. To open System Properties dialog box use, the Control Panel or type the fol-
lowing command in the command window:
Console:
System properties
rundll32 [Link],Control_RunDLL [Link]

2. Navigate to the Advanced tab on the top, and select Environment Vari-
ables...
3. Under System variables, select the variable named Path and click Edit...
4. In the Edit System Variable dialog, go to the Variable value field. This
field is a list of directory paths separated by semi-colons (;).
5. To add a new path, append the location of your JDK bin folder separated by a
semi-colon (;).
6. Click OK on every opened dialog to save changes and get past to where you
started.

Start writing code


Once you have successfully installed the JDK on your system, you are ready to program
code in the Java programming language. However, to write code, you would need a decent
text editor. Windows comes with a default text editor by default — Notepad. In order to
use notepad to write code in Java, you need to follow the steps below:

1. Click Start › All Programs › Accessories › Notepad to invoke the applica-


tion.
Alternatively, you can also press Win + R to open the Run dialog. With the
dialog open, type the following command at the prompt:
Console:
Notepad launching
notepad

2. Once the Notepad application has fired up, you can use the editor to write
code for the Java programming language.

54
The process of encapsulation

Availability check for JRE


The Java Runtime Environment (JRE) is necessary to execute Java programs. To check
which version of JRE you have, follow the steps below.

1. Open the Terminal window.


2. Type the following command:
Console:
JRE availability check
java -version

If you get something like this:


Console:
Output on a particular Kubuntu 12.10 installation, with OpenJDK
as the provider of JDK and JRE
java version "1.7.0_09"
OpenJDK Runtime Environment (IcedTea7 2.3.3) (7u9-2.3.3-0ubuntu1~12.10.1)
OpenJDK Client VM (build 23.2-b09, mixed mode, sharing)

... then a JRE is installed. If you get an error, such as:


Console:
Output error
java: command not found

... then the JDK may not be installed or it may not be in your PATH.

You may have other versions of Java installed; this command will only show the first in your
PATH. You will be made familiar with the PATH environment variable later in this text.
For now, if you have no idea what this is all about, read through towards the end and we
will provide you with a step-by-step guide on how to set your own environment variables.
You can use your system's file search utilities to see if there is a javac executable installed.
If it is, and it is a recent enough version, you should put the bin directory that contains
javac in your system PATH. The Java runtime, java, is often in the same bin directory.
If the installed version is older (i.e. it is Java 5 and you wish to use the more recent Java 7
release), you should proceed below with downloading and installing a JDK.
It is possible that you have the Java runtime (JRE), but not the JDK. In that case the
javac program won't be found, but the java -version will print the JRE version number.

Availability check for JDK


The Java Development Kit (JDK) is necessary to build Java programs. For our purposes,
you must use a JDK. First, check to see if a JDK is already installed on your system. To
do so, first open a terminal window and execute the command below.

55
Installation

Console:
Availability check
javac -version

If the JDK is installed and on your executable path, you should see some output which
tells you the command line options. The output will vary depending on which version is
installed and which vendor provided the Java installation.

Installation using Terminal


Downloading and installing the Java platform on Linux machines is very easy and straight-
forward. You have two choices to install the Java platforms: using a package manager such
as DPKG/APT, YUM/RPM etc., or directly install them using the binary package. To use
the terminal to download and install the Oracle Java SE platform, follow the instructions
below.

1. Open the Terminal window.


2. At the prompt, type in the line followed by the name of your package manager
as shown below:
Console:
Retrieving the java packages
# APT - Ubuntu, Linux Mint
$ sudo add-apt-repository ppa:webupd8team/java
$ sudo apt update
$ sudo apt-get install oracle-java8-installer
# APT - Debian, etc.
$ echo "deb [Link] xenial main" | sudo
tee /etc/apt/[Link].d/[Link]
$ echo "deb-src [Link] xenial main" |
sudo tee -a /etc/apt/[Link].d/[Link]
$ sudo apt-key adv --keyserver hkp://[Link] --recv-keys
EEA14886
$ sudo apt-get update
$ sudo apt-get install oracle-java8-installer
# Portage - Gentoo
# You need to accept the license and fetch the source code manually from
[Link]
# and save it to /usr/portage/distfiles
$ echo "dev-java/oracle-jdk-bin Oracle-BCLA-JavaSE" | sudo tee -a
/etc/portage/[Link] # Accept the Oracle License
$ emerge oracle-jdk-bin
# Pacman - Arch
$ sudo pacman -S jdk8-openjdk

3. All Java softwares should be installed and instantly available now.

Download instructions
Alternatively, you can manually download the Java software9 from the Oracle website.

9 [Link]

56
The process of encapsulation

For the convenience of our readers, the following table presents direct links to the latest
JDK for the Linux operating system.

Operating sys- RPM Tarball License


tem
Linux x86 Download10 Download11 Oracle Binary Code
License Agreement12
Linux x64 Download13 Download14 Oracle Binary Code
License Agreement15

Start writing code


The most widely available text editor on GNOME desktops is Gedit, while on the KDE
desktops, one can find Kate. Both these editors support syntax highlighting and code
completion and therefore are sufficient for our purposes.
However, if you require a robust and standalone text-editor like the Notepad++ editor on
Windows, you would require the use of the minimalistic editor loaded with features −
SciTE. Follow the instructions below if you wish to install SciTE:

1. Open the Terminal window.


2. At the prompt, write the following:
Console:
Retrieving the java packages
$ sudo apt-get install scite

3. You should now be able to use SciTE for your programming needs. You may
also want to try Geany. Installation instructions are similar to those for SciTE.

On Mac OS, both the JRE and the JDK are already installed. However, the version installed
was the latest version when the computer was purchased, so you may want to update it.

Updating Java for Mac OS


1. Go to the Java download page16 .
2. Mechanically accept Oracle's license agreement.
3. Click on the link for Mac OS X.
4. Run the installer package.

10 [Link]
11 [Link]
[Link]
12
[Link]
13 [Link]
14 [Link]
[Link]
15
[Link]
16 [Link]

57
Installation

Availability check for JDK


The Java Development Kit (JDK) is necessary to build Java programs. For our purposes,
you must use a JDK. First, check to see if a JDK is already installed on your system. To
do so, first open a terminal window and execute the command below.

Console:
Availability check
java -version

If the JDK is installed and on your executable path, you should see some output which
tells you the command line options. The output will vary depending on which version is
installed and which vendor provided the Java installation.
If you already have the JRE installed, you can use the Java Wiki Integrated Development
Environment (JavaWIDE) to code directly in your browser, no account or special software
required.
Click here to visit the JavaWIDE Sandbox to get started.17
For more information, click here to visit the JavaWIDE site.18

17 [Link]
18 [Link]

58
9 Compilation

In Java, programs are not compiled into executable files; they are compiled into bytecode1
(as discussed earlier2 ), which the JVM (Java Virtual Machine) then executes at runtime.
Java source code is compiled into bytecode when we use the javac compiler. The bytecode
gets saved on the disk with the file extension .class. When the program is to be run, the
bytecode is converted, using the just-in-time3 (JIT) compiler. The result is machine code
which is then fed to the memory and is executed.
Java code needs to be compiled twice in order to be executed:
1. Java programs need to be compiled to bytecode.
2. When the bytecode is run, it needs to be converted to machine code.
The Java classes/bytecode are compiled to machine code and loaded into memory by the
JVM when needed the first time. This is different from other languages like C/C++ where
programs are to be compiled to machine code and linked to create an executable file before
it can be executed.

9.1 Quick compilation procedure


To execute your first Java program, follow the instructions below:

1. Proceed only if you have successfully installed and configured your system for
Java as discussed here4 .
2. Open your preferred text editor — this is the editor you set while installing the
Java platform.
For example, Notepad or Notepad++ on Windows; Gedit, Kate or SciTE
on Linux; or, XCode on Mac OS, etc.
3. Write the following lines of code in a new text document:Code listing 2.5: Hel-
[Link]

public class HelloWorld {


public static void main(String[] args) {
[Link](”Hello World!”);
}
}

1 [Link]
2 Chapter 5.11 on page 29
3 [Link]
4 Chapter 7.3.1 on page 50

59
Compilation

4. Save the file as [Link] — the name of your file should be the same
as the name of your class definition5 and followed by the .java extension. This
name is case-sensitive, which means you need to capitalize the precise letters
that were capitalized in the name for the class definition.
5. Next, open your preferred command-line application.
For example, Command Prompt on Windows; and, Terminal on Linux and
Mac OS.
6. In your command-line application, navigate to the directory where you just cre-
ated your file. If you do not know how to do this, consider reading through our
crash courses for command-line applications for Windows6 or Linux7 .
7. Compile the Java source file using the following command which you can copy
and paste in if you want:Compilation
javac [Link]

1 Warning
If you obtain an error message like error: cannot read:
[Link] 1 error, your file is not in the current folder or it is
badly spelled. Did you navigate to the program's location in the command
prompt using the cd8 (change directory) command?If you obtain another
message ending by 1 error or ... errors, there may be a mistake in your
code. Are you sure all words are spelled correctly and with the exact case as
shown? Are there semicolons and brackets in the appropriate spot? Are you
missing a quote? Usually, modern IDEs would try coloring the entire source
as a quote in this [Link] your computer emits beeps, then you may have
illegal characters in your [Link] no [Link] file has
been created in the same folder, then you've got an error. Are you launching
the javac program correctly?

8. Once the compiler returns to the prompt, run the application using the following
command:Execution
java HelloWorld

1 Warning
If you obtain an error message like Exception in thread "main"
[Link]: HelloWorld, the [Link]
file is not in the current folder or it is badly [Link] you obtain an error
message like Exception in thread "main"
[Link]: main, your source file may have been
badly written.

5 Chapter 34.1 on page 261


6 [Link]
7 [Link]
8 [Link]

60
Automatic Compilation of Dependent Classes

9. The above command should result in your command-line application displaying


the following result:Output
Hello World!

Ask for help if the program did not execute properly in the Discussion page9 for this chapter
.

9.2 Automatic Compilation of Dependent Classes


In Java, if you have used any reference to any other java object, then the class for that
object will be automatically compiled, if that was not compiled already. These automatic
compilations are nested, and this continues until all classes are compiled that are needed to
run the program. So it is usually enough to compile only the high level class, since all the
dependent classes will be automatically compiled.
Main class compilation

javac ... [Link]

However, you can't rely on this feature if your program is using reflection to create objects,
or you are compiling for servlets or for a ”jar”, package. In these cases you should list these
classes for explicit compilation.
Main class compilation

javac ... [Link] [Link] ...

9.3 Packages, Subdirectories, and Resources


Each Java top level class belongs to a package (covered in the chapter about Packages10 ).
This may be declared in a package statement at the beginning of the file; if that is missing,
the class belongs to the unnamed package.
For compilation, the file must be in the right directory structure. A file containing a class
in the unnamed package must be in the current/root directory; if the class belongs to a
package, it must be in a directory with the same name as the package.
The convention is that package names and directory names corresponding to the package
consist of only lower case letters.

9.3.1 Top level package


A class with this package declaration

9 [Link]
10 Chapter 25.59 on page 223

61
Compilation

Example:
Code section 2.1: Package declaration
package example;

has to be in a directory named example.

9.3.2 Subpackages
A class with this package declaration

Example:
Code section 2.2: Package declaration with sub-packages
package [Link];

has to be in a directory named en which has to be a sub-directory of wikibooks which


in turn has to be a sub-directory of org resulting in org/wikibooks/en on Linux or
org\wikibooks\en on Windows.
Java programs often contain non-code files such as images and properties files. These are
referred to generally as resources and stored in directories local to the classes in which
they're used. For example, if the class [Link] uses the [Link] file,
this file could be stored as /com/example/resources/[Link]. These resources present
a problem when a program is compiled, because javac does not copy them to wherever
the .class files are being compiled to (see above); it is up to the programmer to move the
resource files and directories.

9.4 Filename Case


The Java source file name must be the same as the public class name that the file contains.
There can be only one public class defined per file. The Java class name is case sensitive,
as is the source file name.
The naming convention for the class name is for it to start with a capital letter.

9.5 Compiler Options


9.5.1 Debugging and Symbolic Information
To debug into Java system classes such as String and ArrayList, you need a special version
of the JRE which is compiled with ”debug information”. The JRE included inside the JDK
provides this info, but the regular JRE does not. Regular JRE does not include this info to
ensure better performance.
Modern compilers do a pretty good job converting your high-level code, with
its nicely indented and nested control structures and arbitrarily typed
variables into a big pile of bits called machine code (or bytecode in case of

62
The JIT compiler

Java), the sole purpose of which is to run as fast as possible on the target CPU
(virtual CPU of your JVM). Java code gets converted into several machine code
instructions. Variables are shoved all over the place – into the stack, into
registers, or completely optimized away. Structures and objects don’t even exist
in the resulting code – they’re merely an abstraction that gets translated to
hard-coded offsets into memory buffers.

So how does a debugger know where to stop when you ask it to break at the
entry to some function? How does it manage to find what to show you when you ask
it for the value of a variable? The answer is – debugging information.

Debugging information is generated by the compiler together with the machine


code. It is a representation of the relationship between the executable program
and the original source code. This information is encoded into a pre-defined
format and stored alongside the machine code. Many such formats were invented
over the years for different platforms and executable files.

Symbolic Information : Symbolic resolution is done at class loading time at linking res-
olution step. It is the process of replacing symbolic references from the type with direct
references. It is done by searching into method area to locate the referenced entity

9.6 The JIT compiler


The Just-In-Time (JIT) compiler is the compiler that converts the byte-code to machine
code. It compiles byte-code once and the compiled machine code is re-used again and again,
to speed up execution. Early Java compilers compiled the byte-code to machine code each
time it was used, but more modern compilers cache this machine code for reuse on the
machine. Even then, java's JIT compiling was still faster than an ”interpreter-language”,
where code is compiled from high level language, instead of from byte-code each time it
was used.
The standard JIT compiler runs on demand. When a method is called repeatedly, the
JIT compiler analyzes the bytecode and produces highly efficient machine code, which runs
very fast. The JIT compiler is smart enough to recognize when the code has already
been compiled, so as the application runs, compilation happens only as needed. As Java
applications run, they tend to become faster and faster, because the JIT can perform
runtime profiling and optimization to the code to meet the execution environment. Methods
or code blocks which do not run often receive less optimization; those which run often (so
called hotspots) receive more profiling and optimization.

63
10 Execution

There are various ways in which Java code can be executed. A complex Java application
usually uses third party APIs or services. In this section we list the most popular ways a
piece of Java code may be packed together and/or executed.

10.1 JSE code execution


Java language first edition came out in the client-server era. Thick clients were developed
with rich GUI interfaces. Java first edition, JSE (Java Standard Edition) had/has the
following in its belt:
• GUI capabilities (AWT, Swing)
• Network computing capabilities (RMI1 )
• Multi-tasking capabilities (Threads)
With JSE the following Java code executions are possible:

Figure 52 Figure 1: Stand alone execution

Stand alone Java application

1 [Link]

65
Execution

(Figure 1) Stand alone application refers to a Java program where both the user interface
and business modules are running on the same computer. The application may or may
not use a database to persist data. The user interface could be either AWT or Swing.
The application would start with a main() method of a Class. The application stops when
the main() method exits, or if an exception is thrown from the application to the JVM.
Classes are loaded to memory and compiled as needed, either from the file system or from
a *.jar file, by the JVM.
Invocation of Java programs distributed in this manner requires usage of the command
line. Once the user has all the class files, he needs to launch the application by the following
command line (where Main is the name of the class containing the main() method.)
Execution of class

java Main

Java 'jar' class libraries


Utility classes, framework classes, and/or third party classes are usually packaged and
distributed in Java ' *.jar' files. These 'jar' files need to be put in the CLASSPATH of the
java program from which these classes are going to be used.
If a jar file is executable, it can be run from the command line:
Execution of archive

java -jar [Link]

Client Server applications


The client server applications consist of a front-end, and a back-end part, each running on
a separate computer. The idea is that the business logic would be on the back-end part of
the program, which would be reused by all the clients. Here the challenge is to achieve a
separation between front-end user interface code, and the back-end business logic code.
The communication between the front-end and the back-end can be achieved by two ways.
• One way is to define a data communication protocol2 between the two tiers. The back-
end part would listen for an incoming request. Based on the protocol3 it interprets the
request and sends back the result in data form.
• The other way is to use Java Remote Invocation4 (RMI). With the use of RMI, a remote
object can be created and used by the client. In this case Java objects are transmitted
across the network.
More information can be found about client-server programming, with sample code, at the
Client Server Chapter5 in this book.

2 [Link]
3 [Link]
4 [Link]
5 [Link]

66
J2EE code execution

Web Applications
For applications needed by lots of client installations, the client-server model did not work.
Maintaining and upgrading the hundreds or thousands of clients caused a problem. It was
not practical. The solution to this problem was to create a unified, standard client, for all
applications, and that is the Browser6 .
Having a standard client, it makes sense to create a unified, standard back-end service as
well, and that is the Application Server7 .
Web Application is an application that is running in the Application Server8 , and it can
be accessed and used by the Browser9 client.
There are three main area of interest in Web Applications, those are:
• The Web Browser10 . This is the container of rendering HTML text, and running client
scripts
• The HTTP11 protocol12 . Text data are sent back and forth between Browser and the
Server
• The Web server13 to serve static content, Application server14 to serve dynamic content
and host EJB15 s.
Wikipedia also has an article about Web application16 .

10.2 J2EE code execution


As the focus was shifting from reaching GUI clients to thin client applications, with Java
version 2, Sun introduced J2EE (Java 2 Extended Edition). J2EE added :
• Components Base Architecture17 , (Servlet, JSP, EJB Containers)
With J2EE the following Java component executions are possible:

6 [Link]
7 [Link]
8 [Link]
9 [Link]
10 [Link]
11 [Link]
12 [Link]
13 [Link]
14 [Link]
15 [Link]
16 [Link]
17 [Link]

67
Execution

Figure 53 Figure 2: Servlet Execution

Java Servlet code


(Figure 2) Java got its popularity with server side programming, more specifically with
J2EE18 servlets. Servlets are running in a simple J2EE framework to handle client HTTP19
requests. They are meant to replace CGI programming20 for web pages rendering dynamic
content.
The servlet is running in a so called servlet-container/web container21 . The servlet's re-
sponsibility is to:
• Handle the request by doing the business logic computation,
• Connecting to a database if needed,
• Create HTML to present to the user through the browser
The HTML output represents both the presention logic and the results of the business
computations. This represents a huge problem, and there is no real application relying
only on servlets to handle the presention part of the responsibility. There are two main
solutions to this:
• Use a template tool (Store the presentation part in an HTML file, marking the areas
that need to be replaced after business logic computations).
• Use JSP (See next section)
Wikipedia also has an article about Servlets22 .

18 [Link]
19 [Link]
20 [Link]
21 [Link]
22 [Link]

68
J2EE code execution

Figure 54 Figure 3: Jsp Execution

Java Server Pages (JSP) code


(Figure 3) JSP is an HTML file with embedded Java code inside. The first time the JSP
is accessed, the JSP is converted to a Java Servlet. This servlet outputs HTML which has
inside the result of the business logic computation. There are special JSP tags that helps
to add data dynamically to the HTML. Also JSP technology allows to create custom tags.
Using the JSP technology correctly, business logic computations should not be in the
embedded Java part of the JSP. JSP should be used to render the presentation of the
static and dynamic data. Depending on the complexity of the data, 100% separation is
not easy to achieve. Using custom tags, however may help to get closer to 100%. This is
advocated also in MVC23 architecture (see below).

Figure 55 Figure 4: EJB Execution

EJB code
(Figure 4) In the 1990s, with the client server computing, a trend started, that is to move
away from Mainframe computing. That resulted in many small separate applications in
a Company/Enterprise. Many times the same data was used in different applications. A

23 [Link]

69
Execution

new philosophy, ”Enterprise Computing”, was created to address these issues. The idea
was to create components that can be reused throughout the Enterprise. The Enterprise
Java Beans (EJBs) were supposed to address this.
An EJB is an application component that runs in an EJB container. The client accesses
the EJB modules through the container, never directly. The container manages the life
cycle of the EJB modules, and handles all the issues that arise from network/enterpise
computing. Some of those are security/access control24 , object pooling25 , transaction
management26 , ... .
EJBs have the same problems as any reusable code: they need to be generic enough to
be able to be reused and the changes or maintenance of EJBs can affect existing clients.
Many times EJBs are used unnecessarily when they are not really needed. An EJB should
be designed as a separate application in the enterprise, fulfilling one function.

Figure 56 Figure 5: MVC Execution

Combine J2EE components to create an MVC architecture


This leads us to the three layers/tiers as shown in (Figure 5).
In modern web applications, with lots of static data and nice graphics, how the data is
presented to the user became very important and usually needs the help of a graphic artist.
To help programmers and graphic artists to work together, the separation between data,
code, and how it is presented became crucial.
• The view (User Interface Logic) contains the logic that is necessary to construct the
presentation. This could be handled by JSP technology.

24 [Link]
25 [Link]
26 [Link]

70
Jini

• The servlet acts as the controller and contains the logic that is necessary to process
user events and to select an appropriate response.
• The business logic (model) actually accomplishes the goal of the interaction. This might
be a query or an update to a database. This could be handled by EJB technology.
For more information about MVC, please see MVC27 .

10.3 Jini
After J2EE Sun had a vision about the next step of network computing. That is Jini28 . The
main idea is that in a network environment, there would be many independent services and
consumers. Jini would allow these services/consumers to interact dynamically with each
other in a robust way. The basic features of Jini are:
• No user intervention is needed when services are brought on or offline. (In contrast to
EJBs where the client program has to know the server and port number where the EJB
is deployed, in Jini the client is supposed to find, to discover, the service in the network.)
• Self healing by adapting when services (consumers of services) come and go. (Services
periodically need to renew a lease to indicate that they are still available.)
• Consumers of JINI services do not need prior knowledge of the service's implementation.
The implementation is downloaded dynamically and run on the consumer JVM,
without configuration and user intervention. (For example, the end user may be presented
with a slightly different user interface depending upon which service is being used at the
time. The implementation of the user interface code would be provided by the service
being used.)
A minimal Jini network environment consists of:
• One or more services
• A lookup-service keeping a list of registered services
• One or more consumers
Jini is not widely used at the current writing (2006). There are two possible reasons for
it. One is Jini a bit complicated to understand and to set it up. The other reason is that
Microsoft pulled out from Java, which caused the industry to turn to the use of proprietary
solutions.

27 [Link]
28 [Link]

71
11 Understanding a Java Program

This article presents a small Java program which can be run from the console. It computes
the distance between two points on a plane. You do not need to understand the structure
and meaning of the program just yet; we will get to that soon. Also, because the program
is intended as a simple introduction, it has some room for improvement, and later in the
module we will show some of these improvements. But let's not get too far ahead of
ourselves!

11.1 The Distance Class: Intent, Source, and Use


This class is named Distance, so using your favorite editor or Java IDE1 , first create a file
named [Link], then copy the source below, paste it into the file and save the file.
Code listing 2.1: [Link]

1 public class Distance {


2 private [Link] point0, point1;
3
4 public Distance(int x0, int y0, int x1, int y1) {
5 point0 = new [Link](x0, y0);
6 point1 = new [Link](x1, y1);
7 }
8
9 public void printDistance() {
10 [Link](”Distance between ” + point0 + ” and ” + point1
11 + ” is ” + [Link](point1));
12 }
13
14 public static void main(String[] args) {
15 Distance dist = new Distance(
16 intValue(args[0]), intValue(args[1]),
17 intValue(args[2]), intValue(args[3]));
18 [Link]();
19 }
20
21 private static int intValue(String data) {
22 return [Link](data);
23 }
24 }

At this point, you may wish to review the source to see how much you might be able to
understand. While perhaps not being the most literate of programming languages, some-
one with understanding of other procedural languages such as C, or other object oriented
languages such as C++ or C#, will be able to understand most if not all of the sample
program.

1 Chapter 11.4 on page 85

73
Understanding a Java Program

Once you save the file, compile2 the program: Compilation command

$ javac [Link]

(If the javac command fails, review the installation instructions3 .)


To run the program, you supply it with the x and y coordinates of two points on a plane
separated by a space. For this version of Distance, only integer points are supported. The
command sequence is java Distance <x0 > <y0 > <x1 > <y1 > to compute the distance
between the points (x0 , y0 ) and (x1 , y1 ).

1 Warning
If you get a [Link] exception, some arguments are not
a number. If you get a [Link] exception, you
did not provide enough numbers.

Here are two examples: Output for the distance between the points (0, 3) and (4, 0)

$ java Distance 0 3 4 0
Distance between [Link][x=0,y=3] and [Link][x=4,y=0] is 5.0

Output for the distance between the points (-4, 5) and (11, 19)

$ java Distance -4 5 11 19
Distance between [Link][x=-4,y=5] and [Link][x=11,y=19] is
20.518284528683193

We'll explain this strange looking output, and also show how to improve it, later.

11.2 Detailed Program Structure and Overview


As promised, we will now provide a detailed description of this Java program. We will
discuss the syntax and structure of the program and the meaning of that structure.

11.2.1 Introduction to Java Syntax


Code listing

1 public class Distance {


2 private [Link] point0, point1;
3
4 public Distance(int x0, int y0, int x1, int y1) {
5 point0 = new [Link](x0, y0);
6 point1 = new [Link](x1, y1);

2 Chapter 8 on page 58
3 Chapter 7.3.1 on page 50

74
Detailed Program Structure and Overview

7 }
8
9 public void printDistance() {
10 [Link]("Distance between " + point0 + " and " + point1
11 + " is " + [Link](point1));
12 }
13
14 public static void main(String[] args) {
15 Distance dist = new Distance(
16 intValue(args[0]), intValue(args[1]),
17 intValue(args[2]), intValue(args[3]));
18 [Link]();
19 }
20
21 private static int intValue(String data) {
22 return [Link](data);
23 }
24 }

Figure 2.1: Basic Java syntax.


For a further treatment of the syntax elements of Java, see also Syntax4 .
The syntax of a Java class is the characters, symbols and their structure used to code the
class. Java programs consist of a sequence of tokens. There are different kinds of tokens.
For example, there are word tokens such as class and public which represent keywords5
(in purple above6 ) — special words with reserved meaning in Java. Other words such
as Distance, point0, x1, and printDistance are not keywords but identifiers (in grey).
Identifiers have many different uses in Java but primarily they are used as names. Java also
has tokens to represent numbers, such as 1 and 3; these are known as literals7 (in orange).
String literals8 (in blue), such as "Distance between ", consist of zero or more characters
embedded in double quotes, and operators9 (in red) such as + and = are used to express
basic computation such as addition or String concatenation or assignment. There are also
left and right braces ({ and }) which enclose blocks. The body of a class is one such block.
Some tokens are punctuation, such as periods . and commas , and semicolons ;. You use
whitespace such as spaces, tabs, and newlines, to separate tokens. For example, whitespace
is required between keywords and identifiers: publicstatic is a single identifier with twelve
characters, not two Java keywords.

4 [Link]
5 Chapter 24.4.7 on page 184
6 Chapter 11.2.3 on page 83
7 Chapter 20.1 on page 150
8 [Link]
9 [Link]

75
Understanding a Java Program

11.2.2 Declarations and Definitions

public class Distance {


private [Link] point0, point1;

public Distance(int x0, int y0, int x1, int y1) {


point0 = new [Link](x0, y0);
point1 = new [Link](x1, y1);
}

public void printDistance() {


[Link]("Distance between " + point0 + " and " +
point1
+ " is " + [Link](point1));
}

public static void main(String[] args) {


Distance dist = new Distance(
intValue(args[0]), intValue(args[1]),
intValue(args[2]), intValue(args[3]));
[Link]();
}

private static int intValue(String data) {


return [Link](data);
}

Figure 2.2: Declarations and Definitions.


Sequences of tokens are used to construct the next building blocks of Java classes as shown
above10 : declarations and definitions. A class declaration provides the name and visibility
of a class. In our example, public class Distance is the class declaration. It consists (in
this case) of two keywords, public and class followed by the identifier Distance.
This means that we are defining a class named Distance. Other classes, or in our case,
the command line, can refer to the class by this name. The public keyword is an access
modifier11 which declares that this class and its members may be accessed from other
classes. The class keyword, obviously, identifies this declaration as a class. Java also
allows declarations of interfaces12 and annotations13 .

10 Chapter 11.2.3 on page 83


11 Chapter 39.5 on page 292
12 Chapter 36.2 on page 271
13 [Link]

76
Detailed Program Structure and Overview

The class declaration is then followed by a block (surrounded by curly braces) which provides
the class's definition (in blue in figure 2.214 ). The definition is the implementation of the
class − the declaration and definitions of the class's members. This class contains exactly
six members, which we will explain in turn.
1. Two field declarations, named point0 and point1 (in green)
2. A constructor declaration (in orange)
3. Three method declarations (in red)

Example: Instance Fields


The declaration

Example:
Code section 2.1: Declaration.
1 private [Link] point0, point1;

...declares two instance fields. Instance fields represent named values that are allocated
whenever an instance of the class is constructed. When a Java program creates a Distance
instance, that instance will contain space for point0 and point1. When another Distance
object is created, it will contain space for its own point0 and point1 values. The value of
point0 in the first Distance object can vary independently of the value of point0 in the
second Distance object.
This declaration consists of:
1. The private15 access modifier,
which means these instance fields are not visible to other classes.
2. The type of the instance fields. In this case, the type is [Link].
This is the class Point in the [Link] package.
3. The names of the instance fields in a comma separated list.
These two fields could also have been declared with two separate but more verbose decla-
rations,

Example:
Code section 2.2: Verbose declarations.
1 private [Link] point0;
2 private [Link] point1;

Since the type of these fields is a reference type (i.e. a field that refers to or can hold a
reference to an object value), Java will implicitly initialize the values of point0 and point1
to null when a Distance instance is created. The null value means that a reference value
does not refer to an object. The special Java literal null is used to represent the null value
in a program. While you can explicitly assign null values in a declaration, as in

14 Chapter 11.2.3 on page 83


15 Chapter 25.39 on page 208

77
Understanding a Java Program

Example:
Code section 2.3: Declarations and assignments.
1 private [Link] point0 = null;
2 private [Link] point1 = null;

It is not necessary and most programmers omit such default assignments.

Example: Constructor
A constructor16 is a special method in a class which is used to construct an instance of the
class. The constructor can perform initialization for the object, beyond that which the Java
VM does automatically. For example, Java will automatically initialize the fields point0
and point1 to null.

Example:
Code section 2.4: The constructor for the class
1 public Distance(int x0, int y0, int x1, int y1) {
2 point0 = new [Link](x0, y0);
3 point1 = new [Link](x1, y1);
4 }

The constructor above consists of five parts:


1. The optional access modifier(s)17 .
In this case, the constructor is declared public
2. The constructor name, which must match the class name exactly: Distance in this
case.
3. The constructor parameters.
The parameter list is required. Even if a constructor does not have any parameters,
you must specify the empty list (). The parameter list declares the type and name
of each of the method's parameters.
4. An optional throws clause which declares the exceptions18 that the constructor may
throw. This constructor does not declare any exceptions.
5. The constructor body, which is a Java block (enclosed in {}). This constructor's body
contains two statements.
This constructor accepts four parameters, named x0, y0, x1 and y1. Each parameter
requires a parameter type declaration, which in this example is int for all four parameters.
The parameters in the parameter list are separated by commas.
The two assignments in this constructor use Java's new operatorto allocate two
[Link] objects. The first allocates an object representing the first point, (x0,
y0), and assigns it to the point0 instance variable (replacing the null value that the instance
variable was initialized to). The second statement allocates a second [Link]
instance with (x1, y1) and assigns it to the point1 instance variable.

16 Chapter 22.4 on page 163


17 Chapter 39.5 on page 292
18 [Link]

78
Detailed Program Structure and Overview

This is the constructor for the Distance class. Distance implicitly extends from
[Link]. Java inserts a call to the super constructor as the first executable
statement of the constructor if there is not one explicitly coded. The above constructor
body is equivalent to the following body with the explicit super constructor call:

Example:
Code section 2.5: Super constructor.
1 {
2 super();
3 point0 = new [Link](x0, y0);
4 point1 = new [Link](x1, y1);
5 }

While it is true that this class could be implemented in other ways,


p such as simply storing
the coordinates of the two points and computing the distance as (x1 − x0 )2 + (y1 − y0 )2 ,
this class instead uses the existing [Link] class. This choice matches the abstract
definition of this class: to print the distance between two points on the plane. We take
advantage of existing behavior already implemented in the Java platform rather than im-
plementing it again. We will see later how to make the program more flexible without
adding much complexity, because we choose to use object abstractions here. However, the
key point is that this class uses information hiding. That is, how the class stores its state
or how it computes the distance is hidden. We can change this implementation without
altering how clients use and invoke the class.

Example: Methods
Methods19 are the third and most important type of class member. This class contains
three methods in which the behavior of the Distance class is defined: printDistance(),
main(), and intValue()

The printDistance() method


The printDistance() method prints the distance between the two points to the standard
output (normally the console).

Example:
Code section 2.6: printDistance() method.
1 public void printDistance() {
2 [Link]("Distance between " + point0
3 + " and " + point1
4 + " is " + [Link](point1));
5 }

This instance method executes within the context of an implicit Distance object. The
instance field references, point0 and point1, refer to instance fields of that implicit object.
You can also use the special variable this to explicitly reference the current object. Within

19 Chapter 21.5 on page 156

79
Understanding a Java Program

an instance method, Java binds the name this to the object on which the method is
executing, and the type of this is that of the current class. The body of the printDistance
method could also be coded as

Example:
Code section 2.7: Explicit instance of the current class.
1 [Link]("Distance between " + this.point0
2 + " and " + this.point1
3 + " is " + [Link](this.point1));

to make the instance field references more explicit.


This method both computes the distance and prints it in one statement. The distance
is computed with [Link](point1); distance() is an instance method of the
[Link] class (of which point0 and point1 are instances). The method operates
on point0 (binding this to the object that point0 refers to during the execution of the
method) and accepting another Point as a parameter. Actually, it is slightly more compli-
cated than that, but we'll explain later. The result of the distance() method is a double
precision floating point number.
This method uses the syntax

Example:
Code section 2.8: String concatenation.
1 "Distance between " + this.point0
2 + " and " + this.point1
3 + " is " + [Link](this.point1)

to construct a String to pass to the [Link](). This expression is a series of


String concatenation methods which concatenates Strings or the String representation of
primitive types (such as doubles) or objects, and returns a long string. For example, the
result of this expression for the points (0,3) and (4,0) is the String Output

”Distance between [Link][x=0,y=3] and [Link][x=4,y=0] is 5.0”

which the method then prints to [Link].


In order to print, we invoke the println(). This is an instance method from
[Link], which is the type of the static field out in the class
[Link]. The Java VM binds [Link] to the standard output stream when
it starts a program.

The main() method


The main() method is the main entry point which Java invokes when you start a Java
program from the command line. The command Output

java Distance 0 3 4 0

80
Detailed Program Structure and Overview

instructs Java to locate the Distance class, put the four command line arguments into an
array of String values, then pass those arguments to the public static main(String[])
method of the class. We will introduce arrays shortly. Any Java class that you want to
invoke from the command line or desktop shortcut must have a main method with this
signature or the following signature: public static main(String...).

Example:
Code section 2.9: main() method.
1 public static void main(String[] args) {
2 Distance dist = new Distance(
3 intValue(args[0]), intValue(args[1]),
4 intValue(args[2]), intValue(args[3]));
5 [Link]();
6 }

The main() method invokes the final method, intValue(), four times. The intValue()
takes a single string parameter and returns the integer value represented in the string. For
example, intValue("3") will return the integer 3.
People who do test-first programming or perform regression testing write a main() method
in every Java class, and a main() function in every Python module20 , to run automated
tests. When a person executes the file directly, the main() method executes and runs the
automated tests for that file. When a person executes some other Java file that in turn
imports many other Java classes, only one main() method is executed – the main() method
of the directly-executed file.

The intValue() method


The intValue() method delegates its job to the [Link]() method. The main
method could have called [Link]() directly; the intValue() method simply
makes the main() method slightly more readable.

Example:
Code section 2.10: intValue() method.
1 private static int intValue(String data) {
2 return [Link](data);
3 }

This method is private since, like the fields point0 and point1, it is part of the internal
implementation of the class and is not part of the external programming interface of the
Distance class.

Static vs. Instance Methods


Both the main() and intValue() methods are static methods. The static keyword tells
the compiler to create a single memory space associated with the class. Each individual

[Link]
20
20them%20

81
Understanding a Java Program

object instantiated has its own private state variables and methods but use the same
static methods and members common to the single class object created by the compiler
when the first class object is instantiated or created. This means that the method executes
in a static or non-object context — there is no implicit separate instance available when
the static methods run from various objects, and the special variable this is not avail-
able. As such, static methods cannot access instance methods or instance fields (such as
printDistance()) or point0) directly. The main() method can only invoke the instance
method printDistance() method via an instance reference such as dist.

11.2.3 Data Types


Most declarations have a data type. Java has several categories of data types: reference
types, primitive types, array types, and a special type, void.

Primitive Types
The primitive types21 are used to represent boolean, character, and numeric values. This
program uses only one primitive type explicitly, int, which represents 32 bit signed in-
teger values. The program also implicitly uses double, which is the return type of the
distance() method of [Link]. double values are 64 bit IEEE floating point
values. The main() method uses integer values 0, 1, 2, and 3 to access elements of the com-
mand line arguments. The Distance() constructor's four parameters also have the type
int. Also, the intValue() method has a return type of int. This means a call to that
method, such as intValue(args[0]), is an expression of type int. This helps explain why
the main method cannot call:

1 Warning
Code section 2.11: Wrong type.
1 new Distance(args[0], args[1], args[2], args[3]) // This is an error

Since the type of the args array element is String, and our constructor's parameters must
be int, such a call would result in an error because Java will not automatically convert
values of type String into int values.
Java's primitive types are boolean, byte, char, short, int, long, float and double. Each
of which are also Java language keywords.

Reference Types
In addition to primitive types, Java supports reference type. A reference type is a Java data
type which is defined by a Java class or interface. Reference types derive this name because
such values refer to an object or contain a reference to an object. The idea is similar to
pointers in other languages like C.

21 Chapter 18.8 on page 135

82
Detailed Program Structure and Overview

Java represents sequences of character data, or String22 , with the reference type
[Link] which is most commonly referred to as String. String literals, such
as "Distance between " are constants whose type is String.
This program uses three separate reference types:
1. [Link] (or simply String)
2. Distance
3. [Link]
For more information see chapter: Java Programming/Classes, Objects and Types23 .

Array Types
Java supports arrays24 , which are aggregate types which have a fixed element type (which
can be any Java type) and an integral size. This program uses only one array, String[]
args. This indicates that args has an array type and that the element type is String.
The Java VM constructs and initializes the array that is passed to the main method. See
arrays25 for more details on how to create arrays and access their size.
The elements of arrays are accessed with integer indices. The first element of an array is
always element 0. This program accesses the first four elements of the args array explicitly
with the indices 0, 1, 2, and 3. This program does not perform any input validation, such
as verifying that the user passed at least four arguments to the program. We will fix that
later.

void
void is not a type in Java; it represents the absence of a type. Methods which do not return
values are declared as void methods.
This class defines two void methods:

Example:
Code section 2.12: Void methods
1 public static void main(String[] args) { ... }
2 public void printDistance() { ... }

22 Chapter 22.5 on page 165


23 Chapter 23.8 on page 175
24 Chapter 26.6 on page 227
25 Chapter 26.6 on page 227

83
Understanding a Java Program

11.3 Whitespace
Whitespace in Java is used to separate the tokens in a Java source file. Whitespace is
required in some places, such as between access modifiers26 , type names27 and Identifiers,
and is used to improve readability elsewhere.
Wherever whitespace is required in Java, one or more whitespace characters may be used.
Wherever whitespace is optional in Java, zero or more whitespace characters may be used.
Java whitespace consists of the
• space character ’ ’ (0x20),
• the tab character (hex 0x09),
• the form feed character (hex 0x0c),
• the line separators characters newline (hex 0x0a) or carriage return (hex 0x0d) characters.
Line separators are special whitespace characters in that they also terminate line comments,
whereas normal whitespace does not.
Other Unicode space characters, including vertical tab, are not allowed as whitespace in
Java.

11.3.1 Required Whitespace


Look at the static method intValue:

Example:
Code section 2.13: Method declaration
1 private static int intValue(String data) {
2 return [Link](data);
3 }

Whitespace is required between private and static, between static and int, between int
and intValue, and between String and data.
If the code is written like this:

Example:
Code section 2.14: Collapsed code
1 privatestaticint intValue(String data) {
2 return [Link](data);
3 }

...it means something completely different: it declares a method which has the return type
privatestaticint It is unlikely that this type exists and the method is no longer static,
so the above would result in a semantic error.

26 Chapter 39.5 on page 292


27 Chapter 18.8 on page 135

84
Indentation

11.4 Indentation
Java ignores all whitespace in front of a statement. As this, these two code snippets are
identical for the compiler:

Example:
Code section 2.15: Indented code
1 public static void main(String[] args) {
2 Distance dist = new Distance(
3 intValue(args[0]), intValue(args[1]),
4 intValue(args[2]), intValue(args[3]));
5 [Link]();
6 }
7
8 private static int intValue(String data) {
9 return [Link](data);
10 }

Example:
Code section 2.16: Not indented code
1 public static void main(String[] args) {
2 Distance dist = new Distance(
3 intValue(args[0]), intValue(args[1]),
4 intValue(args[2]), intValue(args[3]));
5 [Link]();
6 }
7
8 private static int intValue(String data) {
9 return [Link](data);
10 }

However, the first one's style (with whitespace) is preferred, as the readability is higher.
The method body is easier to distinguish from the head, even at a higher reading speed.

85
12 Java IDEs

12.1 What is a Java IDE?


A Java IDE (for Integrated Development Environment) is a software application which
enables users to more easily write and debug Java programs. Many IDEs provide features
like syntax highlighting1 and code completion, which help the user to code more easily.

12.2 Eclipse
Eclipse is a Free and Open Source IDE, plus a developer tool framework that can be extended
for a particular development need. IBM was behind its development, and it replaced IBM
VisualAge tool. The idea was to create a standard look and feel that can be extended via
plugins. The extensibility distinguishes Eclipse from other IDEs. Eclipse was also meant
to compete with Microsoft Visual Studio tools. Microsoft tools give a standard way of
developing code in the Microsoft world. Eclipse gives a similar standard way of developing
code in the Java world, with a big success so far. With the online error checking only,
coding can be sped up by at least 50% (coding does not include programming).
The goals for Eclipse are twofold:
1. Give a standard IDE for developing code
2. Give a starting point, and the same look and feel for all other more sophisticated tools
built on Eclipse
IBM's WSAD, and later IBM Rational Software Development Platform, are built on Eclipse.
Standard Eclipse features:
• Standard window management (perspectives, views, browsers, explorers, ...)
• Error checking as you type (immediate error indications, ...)
• Help window as you type (type ., or <ctrl> space, ...)
• Automatic build (changes in source code are automatically compiled, ...)
• Built-in debugger (full featured GUI debugger)
• Source code generation (getters and setters, ...)
• Searches (for implementation, for references, ...)
• Code refactoring (global reference update, ...)
• Plugin-based architecture (ability to build tools that integrate seamlessly with the envi-
ronment, and some other tools)
• ...

1 [Link]

87
Java IDEs

More info: Eclipse2 and Plugincentral3 .

12.3 NetBeans
The NetBeans IDE is a Free and Open Source IDE for software developers. The IDE runs
on many platforms including Windows, GNU/Linux, Solaris and Mac OS X. It is easy to
install and use straight out of the box. You can easily create Java applications for mobile
devices using Mobility Pack in NetBeans. With Netbeans 6.0, the IDE has become one
of the most preferred development tools, whether it be designing a Swing UI, building a
mobile application, an enterprise application or using it as a platform for creating your own
IDE.
More info: netbeans.org4

12.4 JCreator
JCreator is a simple and lightweight JAVA IDE from XINOX Software. It runs only on
Windows platforms. It is very easy to install and starts quickly, as it is a native application.
This is a good choice for beginners.
More info: [Link] or JCre-
ator5

12.5 Processing
Processing is an enhanced IDE. It adds some extra commands and a simplified program-
ming model. This makes it much easier for beginners to start programming in Java. It was
designed to help graphic artists learn a bit of programming without struggling too much.
Processing runs on Windows, GNU/Linux and Mac OS X platforms.
More info: Processing6 .

12.6 BlueJ
BlueJ is an IDE that includes templates and will compile and run the applications for you.
BlueJ is often used by classes because it is not necessary to set classpaths. BlueJ has its
own sets of libraries and you can add your own under preferences. That sets the classpath
for all compilations that come out of it to include those you have added and the BlueJ
libraries.

2 [Link]
3 [Link]
4 [Link]
5 [Link]
6 [Link]

88
Kawa

BlueJ offers an interesting GUI for creation of packages and programs. Classes are repre-
sented as boxes with arrows running between them to represent inheritance/implementation
or if on is constructed in another. The source code is generated by the UML diagram7 or
vice-versa. BlueJ adds all those classes (the project) into the classpath at compile time.
By default it doesn't display the line numbers, so this should be ticked into Options\Pref-
erences...
More info: BlueJ Homesite8

12.7 Kawa
Kawa is basically a Java editor developed by Tek-Tools. It does not include wizards and
GUI tools, best suited to experienced Java programmers in small and midsized development
teams. It looks that there is no new development for Kawa.
See also a javaworld article9

12.8 JBuilder
JBuilder is an IDE with proprietary source code, sold by Embarcadero Technologies. One
of the advantages is the integration with Together, a modeling tool.
More info: Embarcadero10 .

12.9 DrJava
DrJava is an IDE developed by the JavaPLT group at Rice University. It is designed for
students.
For more information see DrJava11 .

12.10 Other IDEs


• w:Geany12
• w:IntelliJ IDEA13
• w:JDeveloper14
• w:jGRASP15

7 [Link]
8 [Link]
9 [Link]
10 [Link]
11 [Link]
12 [Link]
13 [Link]
14 [Link]
15 [Link]

89
Java IDEs

• w:jEdit16
• w:MyEclipse17
• w:Visual Café18
• Gel19
• JIPE20
• Zeus21
• Setu Eye Saving Lightweight(fast)C,C++,JAVA IDE22

16 [Link]
17 [Link]
18 [Link]
19 [Link]
20 [Link]
21 [Link]
22 [Link]

90
13 Language Fundamentals

The previous chapter ”Getting started1 ” was a primer course in the basics of understanding
how Java programming works. Throughout the chapter, we tackled a variety of concepts
that included:
• Objects and class definitions;
• Abstract and data types;
• Properties;
• Methods;
• Class-level and method-level scopes;
• Keywords; and,
• Access modifiers, etc.
From this point on, we will be looking into the above mentioned concepts and many more
in finer detail with a deeper and richer understanding of how each one of them works. This
chapter on Language fundamentals introduces the fundamental elements of the Java
programming language in detail. The discussions in this chapter will use the concepts we
have already gathered from our previous discussions and build upon them in a progressive
manner.

13.1 The Java programming syntax


In linguistics2 , the word syntax (which comes from Ancient Greek3 σύνταξις4 where σύν
[syn] means ”together”, and τάξις [táxis] means ”an ordering”) refers to ”the process of ar-
ranging things”. It defines the principles and rules for constructing phrases and sentences5
in natural languages6 .
When learning a new language, the first step one must take is to learn its programming
syntax. Programming syntax is to programming languages what grammar is to spoken
languages. Therefore, in order to create effective code in the Java programming language,
we need to learn its syntax — its principles and rules for constructing valid code statements
and expressions.

1 Chapter 6.3.2 on page 41


2 [Link]
3 [Link]
4 [Link]
5 [Link]
6 [Link]

91
Language Fundamentals

Java uses a syntax similar to the C programming language7 and therefore if one learns the
Java programming syntax, they automatically would be able to read and write programs in
similar languages — C, C++8 and C#9
The next step one must take when learning a new language is to learn its keywords; by
combining the knowledge of keywords10 with an understanding of syntax11 rules, one can
create statements12 , Programming Blocks, Classes, Interfaces, et al.
Use packages13 to avoid name collisions. To hide as much information as possible use the
access modifiers14 properly.
Create methods15 that do one and if possible only one thing/task. If possible have separate
method that changes the object state.
In an object oriented language, programs are run with objects; however, for ease of use and
for historic reasons, Java has primitive types16 . Primitive Data Types only store values
and have no methods. Primitive Types may be thought of as Raw Data and are usually
embedded attributes inside objects or used as local variables in methods. Because primitive
types are not subclasses of the object superclass, each type has a Wrapper Class which is a
subclass of Object, and can thus be stored in a collection or returned as an object.
Java is a strong type17 checking language. There are two concepts regarding types and
objects. One is the object type and the other the template/class the object was created
from. When an object is created, the template/class is assigned to that object which can
not be changed. Types of an object however can be changed by type casting. Types of an
object is associated with the object reference that referencing the object and determines
what operation can be performed on the object through that object reference. Assigning
the value of one object reference to a different type of object reference is called type casting.
The most often used data structure in any language is a character string. For this reason
java defines a special object that is String18 .
To aggregate same type java objects to an array, java has a special array19 object for that.
Both java objects and primitive types can be aggregated to arrays.

7 [Link]
8 [Link]
9 [Link]
10 Chapter 24.4.7 on page 184
11 [Link]
12 Chapter 13.1 on page 92
13 Chapter 25.59 on page 223
14 [Link]
15 Chapter 21.5 on page 156
16 Chapter 18.8 on page 135
17 Chapter 23.8 on page 175
18 Chapter 22.5 on page 165
19 Chapter 26.6 on page 227

92
14 Statements

Now that we have the Java platform on our systems and have run the first program suc-
cessfully, we are geared towards understanding how programs are actually made. As we
have already discussed, a program is a set of instructions, which are tasks provided to a
computer. These instructions are called statements in Java. Statements can be anything
from a single line of code to a complex mathematical equation. Consider the following line:

Example:
Code section 3.1: A simple assignment statement.
1 int age = 24;

This line is a simple instruction that tells the system to initialize a variable and set its value
as 24. If the above statement was the only one in the program, it would look similar to this:
Code listing 3.1: A statement in a simple class.

1 public class MyProgram


2 {
3 public static void main(String[] args)
4 {
5 int age = 24;
6 }
7}

Java places its statements within a class declaration and, in the class declaration, the
statements are usually placed in a method declaration, as above.

14.1 Variable declaration statement


The simplest statement is a variable declaration:

Example:
Code section 3.2: A simple declaration statement.
1 int age;

It defines a variable that can be used to store values for later use. The first token is the
data type1 of the variable (which type of values this variable can store). The second token

1 Chapter 18.8 on page 135

93
Statements

is the name of the variable, by which you will be referring to it. Then each declaration
statement is ended by a semicolon (;).

14.2 Assignment statements


Up until now, we've assumed the creation of variables as a single statement. In essence, we
assign a value to those variables, and that's just what it is called. When you assign a value
to a variable in a statement, that statement is called an assignment statement (also
called an initialization statement2 ). Did you notice one more thing? It's the semicolon (;),
which is at the end of each statement. A clear indicator that a line of code is a statement
is its termination with an ending semicolon. If one was to write multiple statements, it is
usually done with each statement on a separate line ending with a semicolon. Consider the
example below:

Example:
Code section 3.3: Multiple assignment statements.
1 int a = 10;
2 int b = 20;
3 int c = 30;

You do not necessarily have to use a new line to write each statement. Just like English,
you can begin writing the next statement where you ended the first one as depicted below:

Example:
Code section 3.4: Multiple assignment statements on the same line.
1 int a = 10; int b = 20; int c = 30;

However, the only problem with putting multiple statements on one line is, it's very difficult
to read it. It doesn't look that intimidating at first, but once you've got a significant amount
of code, it's usually better to organize it in a way that makes sense. It would look more
complex and incomprehensible written as it is in Listing 3.4.
Now that we have looked into the anatomy of a simple assignment statement, we can look
back at what we've achieved. We know that...
• A statement is a unit of code in programming.
• If we are assigning a variable a value, the statement is called an assignment statement.
• An assignment statement includes three parts: a data type, the variable name (also called
the identifier) and the value of a variable. We will look more into the nature of identifiers
and values in the section Variables3 later.
Now, before we move on to the next topic, you need to try and understand what the code
below does.

2 [Link]
3 Chapter 17.2 on page 128

94
Assertion

Example:
Code section 3.5: Multiple assignment statements with expressions.
1 int firstNumber = 10;
2 int secondNumber = 20;
3 int result = firstNumber + secondNumber;
4 [Link](result);
5 secondNumber = 30; // This won't change the value of secondNumber. See the
note.
6 [Link](result); // Hence, the result will remain same.

The first two statements are pretty much similar to those in Section 3.34 but with different
variable names. The third however is a bit interesting. We've already talked of variables
as being similar to gift boxes. Think of your computer's memory as a shelf where you put
all those boxes. Whenever you need a box (or variable), you call its identifier (that's the
name of the variable). So calling the variable identifier firstNumber gives you the number
10, calling secondNumber would give you 20 hence when you add the two up, the answer
should be 30. That's what the value of the last variable result would be. The part of the
third statement where you add the numbers, i.e., firstNumber + secondNumber is called
an expression and the expression is what decides what the value is to be. If it's just a
plain value, like in the first two statements, then it's called a literal (the value is literally
the value, hence the name literal).
Note that after the assignment to result its value will not be changed if we assign different
values to firstNumber or secondNumber, like in line 5.
With the information you have just attained, you can actually write a decent Java program
that can sum up values.

14.3 Assertion
An assertion checks if a condition is true:

Example:
Code section 3.6: A return statement.
1 public int getAge()
2 {
3 assert age >= 0;
4 return age;
5 }

Each assert statement is ended by a semi-colon (;). However, assertions are disabled by
default, so you must run the program with the -ea argument in order for assertions to be
enabled (java -ea [name of compiled program]).

4 Chapter 14.11 on page 104

95
Statements

14.4 Program Control Flow


Statements are evaluated in the order as they occur. The execution of flow begins at the top
most statement and proceed downwards till the last statement is encountered. A statement
can be substituted by a statement block. There are special statements that can redirect
the execution flow based on a condition, those statements are called branching statements,
described in detail in a later section.

14.5 Statement Blocks


A bunch of statements can be placed in braces to be executed as a single block. Such a
block of statements can be named or be provided with a condition for execution. Below is
how you'd place a series of statements in a block.

Example:
Code section 3.7: A statement block.
1 {
2 int a = 10;
3 int b = 20;
4 int result = a + b;
5 }

14.6 Branching Statements


Program flow can be affected using function/method calls, loops and iterations. Of various
types of branching constructs, we can easily pick out two generic branching methods.
• Unconditional Branching
• Conditional Branching

14.6.1 Unconditional Branching Statements


If you look closely at a method, you'll see that a method is a named statement block that
is executed by calling that particular name. An unconditional branch is created either by
invoking the method or by calling break, continue, return or throw, all of which are
described below.
When the name of another method is encountered in a flow, it stops execution in the current
method and branches to the newly called method. After returning a value from the called
method, execution picks up at the original method on the line below the method call.

96
Return statement

Code listing 3.8: [Link]


Console:
1 public class UnconditionalBranching { Output provided with the screen of information running the
2 public static void main(String[] args) { above code.
3 [Link](”Inside main method! Invoking aMethod!”);
Inside main method! Invoking aMethod!
4 aMethod(); Inside aMethod!
5 [Link](”Back in main method!”); Back in main method!
6 }
7
8 public static void aMethod() {
9 [Link](”Inside aMethod!”);
10 }
11 }

The program flow begins in the main method. Just as aMethod is invoked, the flow travels
to the called method. At this very point, the flow branches to the other method. Once the
method is completed, the flow is returned to the point it left off and resumes at the next
statement after the call to the method.

14.7 Return statement


A return statement exits from a block, so it is often the last statement of a method:

Example:
Code section 3.9: A return statement.
1 public int getAge() {
2 int age = 24;
3 return age;
4 }

A return statement can return the content of a variable or nothing. Beware not to write
statements after a return statement which would not be executed! Each return statement
is ended by a semi-colon (;).

14.7.1 Conditional Branching Statements


Conditional branching is attained with the help of the if...else and switch statements.
A conditional branch occurs only if a certain condition expression evaluates to true.

Conditional Statements
Also referred to as if statements, these allow a program to perform a test and then take
action based on the result of that test.
The form of the if statement:

if (condition) {
do statements here if condition is true
} else {

97
Statements

do statements here if condition is false


}

The condition is a boolean expression which can be either true or false. The actions
performed will depend on the value of the condition.
Example:

Example:
Code section 3.10: An if statement.
1 if (i > 0) {
2 [Link]("value stored in i is greater than zero");
3 } else {
4 [Link]("value stored is not greater than zero");
5 }

If statements can also be made more complex using the else if combination:

if (condition 1) {
do statements here if condition 1 is true
} else if (condition 2) {
do statements here if condition 1 is false and condition 2 is true
} else {
do statements here if neither condition 1 nor condition 2 is true
}

Example:

Example:
Code section 3.11: An if/else if/else statement.
1 if (i > 0) {
2 [Link]("value stored in i is greater than zero");
3 } else if (i < 0) {
4 [Link]("value stored in i is less than zero");
5 } else {
6 [Link]("value stored is equal to 0");
7 }

If there is only one statement to be executed after the condition, as in the above example,
it is possible to omit the curly braces, however Oracle's Java Code Conventions5 explicitly
state that the braces should always be used.
There is no looping involved in an if statement so once the condition has been evaluated
the program will continue with the next instruction after the statement.

If...else statements
The if ... else statement is used to conditionally execute one of two blocks of statements,
depending on the result of a boolean condition.

5 [Link]

98
Return statement

Example:

Example:
Code section 3.12: An if/else statement.
1 if (list == null) {
2 // This block of statements executes if the condition is true.
3 } else {
4 // This block of statements executes if the condition is false.
5 }

Oracle's Java Code Conventions6 recommend that the braces should always be used.
An if statement has two forms:

if (boolean-condition)
statement1

and

if (boolean-condition)
statement1
else
statement2

Use the second form if you have different statements to execute if the boolean-condition is
true or if it is false. Use the first if you only wish to execute statement1 if the condition is
true and you do not wish to execute alternate statements if the condition is false.
The code section 3.137 calls two int methods, f() and y(), stores the results, then uses
an if statement to test if x is less than y and if it is, the statement1 body will swap the
values. The end result is x always contains the larger result and y always contains the
smaller result.

Example:
Code section 3.13: Value swap.
1 int x = f();
2 int y = y();
3 if (x < y) {
4 int z = x;
5 x = y;
6 y = z;
7 }

if...else statements also allow for the use of another statement, else if. This statement is
used to provide another if statement to the conditional that can only be executed if the
others are not true. For example:

6 [Link]
7 Chapter 14.11 on page 104

99
Statements

Example:
Code section 3.14: Multiple branching.
1 if (x == 2)
2 x = 4;
3 else if (x == 3)
4 x = 6;
5 else
6 x = -1;

The else if statement is useful in this case because if one of the conditionals is true, the
other must be false. Keep in mind that if one is true, the other will not execute. For
example, if the statement at line 2 contained in the first conditional were changed to x =
3;, the second conditional, the else if, would still not execute. However, when dealing with
primitive types in conditional statements, it is more desirable to use switch statements8
rather than multiple else if statements.

Switch statements
The switch conditional statement is basically a shorthand version of writing many if...else
statements. The syntax for switch statements is as follows:

switch(<variable>) {
case <result>: <statements>; break;
case <result>: <statements>; break;
default: <statements>; break;
}

This means that if the variable included equals one of the case results, the statements
following that case, until the word break will run. The default case executes if none of
the others are true. Note: the only types that can be analysed through switch statements
are char, byte, short, or int primitive types. This means that Object variables can not
by analyzed through switch statements. However, as of the JDK 7 release, you can use a
String object in the expression of a switch statement.

Example:
Code section 3.15: A switch.
1 int n = 2, x;
2 switch (n) {
3 case 1: x = 2;
4 break;
5 case 2: x = 4;
6 break;
7 case 3: x = 6;
8 break;
9 case 4: x = 8;
10 break;
11 }
12 return x;

8 Chapter 14.7.1 on page 100

100
Iteration Statements

In this example, since the integer variable n is equal to 2, case 2 will execute, make x
equal to 4. Thus, 4 is returned by the method.

14.8 Iteration Statements


Iteration Statements are statements that are used to iterate a block of statements. Such
statements are often referred to as loops. Java offers four kinds of iterative statements.
• The while loop
• The do...while loop
• The for loop
• The foreach loop

14.8.1 The while loop


Main Page: Java Programming/Keywords/while9 The while loop iterates a block of code
while the condition it specifies is true.
The syntax for the loop is:

while (condition) {
statement;
}

Here the condition is an expression. An expression as discussed earlier is any statement


that returns a value. While condition statements evaluate to a boolean value, that is, either
true or false. As long as the condition is true, the loop will iterate the block of code
over and over and again. Once the condition evaluates to false, the loop exits to the next
statement outside the loop.

14.8.2 The do...while loop


The do-while loop is functionally similar to the while loop, except the condition is evaluated
AFTER the statement executes

do {
statement;
} while (condition);

14.8.3 The for loop


Main Page: Java Programming/Keywords/for The for loop is a specialized while loop whose
syntax is designed for easy iteration through a sequence of numbers. Example:

9 Chapter 25.59 on page 223

101
Statements

Example: Console:
Code section 3.16: A for loop. Output for code listing 3.16a if you compile and run the state-
ment above.

1 for (int i = 0; i < 100; i++) { 0 0


2 [Link](i + "\t" + i * i); 1 1
3 } 2 4
3 9
...
99 9801

a Chapter 14.11 on page 104

The program prints the numbers 0 to 99 and their squares.


The same statement in a while loop:

Example:
Code section 3.17: An alternative version.
1 int i = 0;
2 while (i < 100) {
3 [Link](i + "\t" + i * i);
4 i++;
5 }

14.8.4 The foreach loop


The foreach statement allows you to iterate through all the items in a collection, examin-
ing each item in turn while still preserving its type. The syntax for the foreach statement is:

for (type item : collection) statement;

For an example, we'll take an array of Strings denoting days in a week and traverse through
the collection, examining one item at a time.

Example: Console:
Code section 3.18: A foreach loop. Output for code listing 3.18a
Monday
Tuesday
1 String[] days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
2 "Saturday", "Sunday"}; Wednesday
3 Thursday
4 for (String day : days) { Friday
5 [Link](day); Saturday
6 } Sunday

a Chapter 14.11 on page 104

Notice that the loop automatically exits after the last item in the collection has been
examined in the statement block.
Although the enhanced for loop can make code much clearer, it can't be used in some
common situations.

102
The continue and break statements

• Only access. Elements can not be assigned to, eg, not to increment each element in a
collection.
• Only single structure. It's not possible to traverse two structures at once, eg, to com-
pare two arrays.
• Only single element. Use only for single element access, eg, not to compare successive
elements.
• Only forward. It's possible to iterate only forward by single steps.
• At least Java 5. Don't use it if you need compatibility with versions before Java 5.

14.9 The continue and break statements


At times, you would like to re-iterate a loop without executing the remaining statement
within the loop. The continue statement causes the loop to re-iterate and start over from
the top most statement inside the loop.
Where there is an ability to re-iterate the loop, there is an ability to exit the loop when
required. At any given moment, if you'd like to exit a loop and end all further work within
the loop, the break ought to be used.
The continue and break statements can be used with a label like follows:

Example:
Code section 3.19: Using a label.
1 String s = "A test string for the switch!\nLine two of test string...";
2 outer: for (int i = 0; i < [Link](); i++) {
3 switch ([Link](i)) {
4 case '\n': break outer;
5 case ' ': break;
6 default: [Link]([Link](i));
7 }
8 }

Console:
Output for code listing 3.19a
Ateststringfortheswitch!

a Chapter 14.11 on page 104

14.10 Throw statement


A throw statement exits from a method and so on and so on or it is caught by a try/catch
block. It does not return a variable but an exception:

103
Statements

Example:
Code section 3.20: A return statement.
1 public int getAge() {
2 throw new NullPointerException();
3 }

Beware not to write statements after a throw statement which would not be executed too!
Each throw statement is ended by a semi-colon (;).

14.11 try/catch
A try/catch must at least contain the try block and the catch block:

Example:
Code section 3.21: try/catch block.
1 try {
2 // Some code
3 } catch (Exception e) {
4 // Optional exception handling
5 } finally {
6 // This code is executed no matter what
7 }

Question 3.1: How many statements are there in this class?Code listing 3.2:
[Link]

1 public class AProgram {


2
3 private int age = 24;
4
5 public static void main(String[] args) {
6 int daysInAYear = 365;int ageInDay = 100000;
7 int localAge = ageInDay / daysInAYear;
8 }
9
10 public int getAge() {
11 return age;
12 }
13 }

5
One statement at line 3, two statements at line 6, one statement at line 7 and one statement
at line 11.

104
15 Conditional blocks
Conditional blocks allow a program to take a different path depending on some condition(s).
These allow a program to perform a test and then take action based on the result of that
test. In the code sections, the actually executed code lines will be highlighted.

15.0.1 If
The if block executes only if the boolean expression associated with it is true. The structure
of an if block is as follows:
if (boolean expression1 ) {
statement1
statement2
...
statementn
}

Here is a double example to illustrate what happens if the condition is true and if the
condition is false:
Output for Code section 3.22
Hello!
Example: I'm a child
Code section 3.22: Two if blocks. Bye!

1 int age = 6;
2 [Link]("Hello!");
3
4 if (age < 13) {
5 [Link]("I'm a child.");
6 }
7
8 if (age > 20) {
9 [Link]("I'm an adult.");
10 }
11
12 [Link]("Bye!");

Note:
If only one statement is to be executed after an if block, it does not have to be enclosed
in curly braces. For example, if (i == 0) i = 1; is a perfectly valid portion of Java
code. This works for most control structures, such as else and while. However Oracle's
Java Code Conventionsa explicitly state that the braces should always be used.

a [Link]

105
Conditional blocks

15.0.2 If/else
The if block may optionally be followed by an else block which will execute if that boolean
expression is false. The structure of an if block is as follows:

if (boolean expression1 ) {
statement1
statement2
...
statementn
} else {
statement1bis
statement2bis
...
statementnbis
}

15.0.3 If/else-if/else
An else-if block may be used when multiple conditions need to be checked. else-if
statements come after the if block, but before the else block. The structure of an if
block is as follows:
if (boolean expression1 ) {
statement1.1
statement1.2
...
statementn
} else if (boolean expression2 ) {
statement2.1
statement2.2
...
statement2.n
} else {
statement3.1
statement3.2
...
statement3.n
}

Here is an example to illustrate:

106
try/catch

Code listing 3.3: [Link] Output for code listing 3.3


a is positive
1 public class MyConditionalProgram {
2 public static void main (String[] args) {
3 int a = 5;
4 if (a > 0) {
5 // a is greater than 0, so this statement will execute
6 [Link](”a is positive”);
7 } else if (a >= 0) {
8 // a case has already executed, so this statement will NOT execute
9 [Link](”a is positive or zero”);
10 } else {
11 // a case has already executed, so this statement will NOT execute
12 [Link](”a is negative”);
13 }
14 }
15 }

Keep in mind that only a single block will execute, and it will be the first true condition.
All the conditions are evaluated when if is reached, no matter what the result of the
condition is, after the execution of the if block:

Output for code section 3.23


a is positive
Example:
Code section 3.23: A new value for the variable
a.
1 int a = 5;
2 if (a > 0) {
3 // a is greater than 0, so this statement will execute
4 [Link]("a is positive");
5 a = -5;
6 } else if (a < 0) {
7 // a WAS greater than 0, so this statement will not execute
8 [Link]("a is negative");
9 } else {
10 // a does not equal 0, so this statement will not execute
11 [Link]("a is zero");
12 }

15.0.4 Conditional expressions


Conditional expressions use the compound ?: operator. Syntax:

boolean expression1 ? expression1 : expression2

This evaluates boolean expression1 , and if it is true then the conditional expression has
the value of expression1 ; otherwise the conditional expression has the value of expression
2.

Example:

107
Conditional blocks

Example:
Code section 3.24: Conditional expressions.
1 String answer = (p < 0.05)? "reject" : "keep";

This is equivalent to the following code fragment:

Example:
Code section 3.25: Equivalent code.
1 String answer;
2 if (p < 0.05) {
3 answer = "reject";
4 } else {
5 answer = "keep";
6 }

15.0.5 Switch
The switch conditional statement is basically a shorthand version of writing many if...else
statements. The switch block evaluates a char, byte, short, or int (or enum, starting
in J2SE 5.0; or String, starting in J2SE 7.0), and, based on the value provided, jumps
to a specific case within the switch block and executes code until the break command is
encountered or the end of the block. If the switch value does not match any of the case
values, execution will jump to the optional default case.
The structure of a switch statement is as follows:
switch (int1 or char1 or short1 or byte1 or enum1 or String value1 ) {
case case value1 :
statement1.1
...
statement1.n
break;
case case value2 :
statement2.1
...
statement2.n
break;
default:
statementn.1
...
statementn.n
}

Here is an example to illustrate:

108
try/catch

Output for code section 3.26


i equals something other than 1 or 2
Example:
Code section 3.26: A switch block.
1 int i = 3;
2 switch(i) {
3 case 1:
4 // i doesn't equal 1, so this code won't execute
5 [Link]("i equals 1");
6 break;
7 case 2:
8 // i doesn't equal 2, so this code won't execute
9 [Link]("i equals 2");
10 break;
11 default:
12 // i has not been handled so far, so this code will execute
13 [Link]("i equals something other than 1 or 2");
14 }

If a case does not end with the break statement, then the next case will be checked, other-
wise the execution will jump to the end of the switch statement.
Look at this example to see how it's done:

Output for code section 3.27


i is 1 or -1
Example:
Code section 3.27: A switch block containing a
case without break.
1 int i = -1;
2 switch(i) {
3 case -1:
4 case 1:
5 // i is -1, so it will fall through to this case and execute this
code
6 [Link]("i is 1 or -1");
7 break;
8 case 0:
9 // The break command is used before this case, so if i is 1 or -
1,
this
10 will not execute
11 [Link]("i is 0");
12 }

Starting in J2SE 5.0, the switch statement can also be used with an enum value instead
of an integer.
Though enums have not been covered yet, here is an example so you can see how it's done
(note that the enum constants in the cases do not need to be qualified with the type:

109
Conditional blocks

Output for code section 3.28


Mondays are the worst!
Example:
Code section 3.28: A switch block with an enum
type.
1 Day day = [Link]; // Day is a fictional enum type containing the days of
the
2 week
3 switch(day) {
4 case MONDAY:
5 // Since day == [Link], this statement will execute
6 [Link]("Mondays are the worst!");
7 break;
8 case TUESDAY:
9 case WEDNESDAY:
10 case THURSDAY:
11 [Link]("Weekdays are so-so.");
12 break;
13 case FRIDAY:
14 case SATURDAY:
15 case SUNDAY:
16 [Link]("Weekends are the best!");
17 break;
18 }

Starting in J2SE 7.0, the switch statement can also be used with an String value instead
of an integer.

Output for code section 3.29


Mondays are the worst!
Example:
Code section 3.29: A switch block with a String
type.
1 String day = "Monday";
2 switch(day) {
3 case "Monday":
4 // Since day == "Monday", this statement will execute
5 [Link]("Mondays are the worst!");
6 break;
7 case "Tuesday":
8 case "Wednesday":
9 case "Thursday":
10 [Link]("Weekdays are so-so.");
11 break;
12 case "Friday":
13 case "Saturday":
14 case "Sunday":
15 [Link]("Weekends are the best!");
16 break;
17 default:
18 throw new IllegalArgumentException("Invalid day of the week: " +
day);
19 }

110
16 Loop blocks

Loops are a handy tool that enables programmers to do repetitive tasks with minimal effort.
Say we want a program that can count from 1 to 10, we could write the following program1 .
Code listing 3.4: [Link]
Console:
1 class Count { Output for code listing 3.4
2 public static void main(String[] args) { 1
3 [Link](”1 ”); 2
4 [Link](”2 ”); 3
5 [Link](”3 ”); 4
6 [Link](”4 ”); 5
7 [Link](”5 ”); 6
8 [Link](”6 ”); 7
9 [Link](”7 ”); 8
10 [Link](”8 ”); 9
11 [Link](”9 ”); 10
12 [Link](”10 ”);
13 }
14 }

The task will be completed just fine, the numbers 1 to 10 will be printed in the output, but
there are a few problems with this solution:
• Flexibility: what if we wanted to change the start number or end number? We would
have to go through and change them, adding extra lines of code where they're needed.
• Scalability: 10 repeats are trivial, but what if we wanted 100 or even 1000 repeats? The
number of lines of code needed would be overwhelming for a large number of iterations.
• Maintenance: where there is a large amount of code, one is more likely to make a
mistake.
• Feature: the number of tasks is fixed and doesn't change at each execution.
Using loops we can solve all these problems. Once you get you head around them they will
be invaluable to solving many problems in programming.
Open up your editing program and create a new file saved as [Link]. Now type or copy
the following code2 :

1 Chapter 16.0.1 on page 112


2 Chapter 16.0.1 on page 112

111
Loop blocks

Code listing 3.5: [Link]


Console:
1 class Loop { Output for code listing 3.5
2 public static void main(String[] args) { 1
3 int i; 2
4 for (i = 1; i <= 10; i++) { 3
5 [Link](i + ” ”); 4
6 } 5
7 } 6
8} 7
8
9
10

If we run the program, the same result is produced, but looking at the code, we immediately
see the advantages of loops. Instead of executing ten different lines of code, line 5 executes
ten times. Ten lines of code have been reduced to just four. Furthermore, we may change
the number 10 to any number we like. Try it yourself, replace the 10 with your own number.

16.0.1 While
while loops are the simplest form of loop. The while loop repeats a block of code while
the specified condition is true. Here is the structure of a while loop:

while (boolean expression1 ) {


statement1
statement2
...
statementn
}

The loop's condition is checked before each iteration of the loop. If the condition is false at
the start of the loop, the loop will not be executed at all. The code section 3.283 sets in
squareHigherThan200 the smallest integer whose square exceeds 200.

Example:
Code section 3.28: The smallest integer whose square exceeds 200.
1 int squareHigherThan200 = 0;
2
3 while (squareHigherThan200 * squareHigherThan200 < 200) {
4 squareHigherThan200 = squareHigherThan200 + 1;
5 }

3 Chapter 16.0.1 on page 112

112
try/catch

Note:
If a loop's condition will never become false, such as if the true constant is used for the
condition, said loop is known as an infinite loop. Such a loop will repeat indefinitely
unless it is brokena out of. Infinite loops can be used to perform tasks that need to
be repeated over and over again without a definite stopping point, such as updating a
graphics display.

a Chapter 25.4 on page 188

Do... while
The do-while loop is functionally similar to the while loop, except the condition is evalu-
ated AFTER the statement executes It is useful when we try to find a data that does the
job by randomly browsing an amount of data.

do {
statement1
statement2
...
statementn
} while (boolean expression1 );

16.0.2 For
The for loop is a specialized while loop whose syntax is designed for easy iteration through
a sequence of numbers. It consists of the keyword for followed by three extra statements
enclosed in parentheses. The first statement is the variable declaration statement, which
allows you to declare one or more integer variables. The second is the condition, which is
checked the same way as the while loop. Last is the iteration statement, which is used to
increment or decrement variables, though any statement is allowed.
This is the structure of a for loop:

for (variable declarations; condition; iteration statement) {


statement1
statement2
...
statementn
}

To clarify how a for loop is used, here is an example:

113
Loop blocks

Example: Console:
Code section 3.29: A for loop. Output for code listing 3.29
1
1 for (int i = 1; i <= 10; i++) { 2
2 [Link](i); 3
3 } 4
5
6
7
8
9
10

The for loop is like a template version of the while loop. The alternative code using a
while loop would be as follows:

Example:
Code section 3.30: An iteration using a while loop.
1 int i = 1;
2 while (i <= 10) {
3 [Link](i);
4 i++;
5 }

The code section 3.314 shows how to iterate with the for loop using multiple variables and
the code section 3.325 shows how any of the parameters of a for loop can be skipped. Skip
them all, and you have an infinitely repeating loop.

Example: Example:
Code section 3.31: The for loop using multiple variables. Code section 3.32: The for loop without parameter.

1 for (int i = 1, j = 10; i <= 10; i++, j-- 1 for (;;) {


) { 2 // Some code
2 [Link](i + " "); 3 }
3 [Link](j);
4 }

For-each
Arrays6 haven't been covered yet, but you'll want to know how to use the enhanced for
loop, called the for-each loop. The for-each loop automatically iterates through a list or
array and assigns the value of each index to a variable.
To understand the structure of a for-each loop, look at the following example:

4 Chapter 16.0.1 on page 112


5 Chapter 16.0.1 on page 112
6 Chapter 26.6 on page 227

114
Break and continue keywords

Example:
Code section 3.33: A for-each loop.
1 String[] sentence = {"I", "am", "a", "Java", "program."};
2 for (String word : sentence) {
3 [Link](word + " ");
4 }

Console:
Output for code section 3.33
I am a Java program.

The example iterates through an array of words and prints them out like a sentence. What
the loop does is iterate through sentence and assign the value of each index to word, then
execute the code block.
Here is the general contract of the for-each loop:

for (variable declaration : array or list) {


statement1
statement2
...
statementn
}

Make sure that the type of the array or list is assignable to the declared variable, or you
will get a compilation error. Notice that the loop automatically exits after the last item in
the collection has been examined in the statement block.
Although the enhanced for loop can make code much clearer, it can't be used in some
common situations.
• Only access. Elements can not be assigned to, e.g., not to increment each element in a
collection.
• Only single structure. It's not possible to traverse two structures at once, e.g., to
compare two arrays.
• Only single element. Use only for single element access, e.g., not to compare successive
elements.
• Only forward. It's possible to iterate only forward by single steps.
• At least Java 5. Don't use it if you need compatibility with versions before Java 5.

16.1 Break and continue keywords


The break keyword exits a flow control loop, such as a for loop. It basically breaks the
loop.

115
Loop blocks

In the code section 3.347 , the loop would print out all the numbers from 1 to 10, but we
have a check for when i equals 5. When the loop reaches its fifth iteration, it will be cut
short by the break statement, at which point it will exit the loop.

Example: Console:
Code section 3.34: An interrupted for loop. Output for code section 3.34
1
1 for (int i = 1; i <= 10; i++) { 2
2 [Link](i); 3
3 if (i == 5) { 4
4 [Link]("STOP!"); 5
5 break; STOP!
6 }
7 }

The continue keyword jumps straight to the next iteration of a loop and evaluates the
boolean expression controlling the loop. The code section 3.358 is an example of the
continue statement in action:

Example: Console:
Code section 3.35: A for loop with a skipped iteration. Output for code section 3.35
1
1 for (int i = 1; i <= 10; i++) { 2
2 if (i == 5) { 3
3 [Link]("Caught i == 5"); 4
4 continue; Caught i == 5
5 } 6
6 [Link](i); 7
7 } 8
9
10

As the break and continue statements reduce the readability of the code, it is recom-
mended to reduce their use or replace them with the use of if and while blocks. Some
IDE refactoring operations will fail because of such statements.
Question 3.2: Consider the following code:

7 Chapter 16.0.1 on page 112


8 Chapter 16.0.1 on page 112

116
Break and continue keywords

Example:
Question 3.2: Loops and conditions.
1 int numberOfItems = 5;
2 int currentItems = 0;
3 int currentCandidate = 1;
4
5 while (currentItems < numberOfItems) {
6 currentCandidate = currentCandidate + 1;
7 [Link]("Test with integer: " + currentCandidate);
8
9 boolean found = true;
10 for (int i = currentCandidate - 1; i > 1; i--) {
11
12 // Test if i is a divisor of currentCandidate
13 if ((currentCandidate % i) == 0) {
14 [Link]("Not matching...");
15 found = false;
16 break;
17 }
18
19 }
20
21 if (found) {
22 [Link]("Matching!");
23 currentItems = currentItems + 1;
24 }
25 }
26
27 [Link]("Find the value: " + currentCandidate);

What will be printed in the standard output?

Console:
Output for Question 3.2
Test with integer: 2
Matching!
Test with integer: 3
Matching!
Test with integer: 4
Not matching...
Test with integer: 5
Matching!
Test with integer: 6
Not matching...
Test with integer: 7
Matching!
Test with integer: 8
Not matching...
Test with integer: 9
Not matching...
Test with integer: 10
Not matching...
Test with integer: 11
Matching!
Find the value: 11

117
Loop blocks

The snippet is searching the 5th prime number9 , that is to say: 11. It iterates on each
positive integer from 2 (2, 3, 4, 5, 6, 7, 8, 9, 10, 11...), among them, it counts the prime
numbers (2, 3, 5, 7, 11) and it stops at the 5th one.
So the snippet first iterates on each positive integer from 2 using the while loop:

Example:
Answer 3.2.1: while loop.
1 int numberOfItems = 5;
2 int currentItems = 0;
3 int currentCandidate = 1;
4
5 while (currentItems < numberOfItems) {
6 currentCandidate = currentCandidate + 1;
7 [Link]("Test with integer: " + currentCandidate);
8
9 boolean found = true;
10 for (int i = currentCandidate - 1; i > 1; i--) {
11
12 // Test if i is a divisor of currentCandidate
13 if ((currentCandidate % i) == 0) {
14 [Link]("Not matching...");
15 found = false;
16 break;
17 }
18
19 }
20
21 if (found) {
22 [Link]("Matching!");
23 currentItems = currentItems + 1;
24 }
25 }
26
27 [Link]("Find the value: " + currentCandidate);
28

For each iteration, the current number is either a prime number or not. If it is a prime
number, the code at the left will be executed. If it is not a prime number, the code at the
right will be executed.

9 [Link]

118
Break and continue keywords

Example:
Answer 3.2.2: A prime number.
1 int numberOfItems = 5;
2 int currentItems = 0;
3 int currentCandidate = 1;
4
5 while (currentItems < numberOfItems) {
6 currentCandidate = currentCandidate + 1;
7 [Link]("Test with integer: " + currentCandidate);
8
9 boolean found = true;
10 for (int i = currentCandidate - 1; i > 1; i--) {
11
12 // Test if i is a divisor of currentCandidate
13 if ((currentCandidate % i) == 0) {
14 [Link]("Not matching...");
15 found = false;
16 break;
17 }
18
19 }
20
21 if (found) {
22 [Link]("Matching!");
23 currentItems = currentItems + 1;
24 }
25 }
26
27 [Link]("Find the value: " + currentCandidate);

Example:
Answer 3.2.3: Not a prime number.
1 int numberOfItems = 5;
2 int currentItems = 0;
3 int currentCandidate = 1;
4
5 while (currentItems < numberOfItems) {
6 currentCandidate = currentCandidate + 1;
7 [Link]("Test with integer: " + currentCandidate);
8
9 boolean found = true;
10 for (int i = currentCandidate - 1; i > 1; i--) {
11
12 // Test if i is a divisor of currentCandidate
13 if ((currentCandidate % i) == 0) {
14 [Link]("Not matching...");
15 found = false;
16 break;
17 }
18
19 }
20
21 if (found) {
22 [Link]("Matching!");
23 currentItems = currentItems + 1;
24 }
25 }
26
27 [Link]("Find the value: " + currentCandidate);

119
Loop blocks

The prime numbers are counted using currentItems. When currentItems is equal to
numberOfItems (5), the program go out of the while loop. currentCandidate contains
the last number, that is to say the 5th prime number:

Example:
Answer 3.2.4: End of the program.
1 int numberOfItems = 5;
2 int currentItems = 0;
3 int currentCandidate = 1;
4
5 while (currentItems < numberOfItems) {
6 currentCandidate = currentCandidate + 1;
7 [Link]("Test with integer: " + currentCandidate);
8
9 boolean found = true;
10 for (int i = currentCandidate - 1; i > 1; i--) {
11
12 // Test if i is a divisor of currentCandidate
13 if ((currentCandidate % i) == 0) {
14 [Link]("Not matching...");
15 found = false;
16 break;
17 }
18
19 }
20
21 if (found) {
22 [Link]("Matching!");
23 currentItems = currentItems + 1;
24 }
25 }
26
27 [Link]("Find the value: " + currentCandidate);

16.2 Labels
Labels can be used to give a name to a loop. The reason to do this is so we can break out
of or continue with upper-level loops from a nested loop.
Here is how to label a loop:

label name:loop

To break out of or continue with a loop, use the break or continue keyword followed by
the name of the loop.
For example:

120
Try... catch blocks

Example: Console:
Code section 3.36: A double for loop. Output for code section 3.36
Found number 9 at (1, 1)
1 int i, j;
2 int[][] nums = {
3 {1, 2, 5},
4 {6, 9, 7},
5 {8, 3, 4}
6 };
7
8 Outer:
9 for (i = 0; i < [Link]; i++) {
10 for (j = 0; j < nums[i].length; j++) {
11 if (nums[i][j] == 9) {
12 [Link]("Found number 9 at (" + i + ", " + j + ")");
13 break Outer;
14 }
15 }
16 }

You needn't worry if you don't understand all the code, but look at how the label is used
to break out of the outer loop from the inner loop. However, as such a code is hard to read
and maintain, it is highly recommended not to use labels.

16.3 Try... catch blocks


See also Throwing and Catching Exceptions10 .
The try-catch blocks are used to catch any exceptions or other throwable objects within
the code.
Here's what try-catch blocks looks like:

try {
statement1.1
statement1.2
...
statement1.n
} catch (exception1 ) {
statement2.1
...
statement2.n
}

The code listing 3.611 tries to print all the arguments that have been passed to the program.
However, if there not enough arguments, it will throw an exception.

[Link]
10
20Exceptions
11 Chapter 16.0.1 on page 112

121
Loop blocks

Code listing 3.6: [Link]

1 public class Attempt {


2 public static void main(String[] args) {
3 try {
4 [Link](args[0]);
5 [Link](args[1]);
6 [Link](args[2]);
7 [Link](args[3]);
8 } catch (ArrayIndexOutOfBoundsException e) {
9 [Link](”Not enough arguments”);
10 }
11 }
12 }

In addition to the try and catch blocks, a finally block may be present. The finally block
is always executed, even if an exception is thrown. It may appear with or without a catch
block, but always with a try block.
Here is what a finally block looks like:

try {
statement1.1
statement1.2
...
statement1.n
} catch (exception1 ) {
statement2.1
...
statement2.n
} finally {
statement3.1
...
statement3.n
}

16.4 Examples
The code listing 3.712 receives a number as parameter and print its binary representation.
Code listing 3.7: [Link]

1 public class GetBinary {


2 public static void main(String[] args) {
3 if ([Link] == 0) {
4 // Print usage
5 [Link](”Usage: java GetBinary <decimal integer>”);
6 [Link](0);
7 } else {
8 // Print arguments
9 [Link](”Received ” + [Link] + ” arguments.”);

12 Chapter 16.0.1 on page 112

122
Examples

10 [Link](”The arguments are:”);


11 for (String arg : args) {
12 [Link](”\t” + arg);
13 }
14 }
15
16 int number = 0;
17 String binary = ” ”;
18
19 // Get the input number
20 try {
21 number = [Link](args[0]);
22 } catch (NumberFormatException ex) {
23 [Link](”Error: argument must be a base-10 integer.”);
24 [Link](0);
25 }
26
27 // Convert to a binary string
28 do {
29 switch (number % 2) {
30 case 0: binary = '0' + binary; break;
31 case 1: binary = '1' + binary; break;
32 }
33 number »= 1;
34 } while (number > 0);
35
36 [Link](”The binary representation of ” + args[0] + ” is ”
+
37 binary);
38 }
39 }

The code listing 3.813 is a simulation of playing a game called Lucky Sevens. It is a dice
game where the player rolls two dice. If the numbers on the dice add up to seven, he wins
$4. If they do not, he loses $1. The game shows how to use control flow in a program as
well as the fruitlessness of gambling.
Code listing 3.8: [Link]

1 import [Link].*;
2
3 public class LuckySevens {
4 public static void main(String[] args) {
5 Scanner in = new Scanner([Link]);
6 Random random = new Random();
7 String input;
8 int startingCash, cash, maxCash, rolls, roll;
9
10 // Loop until ”quit” is input
11 while (true) {
12 [Link](”Enter the amount of cash to start with (or
13 \”quit\” to quit): ”);
14
15 input = [Link]();
16
17 // Check if user wants to exit
18 if ([Link]().equals(”quit”)) {
19 [Link](”\tGoodbye.”);
20 [Link](0);
21 }

13 Chapter 16.0.1 on page 112

123
Loop blocks

22
23 // Get number
24 try {
25 startingCash = [Link](input);
26 } catch (NumberFormatException ex) {
27 [Link](”\tPlease enter a positive integer greater
28 than 0.”);
29 continue;
30 }
31
32 // You have to start with some money!
33 if (startingCash <= 0) {
34 [Link](”\tPlease enter a positive integer greater
35 than 0.”);
36 continue;
37 }
38
39 cash = startingCash;
40 maxCash = cash;
41 rolls = 0;
42 roll = 0;
43
44 // Here is the game loop
45 for (; cash > 0; rolls++) {
46 roll = [Link](6) + 1;
47 roll += [Link](6) + 1;
48
49 if (roll == 7)
50 cash += 4;
51 else
52 cash -= 1;
53
54 if (cash > maxCash)
55 maxCash = cash;
56 }
57
58 [Link](”\tYou start with $” + startingCash + ”.\n”
59 + ”\tYou peak at $” + maxCash + ”.\n”
60 + ”\tAfter ” + rolls + ” rolls, you run out of cash.”);
61 }
62 }
63 }

124
17 Boolean expressions

Boolean values are values that evaluate to either true or false, and are represented by the
boolean data type. Boolean expressions are very similar to mathematical expressions, but
instead of using mathematical operators such as ”+” or ”-”, you use comparative or boolean
operators such as ”==” or ”!”.

17.1 Comparative operators


Java has several operators that can be used to compare variables. For example, how would
you tell if one variable has a greater value than another? The answer: use the ”greater-than”
operator.
Here is a list of the comparative operators in Java:
• > : Greater than
• < : Less than
• >= : Greater than or equal to
• <= : Less than or equal to
• == : Equal to
• != : Not equal to
To see how these operators are used, look at this example:

Output for code section 3.37


true
Example: false
Code section 3.37: Comparisons. true
true
1 int a = 5, b = 3;
2 [Link](a > b); // Value is true because a is greater than b
3 [Link](a == b); // Value is false because a does not equal b
4 [Link](a != b); // Value is true because a does not equal b
5 [Link](b <= a); // Value is true because b is less than a

Comparative operators can be used on any primitive types (except boolean), but only
the ”equals” and ”does not equal” operators work on objects. This is because the less-
than/greater-than operators cannot be applied to objects, but the equivalency operators
can.

125
Boolean expressions

Note:
Specifically, the == and != operators test whether both variables point to the same
object. Objects will be covered later in the tutorial, in the ”Classes, Objects, and
Typesa ” module.

a Chapter 23.8 on page 175

17.2 Boolean operators


The Java boolean operators are based on the operations of the boolean algebra1 . The
boolean operators operate directly on boolean values.
Here is a list of four common boolean operators in Java:
• ! : Boolean NOT
• && : Boolean AND
• || : Boolean inclusive OR
• ^ : Boolean exclusive XOR
The boolean NOT operator (”!”) inverts the value of a boolean expression. The boolean
AND operator (”&&”) will result in true if and only if the values on both sides of the
operator are true. The boolean inclusive OR operator (”||”) will result in true if either or
both of the values on the sides of the operator is true. The boolean exclusive XOR operator
(”^”) will result in true if one and only one of the values on the sides of the operator is true.
To show how these operators are used, here is an example:

1 [Link]

126
Boolean operators

Output for code section 3.38


NOT operand:
Example: false
Code section 3.38: Operands. true
false
1 boolean iMTrue = true; AND operand:
2 boolean iMTrueToo = true; true
3 boolean iMFalse = false; false
4 boolean iMFalseToo = false; false
5 true
6 [Link]("NOT operand:"); OR operand:
7 [Link](!iMTrue); true
8 [Link](!iMFalse); false
9 [Link](!(4 < 5)); true
10 [Link]("AND operand:"); false
11 [Link](iMTrue && iMTrueToo); XOR operand:
12 [Link](iMFalse && iMFalseToo); false
13 [Link](iMTrue && iMFalse); false
14 [Link](iMTrue && !iMFalse); true
15 [Link]("OR operand:"); false
16 [Link](iMTrue || iMTrueToo);
17 [Link](iMFalse || iMFalseToo);
18 [Link](iMTrue || iMFalse);
19 [Link](iMFalse || !iMTrue);
20 [Link]("XOR operand:");
21 [Link](iMTrue ^ iMTrueToo);
22 [Link](iMFalse ^ iMFalseToo);
23 [Link](iMTrue ^ iMFalse);
24 [Link](iMFalse ^ !iMTrue);

Here are the truth tables for the boolean operators:

a !a
true false
false true

a b a && b a || b a^b
true true true true false
true false false true true
false true false true true
false false false false false

For help on simplifying complex logic, see De Morgan's laws2 .


In Java, boolean logic has a useful property called short circuiting. This means that expres-
sions will only be evaluated as far as necessary. In the expression (a && b), if a is false,
then b will not be evaluated because the expression will be false no matter what. Here is
an example that shows that the second expression is not automatically checked:

2 [Link]

127
Boolean expressions

Output for code section 3.39


true
Example:
Code section 3.39: Short circuiting.
1 [Link]((4 < 5) || ((10 / 0) == 2));

To disable this property, you can use & instead of && and | instead of || but it's not rec-
ommended.
For the bitwise operations on & and |, see Arithmetic expressions3 .

[Link]
3
20bitwise%20operators%20within%20Java

128
18 Variables

In the Java programming language, the words field and variable are both one and the
same thing. Variables are devices that are used to store data, such as a number, or a string
of character data.

18.1 Variables in Java programming


Java is considered as a strongly typed programming language. Thus all variables in the
Java programming language ought to have a particular data type. This is either declared
or inferred and the Java language only allows programs to run if they adhere to type
constraints.
If you present a numeric type with data that is not numeric, say textual content, then such
declarations would violate Java’s type system. This gives Java the ability of type safety
. Java checks if an expression or data is encountered with an incorrect type or none at all.
It then automatically flags this occurrence as an error at compile time. Most type-related
errors are caught by the Java compiler, hence making a program more secure and safe once
compiled completely and successfully. Some languages (such as C) define an interpretation
of such a statement and use that interpretation without any warning; others (such as PL/I)
define a conversion for almost all such statements and perform the conversion to complete
the assignment. Some type errors can still occur at runtime because Java supports a cast
operation which is a way of changing the type of one expression to another. However, Java
performs run time type checking when doing such casts, so an incorrect type cast will cause
a runtime exception rather than succeeding silently and allowing data corruption.
On the other hand, Java is also known as a hybrid language. While supporting object
oriented programming1 (OOP), Java is not a pure OO language like Smalltalk2 or Ruby3 .
Instead, Java offers both object types and primitive types4 . Primitive types are used for
boolean, character, and numeric values and operations. This allows relatively good per-
formance when manipulating numeric data, at the expense of flexibility. For example, you
cannot subclass the primitive types and add new operations to them.

18.2 Kinds of variables


In the Java programming language, there are four kinds of variables.

1 [Link]
2 [Link]
3 [Link]
4 Chapter 18.8 on page 135

129
Variables

Code listing 3.9: [Link]

1 public class ClassWithVariables {


2 public int id = 0;
3 public static boolean isClassUsed;
4
5 public void processData(String parameter) {
6 Object currentValue = null;
7 }
8}

In the code listing 3.95 , are examples of all four kinds of variables.
• Instance variables: These are variables that are used to store the state of an object
(for example, id). Every object created from a class definition would have its own copy
of the variable. It is valid for and occupies storage for as long as the corresponding object
is in memory.
• Class variables: These variables are explicitly defined within the class-level scope with
a static modifier (for example, isClassUsed). No other variables can have a static
modifier attached to them. Because these variables are defined with the static modifier,
there would always be a single copy of these variables no matter how many times the
class has been instantiated. They live as long as the class is loaded in memory.
• Parameters or Arguments: These are variables passed into a method signature (for
example, parameter). Recall the usage of the args variable in the main method. They
are not attached to modifiers (i.e. public, private, protected or static) and they
can be used everywhere in the method. They are in memory during the execution of the
method and can't be used after the method returns.
• Local variables: These variables are defined and used specifically within the method-
level scope (for example, currentValue) but not in the method signature. They do not
have any modifiers attached to it. They no longer exist after the method has returned.
Question 3.5: Consider the following code:
Question 3.5: [Link]

1 public class SomeClass {


2 public static int c = 1;
3 public int a = c;
4 private int b;
5
6 public void someMethod(int d) {
7 d = c;
8 int e;
9 }
10 }

In the example above, we created five variables: a, b, c, d and e. All these variables have
the same data type int (integer). However, can you tell what kind of variable each one is?
• a and b are instance variables;
• c is a class variable;

5 Chapter 18.1 on page 129

130
Creating variables

• d is a parameter or argument; and,


• e is a local variable.

18.3 Creating variables

Figure 57 A graphical representation of computer memory

Variables and all the information they store are kept in the computer's memory for access.
Think of a computer's memory as a table of data — where each cell corresponds to a
variable.
Upon creating a variable, we basically create a new address space and give it a unique name.
Java goes one step further and lets you define what you can place within the variable — in
Java parlance you call this a data type. So, you essentially have to do two things in order
to create a variable:
• Create a variable by giving it a unique name; and,
• Define a data type for the variable.
The following code demonstrates how a simple variable can be created. This process is
known as variable declaration.

131
Variables

Example:
Code section 3.40: A simple variable declaration.
1 int a;

18.4 Assigning values to variables


Because we have provided a data type for the variable, we have a hint as to what the
variable can and cannot hold. We know that int (integer) data type supports numbers
that are either positive or negative integers. Therefore once a variable is created, we can
provide it with any integer value using the following syntax. This process is called an
assignment operation.

Example:
Code section 3.41: Variable declaration and assignment operation (on differ-
ent lines).
1 int a;
2 a = 10;

Java provides programmers with a simpler way of combining both variable declaration and
assignment operation in one line. Consider the following code:

Example:
Code section 3.42: Variable declaration and assignment operation (on the
same line).
1 int a = 10;

18.5 Grouping variable declarations and assignment


operations
Consider the following code:

Example:
Code section 3.43: Ungrouped declarations.
1 int a;
2 int b;
3 String c;
4 a = 10;
5 b = 20;
6 c = "some text";

There are various ways by which you can streamline the writing of this code. You can group
the declarations of similar data types in one statement, for instance:

132
Identifiers

Example:
Code section 3.44: Grouped declarations.
1 int a, b;
2 String c;
3 a = 10;
4 b = 20;
5 c = "some text";

Alternatively, you can further reduce the syntax by doing group declarations and assign-
ments together, as such:

Example:
Code section 3.45: Grouped declarations and assignments.
1 int a = 10, b = 20;
2 String c = "some text";

18.6 Identifiers
Although memory spaces6 have their own addresses — usually a hash number such as
0xCAD3, etc. — it is much easier to remember a variable's location in the memory if we can
give it a recognizable name. Identifiers are the names we give to our variables. You can
name your variable anything like aVariable, someVariable, age, someonesImportantData
, etcetera. But notice: none of the names we described here has a space within it. Hence,
it is pretty obvious that spaces aren't allowed in variable names. In fact, there are a lot of
other things that are not allowed in variable names. The things that are allowed are:
• Characters A to Z and their lower-case counterparts a to z.
• Numbers 0 to 9. However, numbers should not come at the beginning of a variable's
name.
• And finally, special characters that include only $ (dollar sign) and _ (underscore).
Question 3.6: Which of the ones below are proper variable identifiers?
1. f_name
2. lastname
3. someones name
4. $SomeoneElsesName
5. 7days
6. TheAnswerIs42
I can tell you that 3 and 5 are not the right way to do things around here, the rest are
proper identifiers.
Any valid variable names might be correct but they are not always what you should be
naming your variables for a few reasons as listed below:
• The name of the variable should reflect the value within them.

6 [Link]

133
Variables

• The identifier should be named following the naming guidelines or conventions for doing
so. We will explain that in a bit.
• The identifier shouldn't be a nonsense name like lname, you should always name it prop-
erly: lastName is the best way of naming a variable.

18.7 Naming conventions for identifiers


When naming identifiers, you need to use the following guidelines which ensure that your
variables are named accurately. As we discussed earlier, we should always name our variables
in a way that tells us what they hold. Consider this example:

Example:
Code section 3.46: Unknown process.
1 int a = 24;
2 int b = 365;
3 int c = a * b;

Do you know what this program does? Well, it multiplies two values. That much you
guessed right. But, do you know what those values are? Exactly, you don't. Now consider
this code:

Example:
Code section 3.47: Time conversion.
1 int age = 24;
2 int daysInYear = 365;
3 int ageInDays = age * daysInYear;

Now you can tell what's happening, can't you? However, before we continue, notice the
case of the variables. If a word contains CAPITAL LETTERS, it is in UPPER CASE.
If a word has small letters, it is in lower case. Both cases in a word renders it as mIxEd
CaSe.
The variables we studied so far had a mixed case. When there are two or more words
making up the names of a variable, you need to use a special case called the camel-case.
Just like the humps of a camel, your words need to stand out. Using this technique, the
words first and name could be written as either firstName or FirstName.
The first instance, firstName is what we use as the names of variables. Remember though,
firstName is not the same as FirstName because Java is case-sensitive. Case-sensitive
basically implies that the case in which you wrote one word is the case you have to call
that word in when using them later on. Anything other than that is not the same as you
intended. You'll know more as you progress. You can hopefully tell now why the variables
you were asked to identify weren't proper.

134
Literals (values)

18.8 Literals (values)


Now that we know how variables should be named, let us look at the values of those
variables. Simple values like numbers are called literals. This section shows you what
literals are and how to use them. Consider the following code:

Example:
Code section 3.48: Literals.
1 int age = 24;
2 long bankBalance = 20000005L;

By now, we've only seen how numbers work in assignment statements. Let's look at data
types other than numbers. Characters are basically letters of the English alphabet. When
writing a single character, we use single quotes to encapsulate them. Take a look at the
code below:

Example:
Code section 3.49: Character.
1 char c = 'a';

Why, you ask? Well, the explanation is simple. If written without quotes, the system
would think it's a variable identifier. That's the very distinction you have to make when
differentiating between variables and their literal values. Character data types are a bit
unusual. First, they can only hold a single character. What if you had to store a complete
name within them, say John, would you write something like:

Example:
Code section 3.50: Character list.
1 char firstChar = 'J';
2 char secondChar = 'o';
3 char thirdChar = 'h';
4 char fourthChar = 'n';

Now, that's pathetic. Thankfully, there's a data type that handles large number of charac-
ters, it's called a String. A string can be initialized as follows:

Example:
Code section 3.51: String.
1 String name = "John";

Notice, the use of double quotation marks instead of single quotation marks. That's the
only thing you need to worry about.

135
19 Primitive Types

Primitive types are the most basic data types available within the Java language. There
are 8: boolean, byte, char, short, int, long, float and double. These types serve as
the building blocks of data manipulation in Java. Such types serve only one purpose —
containing pure, simple values of a kind. Because these data types are defined into the Java
type system by default, they come with a number of operations predefined. You can not
define a new operation for such primitive types. In the Java type system, there are three
further categories of primitives:
• Numeric primitives: short, int, long, float and double. These primitive data types
hold only numeric data. Operations associated with such data types are those of simple
arithmetic1 (addition, subtraction, etc.) or of comparisons2 (is greater than, is equal to,
etc.)
• Textual primitives: byte and char. These primitive data types hold characters (that
can be Unicode3 alphabets or even numbers). Operations associated with such types are
those of textual manipulation (comparing two words, joining characters to make words,
etc.). However, byte and char can also support arithmetic operations.
• Boolean and null primitives: boolean and null.
All the primitive types have a fixed size. Thus, the primitive types are limited to a range of
values. A smaller primitive type (byte) can contain less values than a bigger one (long).
Category Types Size (bits) Minimum Value Maximum Precision Example
Value
byte 8 -128 127 From +127 to byte b = 65;
-128
Integer char 16 0 216 -1 All Unicode char c = 'A';
characters4 char c = 65;
short 16 -215 215 -1 From +32,767 to short s = 65;
-32,768
int 32 -231 231 -1 From int i = 65;
+2,147,483,647
to -2,147,483,648
long 64 -263 263 -1 From long l = 65L;
+9,223,372,036,854,775,807
to -
9,223,372,036,854,775,808
float 32 2-149 (2-2-23 )·2127 From float f = 65f;
Floating-point
3.402,823,5 E+38
to 1.4 E-45
double 64 2-1074 (2-2-52 )·21023 From double d =
1.797,693,134,862,315,7 E+308
65.55;
to 4.9 E-324
boolean – – – false, true boolean b =
Other
true;
void – – – – –

1 [Link]
2 Chapter 16.4 on page 124
3 [Link]
4 According to STR01-J. Do not assume that a Java char fully represents a Unicode code point5 . Carnegie
Mellon University - Software Engineering Institute. Retrieved 27 Nov 2018 , not all Unicode characters
fit into a 16 bit representation

137
Primitive Types

Integer primitive types silently overflow:

Console for Code section 3.52


2147483647
Example: -2147483648
Code section 3.52: Several operators. -2147483648

1 int i = Integer.MAX_VALUE;
2 [Link](i);
3 i = i + 1;
4 [Link](i);
5 [Link](Integer.MIN_VALUE);

As Java is strongly typed, you can't assign a floating point number (a number with a decimal
point) to an integer variable:

1 Warning
Code section 3.53: Setting a floating point number as a value to an int
(integer) type.
1 int age;
2 age = 10.5;

A primitive type should be set by an appropriate value. The primitive types can be initial-
ized with a literal. Most of the literals are primitive type values, except String Literals6 ,
which are instance of the String class.

19.1 Numbers in computer science


Programming may not be as trivial or boring as just crunching huge numbers any more.
However, huge chunks of code written in any programming language today, let alone Java,
obsessively deal with numbers, be it churning out huge prime numbers,7 or just calculating
a cost of emission from your scooter. In 1965, Gemini V8 space mission escaped a near-fatal
accident caused by a programming error.9 Again in 1979, a computer program overesti-
mated the ability of five nuclear reactors to withstand earthquakes; the plants shut down
temporarily.10 There is one thing common to both these programming errors: the subject

6 [Link]
7 As of edit (11 December 2013), the Great Internet Mersenne Prime Search ^{[Link]
org/wiki/Great%20Internet%20Mersenne%20Prime%20Search} project has so far identified the largest
prime number as being 17,425,170 digits long. Prime numbers are valuable to cryptologists as the bigger
the number, the securer they can make their data encryption logic using that particular number.
8 [Link]
9 Gemini 5 landed 130 kilometers short of its planned Pacific Ocean landing point due to a software
error. The Earth's rotation rate had been programmed as one revolution per solar day ^{https:
//[Link]/wiki/solar%20time} instead of the correct value, one revolution per sidereal day
^{[Link] .
10 A program used in their design used an arithmetic sum of variables when it should have used the sum of
their absolute values. (Evars Witt, ”The Little Computer and the Big Problem”, AP Newswire, 16 March

138
Numbers in computer science

data, being computed at the time the errors occurred, was numeric. Out of past experi-
ence, Java came bundled with revised type checking for numeric data and put significant
emphasis on correctly identifying different types of it. You must recognise the importance
of numeric data when it comes to programming.
Numbers are stored in memory using a binary system. The memory is like a grid of cells:

Each cell can contain a binary digit (shortened to bit), that is to say, zero or one:
0 1 1 0 0 1 0 1

Actually, each cell does contain a binary digit, as one bit is roughly equivalent to 1 and
an empty cell in the memory signifies 0. A single binary digit can only hold two possible
values: a zero or a one.
Memory state Gives
0 → 0
1 → 1

Multiple bits held together can hold multiple permutations — 2 bits can hold 4 possible
values, 3 can hold 8, and so on. For instance, the maximum number 8 bits can hold (
11111111 in binary) is 255 in the decimal system. So, the numbers from 0 to 255 can fit
within 8 bits.
Memory state Gives
0 0 0 0 0 0 0 0 → 0
0 0 0 0 0 0 0 1 → 1
0 0 0 0 0 0 1 0 → 2
0 0 0 0 0 0 1 1 → 3
...
...

1 1 1 1 1 1 1 1 → 255

It is all good, but this way, we can only host positive numbers (or unsigned integers). They
are called unsigned integers. Unsigned integers are whole number values that are all positive
and do not attribute to negative values. For this very reason, we would ask one of the 8
bits to hold information about the sign of the number (positive or negative). This leaves us
with just 7 bits to actually count out a number. The maximum number that these 7 bits
can hold (1111111) is 127 in the decimal system.
Positive numbers
Memory state Gives
0 0 0 0 0 0 0 0 → 0
0 0 0 0 0 0 0 1 → 1
0 0 0 0 0 0 1 0 → 2
0 0 0 0 0 0 1 1 → 3

1979. See also Peter Neumann, ”An Editorial on Software Correctness and the Social Process” Software
Engineering Notes, Volume 4(2), April 1979, page 3)

139
Primitive Types

...
... ...

0 1 1 1 1 1 1 1 → 127

Negative numbers
Memory state Gives
1 0 0 0 0 0 0 0 → -128
1 0 0 0 0 0 0 1 → -127
1 0 0 0 0 0 1 0 → -126
1 0 0 0 0 0 1 1 → -125
...
... ...

1 1 1 1 1 1 1 1 → -1

Altogether, using this method, 8 bits can hold numbers ranging from -128 to 127 (including
zero) — a total of 256 numbers. Not a bad pay-off one might presume. The opposite to an
unsigned integer is a signed integer that have the capability of holding both positive and
negative values.
But, what about larger numbers. You would need significantly more bits to hold larger
numbers. That's where Java's numeric types come into play. Java has multiple numeric
types — their size dependent on the number of bits that are at play.
In Java, numbers are dealt with using data types specially formulated to host numeric data.
But before we dive into these types, we must first set some concepts in stone. Just like you
did in high school (or even primary school), numbers in Java are placed in clearly distinct
groups and systems. As you'd already know by now, number systems includes groups like
the integer numbers (0, 1, 2 ... ∞); negative integers (0, -1, -2 ... -∞) or even real and
rational numbers (value of Pi11 , ¾, 0.333~, etcetera). Java simply tends to place these
numbers in two distinct groups, integers (-∞... 0 ... ∞) and floating point numbers (any
number with decimal points or fractional representation). For the moment, we would only
look into integer values as they are easier to understand and work with.

19.2 Integer types in Java


With what we have learned so far, we will identify the different types of signed integer
values that can be created and manipulated in Java. Following is a table of the most basic
numeric types: integers. As we have discussed earlier, the data types in Java for integers
caters to both positive and negative values and hence are signed numeric types. The
size in bits for a numeric type determines what its minimum and maximum value would be.
If in doubt, one can always calculate these values.
Lets see how this new found knowledge of the basic integer types in Java fits into the
picture. Say, you want to numerically manipulate the days in a year — all 365 days. What
type would you use? Since the data type byte only goes up to 127, would you risk giving
it a value greater than its allowed maximum. Such decisions might save you from dreaded

11 [Link]

140
Integer numbers and floating point numbers

errors that might occur out of the programmed code. A much more sensible choice for such
a numeric operation might be a short. Now, why couldn't they make just one data type
to hold all kinds of numbers? Let's explore why.
When you tell a program you need to use an integer, say even a byte, the Java program
allocates a space in the memory. It allocates whole 8 bits of memory. Where it wouldn't
seem to matter for today's memory modules that have place for almost a dozen trillion
such bits, it matters in other cases. Once allocated that part of the memory gets used
and can only be claimed back after the operation is finished. Consider a complicated Java
program where the only data type you'd be using would be long integers. What happens
when there's no space for more memory allocation jobs? Ever heard of the Stack Overflow
error12 s. That's exactly what happens — your memory gets completely used up and fast.
So, choose your data types with extreme caution.
Enough talk, let's see how you can create a numeric type. A numeric type begins with the
type's name (short, int, etc.) and then provides with a name for the allocated space in the
memory. Following is how it's done. Say, we need to create a variable to hold the number
of days in a year.

Example:
Code section 3.54: Days in a year.
1 short daysInYear = 365;

Here, daysInYear is the name of the variable that holds 365 as its value, while short is
the data type for that particular value. Other uses of integer data types in Java might see
you write code such as this given below:

Example:
Code section 3.55: Integer data types in Java.
1 byte maxByte = 127;
2 short maxShort = 32767;
3 int maxInt = 2147483647;
4 long maxLong = 9223372036854775807L;

19.3 Integer numbers and floating point numbers


The data types that one can use for integer numbers are byte, short, int and long but
when it comes to floating point numbers, we use float or double. Now that we know that,
we can modify the code in the code section 3.5313 as:

Example:
Code section 3.56: Correct floating point declaration and assignment.
1 double age = 10.5;

12 [Link]
13 Chapter 19 on page 137

141
Primitive Types

Why not float, you say? If we'd used a float, we would have to append the number with
a f as a suffix, so 10.5 should be 10.5f as in:

Example:
Code section 3.57: The correct way to define floating point numbers of type
float.
1 float age = 10.5f;

Floating-point math never throws exceptions. Dividing a non-zero value by 0 equals


infinity. Dividing a non-infinite value by infinity equals 0.
Question 3.7: Consider the following code:

Example:
Question 3.7: Primitive type assignments.
1 ...
2
3 a = false;
4 b = 3.2;
5 c = 35;
6 d = -93485L;
7 e = 'q';

These are five variables. There are a long, a byte, a char, a double and a boolean.
Retrieve the type of each one.

Example:
Answer 3.7: Primitive type assignments and declarations.
1 boolean a;
2 double b;
3 byte c;
4 long d;
5 char e;
6
7 a = false;
8 b = 3.2;
9 c = 35;
10 d = -93485L;
11 e = 'q';

• a can only be the boolean because only a boolean can handle boolean values.
• e can only be the char because only a char can contain a character.
• b can only be the double because only a double can contain a decimal number here.
• d is the long because a byte can not contain such a low value.
• c is the remaining one so it is the byte.

19.4 Data conversion (casting)


Data conversion (casting) can happen between two primitive types. There are two kinds of
casting:

142
Notes

• Implicit: casting operation is not required; the magnitude of the numeric value is always
preserved. However, precision may be lost when converting from integer to floating point
types
• Explicit: casting operation required; the magnitude of the numeric value may not be
preserved

Example:
Code section 3.58: Implicit casting (int is converted to long,casting is not
needed).
1 int i = 65;
2 long l = i;

Example:
Code section 3.59: Explicit casting (long is converted to int,casting is needed).
1 long l = 656666L;
2 int i = (int) l;

The following table shows the conversions between primitive types, it shows the casting
operation for explicit conversions:
from byte from char from short from int from long from float from double from boolean
to byte - (byte) (byte) (byte) (byte) (byte) (byte) N/A
to char - (char) (char) (char) (char) (char) N/A
to short (short) - (short) (short) (short) (short) N/A
to int - (int) (int) (int) N/A
to long - (long) (long) N/A
to float - (float) N/A
to double - N/A
to boolean N/A N/A N/A N/A N/A N/A N/A -

Unlike C, C++ and similar languages, Java can't represent false as 0 or null and can't
represent true as non-zero. Java can't cast from boolean to a non-boolean primitive data
type, or vice versa.
For non primitive types:
to Integer to Float to Double to String to Array
Integer - (float)x (double)x [Link]() new int[] {x}
[Link]() [Link](x)
Float [Link]- - (double)x [Link]() new float[] {x}
imalFor-
mat(”#”).for-
mat(x)
Double [Link]- [Link]- - [Link]() new double[] {x}
imalFor- imalFor-
mat(”#”).for- mat(”#”).for-
mat(x) mat(x)
String [Link](x) [Link](x) [Link]- - new String[] {x}
ble(x)
Array x[0] x[0] x[0] [Link](x) -

19.5 Notes

143
20 Arithmetic expressions

In order to do arithmetic in Java, one must first declare at least one variable. Typically one
declares a variable and assigns it a value before any arithmetic is done. Here's an example
of declaring an integer variable:

Example:
Code section 3.59: Variable assignation.
1 int x = 5;

After creating a variable, one can manipulate its value by using Java's operators: +
(addition), - (subtraction), * (multiplication), / (integer division), % (modulo or remain-
der1 ), ++ (pre- & postincrement by one), -- (pre- & postdecrement by one).

1 [Link]

145
Arithmetic expressions

Code listing 3.10: [Link]


Console:
1 public class Operators { Console for Code listing 3.10
2 public static void main(String[] args) {
3 int x = 5; x = 5
4 [Link](”x = ” + x);
5 [Link](); --- Addition ---
6 x + 2 = 7
7 [Link](”— Addition —”); x = 5
8 x = 5;
9 [Link](”x + 2 = ” + (x + 2)); --- Subtraction ---
10 [Link](”x = ” + x); x - 4 = 1
11 [Link](); x = 5
12
13 [Link](”— Subtraction —”); --- Multiplication ---
14 x = 5; x * 3 = 15
15 [Link](”x - 4 = ” + (x - 4)); x = 5
16 [Link](”x = ” + x);
17 [Link](); --- (Integer) Division ---
18 x / 2 = 2
19 [Link](”— Multiplication —”); x = 5
20 x = 5;
21 [Link](”x * 3 = ” + (x * 3)); --- Modulo (Remainder) ---
22 [Link](”x = ” + x); x % 2 = 1
23 [Link](); x = 5
24
25 [Link](”— (Integer) Division —”); --- Preincrement by one ---
26 x = 5; ++x = 6
27 [Link](”x / 2 = ” + (x / 2)); x = 6
28 [Link](”x = ” + x);
29 [Link](); --- Predecrement by one ---
30 --x = 4
31 [Link](”— Modulo (Remainder) —”); x = 4
32 x = 5;
33 [Link](”x % 2 = ” + (x % 2)); --- Postincrement by one ---
34 [Link](”x = ” + x); x++ = 5
35 [Link](); x = 6
36
37 [Link](”— Preincrement by one —”); --- Postdecrement by one ---
38 x = 5; x-- = 5
39 [Link](”++x = ” + (++x )); x = 4
40 [Link](”x = ” + x);
41 [Link]();
42
43 [Link](”— Predecrement by one —”);
44 x = 5;
45 [Link](”–x = ” + (–x ));
46 [Link](”x = ” + x);
47 [Link]();
48
49 [Link](”— Postincrement by one —”);
50 x = 5;
51 [Link](”x++ = ” + (x++ ));
52 [Link](”x = ” + x);
53 [Link]();
54
55 [Link](”— Postdecrement by one —”);
56 x = 5;
57 [Link](”x– = ” + (x– ));
58 [Link](”x = ” + x);
59 [Link]();
60 }
61 }

146
Notes

The division operator rounds towards zero: 5/2 is 2, and -5/2 is -2. The remainder operator
has the same sign as the left operand; it is defined such that ((a/b)*b) + (a%b) is always
equal to a. The preincrement, predecrement, postincrement, and postdecrement operators
are special: they also change the value of the variable, by adding or subtracting one.
The only difference is that preincrement/decrement returns the new value of the variable;
postincrement returns the original value of the variable.
Question 3.8: Consider the following code:
Question 3.8: [Link]

1 public class Question8 {


2 public static void main(String[] args) {
3 int x = 10;
4 x = x + 10;
5 x = 2 * x;
6 x = x - 19;
7 x = x / 3;
8 [Link](x);
9 }
10 }

What will be printed in the standard output?

Console:
Output for Question 3.8
7

int x = 10; => 10

x = x + 10; => 20

x = 2 * x; => 40

x = x - 19; => 21

x = x / 3; => 7

When using several operators in the same expression, one must consider Java's order of
precedence. Java uses the standard PEMDAS (Parenthesis, Exponents, Multiplication and
Division, Addition and Subtraction) order. When there are multiple instances of the same
precedence, Java reads from left to right. Consider what the output of the following code
would be:

147
Arithmetic expressions

Example: Console:
Code section 3.60: Several operators. Console for Code section 3.60
56
1 [Link](10*5 + 100/10 - 5 + 7%2);

The following chart shows how Java would compute this expression:
Figure 3.1: Computation of an arithmetic expression in the Java programming language

Figure 58

148
Using bitwise operators within Java

Besides performing mathematical functions, there are also operators to assign numbers to
variables (each example again uses the variable initialized as x = 5):
Code listing 3.11: [Link]
Console:
1 public class Assignments { Console for Code listing 3.11
2 public static void main(String[] args) { Assignment (x = 3) : 3
3 int x = 5; Assign x plus another integer to itself (x += 5): 10
4 x = 3; Assign x minus another integer to itself (x -
5 [Link](”Assignment = 4):(x1 =
3) Assign x multiplied by another integer to itself (x *= 6): 30
6 : ” + x); Assign x divided by another integer to itself (x /= 5): 1
7
8 x = 5;
9 x += 5;
10 [Link](”Assign x plus another integer to itself (x
+=
11 5): ” + x);
12
13 x = 5;
14 x -= 4;
15 [Link](”Assign x minus another integer to itself (x
-=
16 4): ” + x);
17
18 x = 5;
19 x *= 6;
20 [Link](”Assign x multiplied by another integer to itself (x
*=
21 6): ” + x);
22
23 x = 5;
24 x /= 5;
25 [Link](”Assign x divided by another integer to itself (x
/=
26 5): ” + x);
27 }
28 }

20.1 Using bitwise operators within Java


Java has besides arithmetic operators a set of bit operators to manipulate the bits in a
number, and a set of logical operators. The bitwise logical operators are
Operator Function Value of Example Example Value of
x before input output x after
& Bitwise AND 7 x&27 3 7
| Bitwise OR 7 x|27 31 7
^ Bitwise XOR 7 x^27 28 7
~ Bitwise inversion 7 ~x -8 7

Besides these logical bitwise functions, there are also operators to assign numbers to vari-
ables (x = -5):

Opera- Function Example Example output


tor input

149
Arithmetic expressions

Opera- Function Example Example output


tor input
&= Assign x bitwisely x &= 3 3
ANDed with another
value to itself
|= Assign x bitwisely x|= 3 -5
ORed with another
value to itself
^= Assign x bitwisely x ^= 3 -8
XORed with another
value to itself
<<= Assign x divided by x <<= 1 -10
another integer to itself
>>= Assign x bitwisely x >>= 1 -3
negated with another
value to itself
>>>= Assign x bitwisely x >>>= 1 2,305,843,009,213,693,949 (64
negated with another bit)
value to itself

The shift operators are used to shift the bits to the left or right, which is also a quick way
to multiply/divide by two:
Operator Function Value of Example Example output Value of
x before input x after
<< Logical shift left -15 x << 2 -60 -15
>> Arithmetic shift right -15 x >> 3 -2 -15
>>> Logical shift right -15 x >>> 3 -15
2,305,843,009,213,693,937
(64 bit)

150
21 Literals

Java Literals are syntactic representations of boolean, character, numeric, or string data.
Literals provide a means of expressing specific values in your program. For example, in the
following statement, an integer variable named count is declared and assigned an integer
value. The literal 0 represents, naturally enough, the value zero.

Example:
Code section 3.61: Numeric literal.
1 int count = 0;

The code section 3.621 contains two number literals followed by two boolean literals at line
1, one string literal followed by one number literal at line 2, and one string literal followed
by one real number literal at line 3:

Example:
Code section 3.62: Literals.
1 (2 > 3) ? true : false;
2 "text".substring(2);
3 [Link]("Display a hard coded float: " + 37.19f);

21.1 Boolean Literals


There are two boolean literals
• true represents a true boolean value
• false represents a false boolean value
There are no other boolean literals, because there are no other boolean values!

21.2 Numeric Literals


There are three types of numeric literals in Java.

21.2.1 Integer Literals


In Java, you may enter integer numbers in several formats:

1 Chapter 21.4 on page 154

151
Literals

1. As decimal numbers such as 1995, 51966. Negative decimal numbers such as -42 are
actually expressions consisting of the integer literal with the unary negation operation
-.
2. As octal numbers, using a leading 0 (zero) digit and one or more additional octal
digits (digits between 0 and 7), such as 077. Octal numbers may evaluate to negative
numbers; for example 037777777770 is actually the decimal value -8.
3. As hexadecimal numbers, using the form 0x (or 0X) followed by one or more hexadec-
imal digits (digits from 0 to 9, a to f or A to F). For example, 0xCAFEBABEL is
the long integer 3405691582. Like octal numbers, hexadecimal literals may represent
negative numbers.
4. Starting in J2SE 7.0, as binary numbers, using the form 0b (or 0B) followed by one or
more binary digits (0 or 1). For example, 0b101010 is the integer 42. Like octal and
hex numbers, binary literals may represent negative numbers.
By default, the integer literal primitive type is int. If you want a long, add a letter el
suffix (either the character l or the character L) to the integer literal. This suffix denotes
a long integer rather than a standard integer. For example, 3405691582L is a long integer
literal. Long integers are 8 bytes in length as opposed to the standard 4 bytes for int. It is
best practice to use the suffix L instead of l to avoid confusion with the digit 1 (one) which
looks like l in many fonts: 200l 6= 2001. If you want a short integer literal, you have to
cast it.
Starting in J2SE 7.0, you may insert underscores between digits in a numeric literal. They
are ignored but may help readability by allowing the programmer to group digits.

21.2.2 Floating Point Literals


Floating point numbers are expressed as decimal fractions or as exponential notation:

Example:
Code section 3.63: Floating point literals.
1 double decimalNumber = 5.0;
2 decimalNumber = 89d;
3 decimalNumber = 0.5;
4 decimalNumber = 10f;
5 decimalNumber = 3.14159e0;
6 decimalNumber = 2.718281828459045D;
7 decimalNumber = 1.0e-6D;

Floating point numbers consist of:


1. an optional leading + or - sign, indicating a positive or negative value; if omitted, the
value is positive,
2. one of the following number formats
• integer digits (must be followed by either an exponent or a suffix or both, to distin-
guish it from an integer literal)
• integer digits .
• integer digits . integer digits
• . integer digits
3. an optional exponent of the form

152
String Literals

• the exponent indicator e or E


• an optional exponent sign + or - (the default being a positive exponent)
• integer digits representing the integer exponent value
4. an optional floating point suffix:
• either f or F indicating a single precision (4 bytes) floating point number, or
• d or D indicating the number is a double precision floating point number (by default,
thus the double precision (8 bytes) is default).
Here, integer digits represents one or more of the digits 0 through 9.
Starting in J2SE 7.0, you may insert underscores between digits in a numeric literal. They
are ignored but may help readability by allowing the programmer to group digits.

21.2.3 Character Literals


Character literals are constant valued character expressions embedded in a Java program.
Java characters are sixteen bit Unicode characters, ranging from 0 to 65535. Character
literals are expressed in Java as a single quote, the character, and a closing single quote (’a’,
’7’, ’$’, ’π’). Character literals have the type char, an unsigned integer primitive type2 .
Character literals may be safely promoted to larger integer types such as int and long.
Character literals used where a short or byte is called for must be cast to short or byte
since truncation may occur.

21.3 String Literals


String literals consist of the double quote character (") (ASCII 34, hex 0x22), zero or more
characters (including Unicode characters), followed by a terminating double quote character
("), such as: Ceci est une string.
So a string literal follows the following grammar:
<STRING :
"\""
( (~["\"","\\","\n","\r"])
|("\\"
( ["n","t","b","r","f","\\","'","\""]
|["0"-"7"](["0"-"7"])?
|["0"-"3"]["0"-"7"]["0"-"7"]
)
)
)*
"\"">

Within string and character literals, the backslash character can be used to escape special
characters, such as unicode escape sequences3 , or the following special characters:

Name Character ASCII hex


Backspace \b 8 0x08

2 Chapter 18.8 on page 135


[Link]
3
20Sequences

153
Literals

Name Character ASCII hex


TAB \t 9 0x09
NUL character \0 0 0x00
newline \n 10 0x0a
carriage control \r 13 0xd
double quote \" 34 0x22
single quote \' 39 0x27
backslash \\ 92 0x5c

String literals may not contain unescaped newline or linefeed characters. However, the Java
compiler will evaluate compile time expressions, so the following String expression results
in a string with three lines of text:

Example:
Code section 3.64: Multi-line string.
1 String text = "This is a String literal\n"
2 + "which spans not one and not two\n"
3 + "but three lines of text.\n";

21.4 null
null is a special Java literal which represents a null value: a value which does not refer to
any object. It is an error to attempt to dereference the null value — Java will throw a
NullPointerException. null is often used to represent uninitialized state.

21.5 Mixed Mode Operations


In concatenation operations, the values in brackets are concatenated first. Then the values
are concatenated from the left to the right. Be careful when mixing character literals and
integers in String concatenation operations:

Example: Console:
Code section 3.65: Concatenation operations. Console for Code section 3.65
120? 49248
1 int > 2 int zero = '0';
3
4 [Link]("120? " + one + '2' + zero);

The unexpected results arise because ’1’ and ’0’ are converted twice. The expression is
concatenated as such:
"120? " + one + '2' + zero
"120? " + 49 + '2' + 48
"120? 49" + '2' + 48

154
Mixed Mode Operations

"120? 492" + 48
"120? 49248"

1. one and zero are integers. So they store integer values. The integer value of ’1’ is 49
and the integer value of ’0’ is 48.
2. So the first concatenation concatenates 120? and 49. 49 is first converted into String,
yielding 49 and the concatenation returns the string 120? 49.
3. The second concatenation concatenates 120? 49 and ’2’. ’2’ is converted into the
String 2 and the concatenation returns the string 120? 492.
4. The concatenation between 120? 492 and ’0’ returns the string 120? 49248.
The code section 664 yields the desired result:

Example: Console:
Code section 3.66: Correct primitive type. Console for Code section 3.66
120? 120
1 char > 2 char zero = '0';
3
4 [Link]("120? " + one + '2' + zero);

Question 3.9: Consider the following code:

Example: Console:
Question 3.9: New concatenation operations. Console for Question 3.9
3? 147
1 int 102? 10020
2 int zero = '0'; 102? 150
3
4 [Link](" 3? " + (one + '2' + zero));
5 [Link]("102? " + 100 + '2' + 0);
6 [Link]("102? " + (100 + '2' + 0));

Explain the results seen. For the first line:

" 3? " + (one + '2' + zero)


" 3? " + (49 + '2' + 48)
" 3? " + (99 + 48)
" 3? " + 147
" 3? 147"

For the second line:


"102? " + 100 + '2' + 0
"102? 100" + '2' + 0

4 Chapter 21.4 on page 154

155
Literals

"102? 1002" + 0
"102? 10020"

For the last line:


"102? " + (100 + '2' + 0)
"102? " + (150 + 0)
"102? " + 150
"102? 150"

156
22 Methods
Methods are how we communicate with objects. When we invoke or call a method we are
asking the object to carry out a task. We can say methods implement the behaviour of
objects. For each method we need to give a name, we need to define its input parameters
and we need to define its return type. We also need to set its visibility1 (private, protected
or public). If the method throws a checked exception, that needs to be declared as well. It
is called a method definition. The syntax of method definition is:
1 MyClass {
2 ...
3 public ReturnType methodName(ParamOneType parameter1, ParamTwoType
parameter2)
4 {
5 ...
6 return returnValue;
7 }
8 ...
9 }

We can declare that the method does not return anything using the void Java keyword.
For example:

Example:
Code section 3.67: Method without returned data.
1 private void methodName(String parameter1, String parameter2) {
2 ...
3 return;
4 }

When the method returns nothing, the return keyword at the end of the method is optional.
When the execution flow reaches the return keyword, the method execution is stopped and
the execution flow returns to the caller method. The return keyword can be used anywhere
in the method as long as there is a way to execute the instructions below:

1 Warning
Code section 3.68: return keyword location.

1 Chapter 39.5 on page 292

157
Methods

1 private void aMethod(int a, int b) {


2 int c = 0;
3 if (a > 0) {
4 c = a;
5 return;
6 }
7 int c = c + b;
8 return;
9 int c = c * 2;
10 }

In the code section 3.682 , the return keyword at line 5 is well placed because the instructions
below can be reached when a is negative or equal to 0. However, the return keyword at
line 8 is badly placed because the instructions below can't be reached.
Question 3.9: Consider the following code:

Example:
Question 3.9: Compiler error.
1 private int myMethod(int a, int b, boolean c) {
2 b = b + 2;
3 if (a > 0) {
4 a = a + b;
5 return a;
6 } else {
7 a = 0;
8 }
9 }

The code above will return a compiler error. Why?

Example:
Answer 3.9: Compiler error.
1 private int myMethod(int a, int b, boolean c) {
2 b = b + 2;
3 if (a > 0) {
4 a = a + b;
5 return a;
6 } else {
7 a = 0;
8 }
9 }

The method is supposed to return a int but when a is negative or equal to 0, it returns
nothing.

2 Chapter 22.2 on page 161

158
Parameter passing

22.1 Parameter passing


We can pass any primitive data types3 or reference data type to a method.

22.1.1 Primitive type parameter


The primitive types are passed in by value. It means that as soon as the primitive type
is passed in, there is no more link between the value inside the method and the source
variable:

Example:
Code section 3.69: A method modifying a variable.
1 private void modifyValue(int number) {
2 number += 1;
3 }

Example: Console:
Code section 3.70: Passing primitive value to method. Output for Code section 3.70
0
1 int i = 0;
2 modifyValue(i);
3 [Link](i);

As you can see in code section 3.704 , the modifyValue() method has not modified the value
of i.

22.1.2 Reference type parameter


The object references are passed by value. It means that:
• There is no more link between the reference inside the method and the source reference,
• The source object itself and the object itself inside the method are still the same.
You must understand the difference between the reference of an object and the object itself.
An object reference is the link between a variable name and an instance of object:

Object object ⇔new Object()

An object reference is a pointer, an address to the object instance.


The object itself is the value of its attributes inside the object instance:

[Link] ⇒ ”James”
[Link] ⇒ ”Gosling”
[Link] ⇒ ”May 19”

3 Chapter 18.8 on page 135


4 Chapter 22.2 on page 161

159
Methods

Take a look at the example above:

Example:
Code section 3.71: A method modifying an object.
1 private void modifyObject(FirstClass anObject) {
2 [Link]("Susan");
3 }

Example: Console:
Code section 3.72: Passing reference value to method. Output for Code section 3.72
Susan
1 FirstClass object = new FirstClass();
2 [Link]("Christin");
3
4 modifyObject(object);
5
6 [Link]([Link]());

The name has changed because the method has changed the object itself and not the
reference. Now take a look at the other example:

Example:
Code section 3.73: A method modifying an object reference.
1 private void modifyObject(FirstClass anObject) {
2 anObject = new FirstClass();
3 [Link]("Susan");
4 }

Example: Console:
Code section 3.74: Passing reference value to method. Output for Code section 3.74
Christin
1 FirstClass object = new FirstClass();
2 [Link]("Christin");
3
4 modifyObject(object);
5
6 [Link]([Link]());

The name has not changed because the method has changed the reference and not the
object itself. The behavior is the same as if the method was in-lined and the parameters
were assigned to new variable names:

160
Variable argument list

Example: Console:
Code section 3.75: In-lined method. Output for Code section 3.75
Christin
1 FirstClass object = new FirstClass();
2 [Link]("Christin");
3
4 // Start of the method
5 FirstClass anObject = object;
6 anObject = new FirstClass();
7 [Link]("Susan");
8 // End of the method
9
10 [Link]([Link]());

22.2 Variable argument list


Java SE 5.0 added syntactic support for methods with variable argument list5 , which sim-
plifies the typesafe usage of methods requiring a variable number of arguments. Less for-
mally, these parameters are called varargs[Link]
in-java-50/. The type of a variable parameter must be followed with ..., and Java will
box all the arguments into an array:

Example:
Code section 3.76: A method using vararg parameters.
1 public void drawPolygon(Point... points) {
2 //…
3 }

When calling the method, a programmer can simply separate the points by commas, without
having to explicitly create an array6 of Point objects. Within the method, the points can
be referenced as points[0], points[1], etc. If no points are passed, the array has a length
of zero.
A method can have both normal parameters and a variable parameter but the variable
parameter must always be the last parameter. For instance, if the programmer is required
to use a minimum number of parameters, those parameters can be specified before the
variable argument:

Example:
Code section 3.77: Variable arguments.
1 // A polygon needs at least three points.
2 public void drawPolygon(Point p1, Point p2, Point p3, Point... otherPoints) {
3 //…
4 }

5 [Link]
6 Chapter 26.6 on page 227

161
Methods

22.3 Return parameter


A method may return a value (which can be a primitive type or an object reference). If the
method does not return a value we use the void Java keyword.
However, a method can return only one value so what if you want to return more than
one value from a method? You can pass in an object reference to the method, and let the
method modify the object properties so the modified values can be considered as an output
value from the method. You can also create an Object array inside the method, assign the
return values and return the array to the caller. However, this gives a problem if you want
to mix primitive data types and object references as the output values from the method.
There is a better approach, define a special return object with the needed return values.
Create that object inside the method, assign the values and return the reference to this
object. This special object is ”bound” to this method and used only for returning values, so
do not use a public class. The best way is to use a nested class, see example below:
Code listing 3.12: Multiple returned variables.

1 public class MyObject {


2 ...
3
4 /** Nested object is for return values from getPersonInfoById method */
5 private static class ReturnObject {
6 private int age;
7 private String name;
8
9 public void setAge(int age) {
10 [Link] = age;
11 }
12
13 public int getAge() {
14 return age;
15 }
16
17 public void setName(String name) {
18 name = name;
19 }
20
21 public String getName() {
22 return name;
23 }
24 } // End of nested class definition
25
26 /** Method using the nested class to return values */
27 public ReturnObject getPersonInfoById(int id) {
28 int age;
29 String name;
30 ...
31 // Get the name and age based on the ID from the database
32 ...
33 ReturnObject result = new ReturnObject();
34 [Link](age);
35 [Link](name);
36
37 return result;
38 }
39 }

In the above example the getPersonInfoById method returns an object reference that
contains both values of the name and the age. See below how you may use that object:

162
Special method, the constructor

Example:
Code section 3.78: Retrieving the values.
1 MyObject object = new MyObject();
2 [Link] person = [Link](102);
3
4 [Link]("Person Name=" + [Link]());
5 [Link]("Person Age =" + [Link]());

Question 3.10: Consider the following code:

Example:
Question 3.10: Compiler error.
1 private int myMethod(int a, int b, String c) {
2 if (a > 0) {
3 c = "";
4 return c;
5 }
6 int b = b + 2;
7 return b;
8 }

The code above will return a compiler error. Why?

Example:
Answer 3.10: Compiler error.
1 private int myMethod(int a, int b, String c) {
2 if (a > 0) {
3 c = "";
4 return c;
5 }
6 int b = b + 2;
7 return b;
8 }

The method is supposed to return a int but at line 4, it returns c, which is a String.

22.4 Special method, the constructor


The constructor is a special method called automatically when an object is created with
the new keyword. Constructor does not have a return value and its name is the same as
the class name. Each class must have a constructor. If we do not define one, the compiler
will create a default so called empty constructor automatically.
Code listing 3.13: Automatically created constructor.

1 public class MyClass {


2 /**
3 * MyClass Empty Constructor
4 */
5 public MyClass() {

163
Methods

6 }
7}

22.5 Static methods


A static method is a method that can be called without an object instance. It can be called
on the class directly. For example, the valueOf(String) method of the Integer class is a
static method:

Example:
Code section 3.79: Static method.
1 Integer i = [Link]("10");

The static keyword makes attributes instance-agnostic. This means that you cannot refer-
ence a static attribute of a single object (because such a specific object attribute doesn't
exist). Instead, only one instance of a static attribute exists, whether there is one object in
the JVM or one hundred. Here is an example of using a static attribute in a static method:

Example:
Code section 3.80: Static attribute.
1 private static int count = 0;
2
3 public static int getNewInteger() {
4 return count++;
5 }

You can notice that when you use [Link](), out is a static attribute of the
System class. A static attribute is related to a class, not to any object instance. This is
how Java achieves one universal output stream that we can use to print output. Here is a
more complex use case:
Code listing 3.14: A static attribute.
Console:
1 public class MyProgram { Output for Code listing 3.14
2 4
3 public static int count = 0;
4
5 public static void main (String[] args) {
6 [Link]++;
7
8 MyProgram program1 = new MyProgram();
9 [Link]++;
10
11 MyProgram program2 = new MyProgram();
12 [Link]++;
13
14 new MyProgram().count++;
15 [Link]([Link]);
16 }
17 }

164
Static methods

Question 3.11: Visit the Oracle JavaDoc of the class [Link].Integer7 .


How many static fields does this class have? 4.
• int MAX_VALUE,
• int MIN_VALUE,
• int SIZE and
• Class<Integer> TYPE.
To learn how to overload and override a method, see Overloading Methods and Construc-
tors8 .

7 [Link]
8 Chapter 37.2 on page 278

165
23 API/[Link]

String is a class built into the Java language defined in the [Link] package. It rep-
resents character strings. Strings are ubiquitous in Java. Study the String class and its
methods carefully. It will serve you well to know how to manipulate them skillfully. String
literals1 in Java programs, such as ”abc”, are implemented as instances of this class like this:

Example:
Code section 3.81: String example.
1 String str = "This is string literal";

On the right hand side a String object is created represented by the string literal. Its object
reference is assigned to the str variable.

23.1 Immutability
Strings are immutable; that is, they cannot be modified once created. Whenever it looks as
if a String object was modified actually a new String object was created. For instance, the
[Link]() method returns the string with leading and trailing whitespace removed.
Actually, it creates a new trimmed string and then returns it. Pay attention on what
happens in Code section 3.822 :

Example: Console:
Code section 3.82: Immutability. Output for Code section 3.82
Java is great.
1 String badlyCutText = " Java is great. "; Java is great.
2 [Link](badlyCutText);
3
4 [Link]();
5 [Link](badlyCutText);

The trim() method call does not modify the object so nothing happens. It creates a new
trimmed string and then throws it away.

1 Chapter 21.3 on page 153


2 Chapter 23.3 on page 169

167
API/[Link]

Example: Console:
Code section 3.83: Assignment. Output for Code section 3.83
Java is great.
1 String badlyCutText = " Java is great. Java is great.
";
2 [Link](badlyCutText);
3
4 badlyCutText = [Link]();
5 [Link](badlyCutText);

The returned string is assigned to the variable. It does the job as the trim() method has
created a new String instance.

23.2 Concatenation
The Java language provides special support for the string concatenation with operator +:

Example: Console:
Code section 3.84: Examples of concatenation. Output for Code section 3.84
First part
1 [Link]("First part"); second part
2 [Link](" second part"); First part second part
3 String str = "First part" + " second part";
4 [Link](str);

The concatenation is not always processed at the same time. Raw string literals3 concate-
nation is done at compile time, hence there is a single string literal in the byte code of the
class. Concatenation with at least one object is done at runtime.
+ operator can concatenate other objects with strings. For instance, integers will be con-
verted to strings before the concatenation:

Example: Console:
Code section 3.85: Concatenation of integers. Output for Code section 3.85
Age=25
1 [Link]("Age=" + 25);

Each Java object has the String toString() inherited from the Object class. This
method provides a way to convert objects into Strings. Most classes override the default
behavior to provide more specific (and more useful) data in the returned String:

3 Chapter 21.3 on page 153

168
Using StringBuilder/StringBuffer to concatenate strings

Example: Console:
Code section 3.86: Concatenation of objects. Output for Code section 3.86
Age=31
1 [Link]("Age=" + new Integer(31));

23.3 Using StringBuilder/StringBuffer to concatenate


strings
Remember that String objects are immutable objects. Once a String is created, it can
not be modified, takes up memory until garbage collected. Be careful of writing a method
like this:

Example:
Code section 3.87: Raw concatenation.
1 public String convertToString(Collection<String> words) {
2 String str = "";
3 // Loops through every element in words collection
4 for (String word : words) {
5 str = str + word + " ";
6 }
7 return str;
8 }

On the + operation a new String object is created at each iteration. Suppose words
contains the elements ["Foo", "Bar", "Bam", "Baz"]. At runtime, the method creates
thirteen Strings:
1. ""
2. "Foo"
3. " "
4. "Foo "
5. "Foo Bar"
6. " "
7. "Foo Bar "
8. "Foo Bar Bam"
9. " "
10. "Foo Bar Bam "
11. "Foo Bar Bam Baz"
12. " "
13. "Foo Bar Bam Baz "
Even though only the last one is actually useful.
To avoid unnecessary memory use like this, use the StringBuilder class. It provides similar
functionality to Strings, but stores its data in a mutable way. Only one StringBuilder
object is created. Also because object creation is time consuming, using StringBuilder
produces much faster code.

169
API/[Link]

Example:
Code section 3.88: Concatenation with StringBuilder.
1 public String convertToString(Collection<String> words) {
2 StringBuilder buf = new StringBuilder();
3 // Loops through every element in words collection
4 for (String word : words) {
5 [Link](word);
6 [Link](" ");
7 }
8 return [Link]();
9 }

As StringBuilder isn't thread safe (see the chapter on Concurrency4 ) you can't use it in
more than one thread. For a multi-thread environment, use StringBuffer instead which
does the same and is thread safe. However, StringBuffer is slower so only use it when it
is required. Moreover, before Java 5 only StringBuffer existed.

23.4 Comparing Strings


Comparing strings is not as easy as it may first seem. Be aware of what you are doing when
comparing String's using ==:

Example: Console:
Code section 3.89: Dangerous comparison. Output for Code section 3.89
Match found.
1 String greeting = "Hello World!";
2 if (greeting == "Hello World!") {
3 [Link]("Match found.");
4 }

The difference between the above and below code is that the above code checks to see if
the String's are the same objects in memory which they are. This is as a result of the fact
that String's are stored in a place in memory called the String Constant Pool. If the new
keyword is not explicitly used when creating the String it checks to see if it already exists
in the Pool and uses the existing one. If it does not exist, a new Object is created. This is
what allows Strings to be immutable in Java. To test for equality, use the equals(Object)
method inherited by every class and defined by String to return true if and only if the
object passed in is a String contains the exact same data:

Example: Console:
Code section 3.90: Right comparison. Output for Code section 3.90
Match found.
1 String greeting = "Hello World!";
2 if ([Link]("Hello World!")) {
3 [Link]("Match found.");
4 }

4 [Link]

170
Splitting a String

Remember that the comparison is case sensitive.

Example: Console:
Code section 3.91: Comparison with lowercase. Output for Code section 3.91

1 String greeting = "Hello World!";


2 if ([Link]("hello world!")) {
3 [Link]("Match found.");
4 }

To order String objects, use the compareTo() method, which can be accessed wherever
we use a String datatype. The compareTo() method returns a negative, zero, or positive
number if the parameter is less than, equal to, or greater than the object on which it is
called. Let's take a look at an example:

Example:
Code section 3.92: Order.
1 String person1 = "Peter";
2 String person2 = "John";
3 if ([Link](person2) > 0) {
4 // Badly ordered
5 String temp = person1;
6 person1 = person2;
7 person2 = temp;
8 }

The code section 3.925 is comparing the String variable person1 to person2. If person1
is different even in the slightest manner, we will get a value above or below 0 depending on
the exact difference. The result is negative if this String object lexicographically precedes
the argument string. The result is positive if this String object lexicographically follows the
argument string. Take a look at the Java API6 for more details.

23.5 Splitting a String


Sometimes it is useful to split a string into separate strings, based on a regular expressions7 .
The String class has a split() method, since Java 1.4, that will return a String array:

5 Chapter 23.3 on page 169


[Link]
6
String%29
7 [Link]

171
API/[Link]

Example:
Code section 3.93: Order.
1 String person = "Brown, John:100 Yonge Street, Toronto:(416)777-9999";
2 ...
3 String[] personData = [Link](":");
4 ...
5 String name = personData[0];
6 String address = personData[1];
7 String phone = personData[2];

Another useful application could be to split the String text based on the new line character,
so you could process the text line by line.

23.6 Substrings
It may also be sometimes useful to create substrings, or strings using the order of letters
from an existing string. This can be done in two methods.
The first method involves creating a substring out of the characters of a string from a given
index to the end:

Example: Console:
Code section 3.94: Truncating string. Output for Code section 3.94
fee
1 String str = "coffee";
2 [Link]([Link](3));

The index of the first character in a string is 0.


c o f f e e
0 1 2 3 4 5

By counting from there, it is apparent that the character in index 3 is the second ”f” in
”coffee”. This is known as the beginIndex. All characters from the beginIndex until the
end of the string will be copied into the new substring.
The second method involves a user-defined beginIndex and endIndex:

Example: Console:
Code section 3.95: Extraction of string. Output for Code section 3.95
port
1 String str = "supporting";
2 [Link]([Link](3, 7));

The string returned by substring() would be ”port”.


s u p p o r t i n g
0 1 2 3 4 5 6 7 8 9

172
String cases

Please note that the endIndex is not inclusive. This means that the last character will be
of the index endIndex-1. Therefore, in this example, every character from index 3 to index
6, inclusive, was copied into the substring.

Note:
It is easy to mistake the method substring() for subString() (which does not exist
and would return with a syntax error on compilation). Substring is considered to be one
word. This is why the method name does not seem to follow the common syntax of
Java. Just remember that this style only applies to methods or other elements that are
made up of more than one word.

23.7 String cases


The String class also allows for the modification of cases. The two methods that make this
possible are toLowerCase() and toUpperCase().

Example: Console:
Code section 3.96: Case modification. Output for Code section 3.96
wikibooks
1 String str = "wIkIbOoKs"; WIKIBOOKS
2 [Link]([Link]());
3 [Link]([Link]());

These methods are useful to do a search which is not case sensitive:

Example:
Code section 3.97: Text search.
1 String word = "Integer";
2 String text = "A number without a decimal part is an integer."
3 + " Integers are a list of digits.";
4
5 ...
6
7 // Remove the case
8 String lowerCaseWord = [Link]();
9 String lowerCaseText = [Link]();
10
11 // Search
12 int index = [Link](lowerCaseWord);
13 while (index != -1) {
14 [Link](word
15 + " appears at column "
16 + (index + 1)
17 + ".");
18 index = [Link](lowerCaseWord, index + 1);
19 }
20

173
API/[Link]

Console:
Output for Code section 3.97
Integer appears at column 38.
Integer appears at column 47.

Question 3.12: You have mail addresses in the following form: <firstName>.<lastName>
@<companyName>.org
Write the String getDisplayName(String) method that receives the mail string as pa-
rameter and returns the readable person name like this: LASTNAME Firstname

Example:
Answer 3.12: getDisplayName()
1 public static String getDisplayName(String mail) {
2 String displayName = null;
3
4 if (mail != null) {
5 String[] mailParts = [Link]("@");
6 String namePart = mailParts[0];
7 String[] namesParts = [Link]("\\.");
8
9 // The last name
10 String lastName = namesParts[1];
11 lastName = [Link]();
12
13 // The first name
14 String firstName = namesParts[0];
15
16 String firstNameInitial = [Link](0, 1);
17 firstNameInitial = [Link]();
18
19 String firstNameEnd = [Link](1);
20 firstNameEnd = [Link]();
21
22 // Concatenation
23 StringBuilder displayNameBuilder = new StringBuilder(lastName).append("
24 ").append(firstNameInitial).append(firstNameEnd);
25 displayName = [Link]();
26 }
27
28 return displayName;
29 }

1. We only process non null strings,


2. We first split the mail into two parts to separate the personal information from the
company information and we keep the name data,
3. Then we split the name information to separate the first name from the last name.
As the split() method use regular expression and . is a wildcard character, we have
to escape it (\.). However, in a string, the \ is also a special character, so we need
to escape it too (\\.),
4. The last name is just capitalized,
5. As the case of all the first name characters will not be the same, we have to cut the
first name. Only the first name initial will be capitalized,
6. Now we can concatenate all the fragments. We prefer to use a StringBuilder to do
that.

174
See also

23.8 See also


• Java API: [Link].String8
• Java API: [Link].StringBuffer9
• Java API: [Link].StringBuilder10

8 [Link]
9 [Link]
10 [Link]

175
24 Classes, Objects and Types

An object is composed of fields and methods. The fields, also called data members,
characteristics, attributes, or properties, describe the state of the object. The methods
generally describe the actions associated with a particular object. Think of an object as a
noun, its fields as adjectives describing that noun, and its methods as the verbs that can
be performed by or on that noun.
For example, a sports car is an object. Some of its fields might be its height, weight,
acceleration, and speed. An object's fields just hold data about that object. Some of the
methods of the sports car could be ”drive”, ”park”, ”race”, etc. The methods really don't
mean much unless associated with the sports car, and the same goes for the fields.
The blueprint that lets us build our sports car object is called a class. A class doesn't tell us
how fast our sports car goes, or what color it is, but it does tell us that our sports car will
have a field representing speed and color, and that they will be say, a number and a word
(or hex color code), respectively. The class also lays out the methods for us, telling the car
how to park and drive, but these methods can't take any action with just the blueprint —
they need an object to have an effect.
In Java, a class is located in a file similar to its own name. If you want to have a class
called SportsCar, its source file needs to be [Link]. The class is created by
placing the following in the source file: Code listing 3.13: [Link]

1 public class SportsCar {


2 /* Insert your code here */
3}

The class doesn't do anything yet, as you will need to add methods and field variables first.
The objects are different from the primitive types because:
1. The primitive types are not instantiated.
2. In the memory, for a primitive type only its value is stored. For an object, also a
reference to an instance can be stored.
3. In the memory, the allocated space of a primitive type is fixed, whatever their value.
The allocated space of an object can vary, for instance either the object is instantiated
or not.
4. The primitive types don't have methods callable on them.
5. A primitive type can't be inherited.

177
Classes, Objects and Types

24.1 Instantiation and constructors


In order to get from class to object, we ”build” our object by instantiation. Instantiation
simply means to create an instance of a class. Instance and object are very similar terms
and are sometimes interchangeable, but remember that an instance refers to a specific object
, which was created from a class.
This instantiation is brought about by one of the class's methods, called a constructor. As
its name implies, a constructor builds the object based on the blueprint. Behind the scenes,
this means that computer memory is being allocated for the instance, and values are being
assigned to the data members.
In general there are four constructor types: default, non-default, copy, and cloning.
A default constructor will build the most basic instance. Generally, this means assigning
all the fields values like null, zero, or an empty string. Nothing would stop you, however,
from setting the color of your default sports car color to red, but this is generally bad
programming style. Another programmer would be confused if your basic car came out red
instead of say, colorless.

Example:
Code section 3.79: A default constructor.
1 SportsCar car = new SportsCar();

A non-default constructor is designed to create an object instance with prescribed values


for most, if not all, of the object's fields. The car is red, goes from 0-60 in 12 seconds, tops
out at 190mph, etc.

Example:
Code section 3.80: A non-default constructor.
1 SportsCar car = new SportsCar("red", 12, 190);

A copy constructor is not included in the Java language, however one can easily create a
constructor that does the same as a copy constructor. It's important to understand what it
is. As the name implies, a copy constructor creates a new instance to be a duplicate of an
already existing one. In Java, this can be also accomplished by creating the instance with
the default constructor, and then using the assignment operator to equivocate them. This
is not possible in all languages though, so just keep the terminology under your belt.
Java has the concept of cloning an object, and the end results are similar to the copy
constructor. Cloning an object is faster than creation with the new keyword, because all
the object memory is copied at once to the destination cloned object. This is possible
by implementing the Cloneable interface, which allows the method [Link]() to
perform a field-by-field copy.

178
Type

Example:
Code section 3.81: Cloning object.
1 SportsCar car = [Link]();

24.2 Type
When an object is created, a reference to the object is also created. The object can not be
accessed directly in Java, only through this object reference. This object reference has a
type assigned to it. We need this type when passing the object reference to a method as a
parameter. Java does strong type checking.
Type is basically a list of features/operations, that can be performed through that object
reference. The object reference type is basically a contract that guarantees that those
operations will be there at run time.
When a car is created, it comes with a list of features/operations listed in the user manual
that guarantees that those will be there when the car is used.
When you create an object from a class by default its type is the same as its class. It means
that all the features/operations the class defined are there and available, and can be used.
See below:

Example:
Code section 3.82: Default type.
1 (new ClassName()).operations();

You can assign this to a variable having the same type as the class:

Example:
Code section 3.83: A variable having the same type as the class.
1 ClassName objRefVariable = new ClassName();
2 [Link]();

You can assign the created object reference to the class, super class, or to an interface the
class implements:

Example:
Code section 3.84: Using the super class.
1 SuperClass objectRef = new ClassName(); // features/operations list are
defined
2 by the SuperClass class
3 ...
4 Interface inter = new ClassName(); // features/operations list are defined by
5 the interface

In the car analogy, the created car may have different Types of drivers. We create sepa-
rate user manuals for them, an Average user manual, a Power user manual, a Child user

179
Classes, Objects and Types

manual, or a Handicapped user manual. Each type of user manual describes only those
features/operations appropriate for the type of driver. For instance, the Power driver may
have additional gears to switch to higher speeds, that are not available to other type of
users...
When the car key is passed from an adult to a child we are replacing the user manuals, that
is called Type Casting.
In Java, casts can occur in three ways:
• up casting going up in the inheritance tree, until we reach the Object
• up casting to an interface the class implements
• down casting until we reach the class the object was created from

24.3 Autoboxing/unboxing
Autoboxing and unboxing, language features since Java 1.5, make the programmer's life
much easier when it comes to working with the primitive wrapper types. Consider this code
fragment:

Example:
Code section 3.85: Traditional object creation.
1 int age = 23;
2 Integer ageObject = new Integer(age);

Primitive wrapper objects were Java's way of allowing one to treat primitive data types
as though they were objects. Consequently, one was expected to wrap one's primitive data
type with the corresponding primitive wrapper object, as shown above.
Since Java 1.5, one may write as below and the compiler will automatically create the
wrap object. The extra step of wrapping the primitive is no longer required. It has been
automatically boxed up on your behalf:

Example:
Code section 3.86: Autoboxing.
1 int age = 23;
2 Integer ageObject = age;

Note:
Keep in mind that the compiler still creates the missing wrapper code, so one doesn't
really gain anything performance-wise. Consider this feature a programmer convenience,
not a performance booster.

Each primitive type has a class wrapper:

Primitive type Class wrapper


byte [Link]
char [Link]

180
Methods in the Object class

short [Link]
int [Link]
long [Link]
float [Link]
double [Link]
boolean [Link]
void [Link]

Unboxing uses the same process in reverse. Study the following code for a moment. The if
statement requires a boolean primitive value, yet it was given a Boolean wrapper object.
No problem! Java 1.5 will automatically unbox this.

Example:
Code section 3.87: Unboxing.
1 Boolean canMove = new Boolean(true);
2
3 if (canMove) {
4 [Link]("This code is legal in Java 1.5");
5 }

Question 3.11: Consider the following code:

Example:
Question 3.11: Autoboxing/unboxing.
1 Integer a = 10;
2 Integer b = a + 2;
3 [Link](b);

How many autoboxings and unboxings are there in this code?

Example:
Answer 3.11: Autoboxing/unboxing.
1 Integer a = 10;
2 Integer b = a + 2;
3 [Link](b);

3
• 1 autoboxing at line 1 to assign.
• 1 unboxing at line 2 to do the addition.
• 1 autoboxing at line 2 to assign.
• No autoboxing nor unboxing at line 3 as println() supports the Integer class as pa-
rameter.

24.4 Methods in the Object class


Methods in the [Link] class are inherited, and thus shared in common by all
classes.

181
Classes, Objects and Types

24.4.1 The clone method


The [Link]() method returns a new object that is a copy of the current
object. Classes must implement the marker interface [Link] to indicate
that they can be cloned.

24.4.2 The equals method


The [Link]([Link]) method compares the object to an-
other object and returns a boolean result indicating if the two objects are equal. Seman-
tically, this method compares the contents of the objects whereas the equality comparison
operator ”==” compares the object references. The equals method is used by many of the
data structure classes in the [Link] package. Some of these data structure classes also
rely on the [Link] method—see the hashCode method for details on the contract
between equals and hashCode. Implementing equals() isn't always as easy as it seems, see
'Secrets of equals()1 ' for more information.

24.4.3 The finalize method


The [Link]() method is called exactly once before the garbage col-
lector frees the memory for object. A class overrides finalize to perform any clean up
that must be performed before an object is reclaimed. Most objects do not need to override
finalize.
There is no guarantee when the finalize method will be called, or the order in which the
finalize method will be called for multiple objects. If the JVM exits without performing
garbage collection, the OS may free the objects, in which case the finalize method doesn't
get called.
The finalize method should always be declared protected to prevent other classes from
calling the finalize method.

protected void finalize() throws Throwable { ... }

24.4.4 The getClass method


The [Link]() method returns the [Link] object for the
class that was used to instantiate the object. The class object is the base class of reflection2
in Java. Additional reflection support is provided in the [Link] package.

24.4.5 The hashCode method


The [Link]() method returns an integer (int). This integer can
be used to distinguish objects although not completely. It quickly separates most of the

1 [Link]
2 [Link]

182
Methods in the Object class

objects and those with the same hash code are separated later in another way. It is used
by the classes that provide associative arrays, for instance, those that implement the
[Link] interface . They use the hash code to store the object in the associative
array. A good hashCode implementation will return a hash code:
• Stable: does not change
• Evenly distributed: the hash codes of unequal objects tend to be unequal and the hash
codes are evenly distributed across integer values.
The second point means that two different objects can have the same hash code so two
objects with the same hash code are not necessarily the same!
Since associative arrays depend on both the equals and hashCode methods, there is an
important contract between these two methods that must be maintained if the objects are
to be inserted into a Map:
For two objects a and b
• [Link](b) == [Link](a)
• if [Link](b) then [Link]() == [Link]()
• but if [Link]() == [Link]() then [Link](b)
In order to maintain this contract, a class that overrides the equals method must also
override the hashCode method, and vice versa, so that hashCode is based on the same
properties (or a subset of the properties) as equals.
A further contract that the map has with the object is that the results of the hashCode and
equals methods will not change once the object has been inserted into the map. For this
reason, it is generally a good practice to base the hash function on immutable properties of
the object.

24.4.6 The toString method


The [Link]() method returns a [Link] that contains a
text representation of the object. The toString method is implicitly called by the compiler
when an object operand is used with the string concatenation operators (+ and +=).

24.4.7 The wait and notify thread signaling methods


Every object has two wait lists for threads associated with it. One wait list is used by the
synchronized keyword to acquire the mutex lock associated with the object. If the mutex
lock is currently held by another thread, the current thread is added to the list of blocked
threads waiting on the mutex lock. The other wait list is used for signaling between threads
accomplished through the wait and notify and notifyAll methods.
Use of wait/notify allows efficient coordination of tasks between threads. When one thread
needs to wait for another thread to complete an operation, or needs to wait until an event
occurs, the thread can suspend its execution and wait to be notified when the event occurs.
This is in contrast to polling, where the thread repeatedly sleeps for a short period of time
and then checks a flag or other condition indicator. Polling is both more computationally

183
Classes, Objects and Types

expensive, as the thread has to continue checking, and less responsive since the thread won't
notice the condition has changed until the next time to check.

The wait methods


There are three overloaded versions of the wait method to support different ways to spec-
ify the timeout value: [Link](), [Link](long) and
[Link](long, int). The first method uses a timeout value of zero (0),
which means that the wait does not timeout; the second method takes the number of mil-
liseconds as a timeout; the third method takes the number of nanoseconds as a timeout,
calculated as 1000000 * timeout + nanos.
The thread calling wait is blocked (removed from the set of executable threads) and added
to the object's wait list. The thread remains in the object's wait list until one of three
events occurs:
1. another thread calls the object's notify or notifyAll method;
2. another thread calls the thread's [Link] method; or
3. a non-zero timeout that was specified in the call to wait expires.
The wait method must be called inside of a block or method synchronized on the object.
This insures that there are no race conditions between wait and notify. When the thread
is placed in the wait list, the thread releases the object's mutex lock. After the thread is
removed from the wait list and added to the set of executable threads, it must acquire the
object's mutex lock before continuing execution.

The notify and notifyAll methods


The [Link]() and [Link]() methods remove
one or more threads from an object's wait list and add them to the set of executable threads.
notify removes a single thread from the wait list, while notifyAll removes all threads from
the wait list. Which thread is removed by notify is unspecified and dependent on the JVM
implementation.
The notify methods must be called inside of a block or method synchronized on the object.
This insures that there are no race conditions between wait and notify.

184
25 Keywords

Keywords are special tokens in the language which have reserved use in the language.
Keywords may not be used as identifiers in Java — you cannot declare a field whose name
is a keyword, for instance.
Examples of keywords are the primitive types, int and boolean; the control flow statements
for and if; access modifiers such as public, and special words which mark the declaration
and definition of Java classes, packages, and interfaces: class, package, interface.
Below are all the Java language keywords:
• abstract
• assert (since Java 1.4)
• boolean
• break
• byte
• case
• catch
• char
• class
• const (not used)
• continue
• default
• do
• double
• else
• enum (since Java 5.0)
• extends
• final
• finally
• float
• for
• goto (not used)
• if
• implements
• import
• instanceof
• int
• interface
• long
• native
• new

185
Keywords

• package
• private
• protected
• public
• return
• short
• static
• strictfp (since Java 1.2)
• super
• switch
• synchronized
• this
• throw
• throws
• transient
• try
• void
• volatile
• while
In addition, the identifiers null, true, and false denote literal values and may not be used
to create identifiers.

25.1 abstract
abstract is a Java keyword. It can be applied to a class and methods. An abstract class
cannot be directly instantiated. It must be placed before the variable type or the method
return type. It is recommended to place it after the access modifier and after the static
keyword. A non-abstract class is a concrete class. An abstract class cannot be final.
Only an abstract class can have abstract methods. An abstract method is only declared,
not implemented: Code listing 1: [Link]

1 public abstract class AbstractClass {


2 // This method does not have a body; it is abstract.
3 public abstract void abstractMethod();
4
5 // This method does have a body; it is implemented in the abstract class
and
6 gives a default behavior.
7 public void concreteMethod() {
8 [Link](”Already coded.”);
9 }
10 }

An abstract method cannot be final, static nor native. Either you instantiate a concrete
sub-class, either you instantiate the abstract class by implementing its abstract methods
alongside a new statement:

186
assert

Example:
Code section 1: Abstract class use.
1 AbstractClass myInstance = new AbstractClass() {
2 public void abstractMethod() {
3 [Link]("Implementation.");
4 }
5 };

A private method cannot be abstract.

25.2 assert
assert is a Java keyword1 used to define an assert statement2 . An assert statement is
used to declare an expected boolean condition in a program. If the program is running
with assertions enabled, then the condition is checked at runtime. If the condition is false,
the Java runtime system throws3 an AssertionError4 Assertions may be declared using the
following syntax:
assert expression1 [: expression2];

expression1 is a boolean that will throw the assertion if it is false. When it is thrown, the
assertion error exception is created with the parameter expression2 (if applicable).
An example:
assert list != null && [Link]() > 0 : "list variable is null or empty";
Object value = [Link](0);

Assertions are usually used as a debugging aid. They should not be used instead of validating
arguments to public methods, or in place of a more precise runtime error exception.
Assertions are enabled with the Java -ea or -enableassertionsruntime option. See your
Java environment documentation for additional options for controlling assertions.

25.3 boolean
boolean is a keyword5 which designates the boolean primitive type6 . There are only two
possible boolean values: true and false. The default value for boolean fields is false.
The following is a declaration of a private boolean field named initialized, and its use
in a method named synchronizeConnection.

1 Chapter 24.4.7 on page 184


2 [Link]
3 [Link]
4 [Link]
5 Chapter 24.4.7 on page 184
6 Chapter 18.8 on page 135

187
Keywords

Example:
Code section 1: Connection synchronization.
1 private boolean initialized = false;
2
3 public void synchronizeConnection() {
4 if (!initialized) {
5 connection = connect();
6 initialized = true;
7 }
8 }

The previous code only creates a connection once (at the first method call). Note that there
is no automatic conversion between integer types (such as int) to boolean as is possible in
some languages like C7 . Instead, one must use an equivalent expression such as (i != 0)
which evaluates to true if i is not zero.

25.4 break
break is a Java keyword.
Jumps (breaks) out from a loop. Also used at switch statement.
For example:

for ( int i=0; i < maxLoopIter; i++ ) {


[Link]("Iter=" +i);
if ( i == 5 ) {
break; // -- 5 iteration is enough --
}
}

See also:
• Java Programming/Keywords/switch8

25.5 byte
byte is a keyword9 which designates the 8 bit signed integer primitive type10 .
The [Link] class is the nominal wrapper class when you need to store a byte
value but an object reference is required.
Syntax: byte <variable-name> = <integer-value>;
For example:

7 [Link]
8 Chapter 25.48 on page 214
9 Chapter 24.4.7 on page 184
10 Chapter 18.8 on page 135

188
case

byte b = 65;

or

byte b = 'A';

The number 65 is the code for 'A' in ASCII11 .


See also:
• Java Programming/Primitive Types12

25.6 case
case is a Java keyword.
This is part of the switch statement, to find if the value passed to the switch statement
matches a value followed by case.
For example:

int i = 3;
switch(i) {
case 1:
[Link]("The number is 1.");
break;
case 2:
[Link]("The number is 2.");
break;
case 3:
[Link]("The number is 3."); // this line will print
break;
case 4:
[Link]("The number is 4.");
break;
case 5:
[Link]("The number is 5.");
break;
default:
[Link]("The number is not 1, 2, 3, 4, or 5.");
}

25.7 catch
catch is a keyword.

11 [Link]
12 Chapter 18.8 on page 135

189
Keywords

It's part of a try block. If an exception is thrown inside a try block, the exception will be
compared to any of the catch part of the block. If the exception match with one of the
exception in the catch part, the exception will be handled there.
For example:

try {
//...
throw new MyException_1();
//...
} catch ( MyException_1 e ) {
// --- Handle the Exception_1 here --
} catch ( MyException_2 e ) {
// --- Handle the Exception_2 here --
}

See also:
• Java Programming/Keywords/try13

25.8 char
char is a keyword. It defines a character primitive type14 . char can be created from
character literals and numeric representation. Character literals consist of a single quote
character (') (ASCII 39, hex 0x27), a single character, and a close quote ('), such as 'w'
. Instead of a character, you can also use unicode escape sequences15 , but there must be
exactly one.
Syntax:

char variable name1 = 'character1 ';

Output for Code section 1


A
Example: A
Code section 1: Three examples. A

1 char oneChar1 = 'A';


2 char oneChar2 = 65;
3 char oneChar3 = '\u0041';
4 [Link](oneChar1);
5 [Link](oneChar2);
6 [Link](oneChar3);

13 Chapter 25.56 on page 220


14 Chapter 18.8 on page 135
[Link]
15
20Sequences

190
class

65 is the numeric representation of character 'A' , or its ASCII16 code.


The nominal wrapper class is the [Link] class when you need to store a
char value but an object reference is required.

Example:
Code section 2: char wrapping.
1 char aCharPrimitiveType = 'A';
2 Character aCharacterObject = aCharPrimitiveType;

See also:
• Java Programming/Primitive Types17

25.9 class
class is a Java keyword which begins the declaration and definition of a class18 .
The general syntax of a class declaration, using Extended Backus-Naur Form, is

class-declaration ::= [access-modifiers] class identifier


[extends-clause] [implements-clause]
class-body

extends-clause ::= extends class-name


implements-clause ::= implements interface-names
interface-names ::= interface-name [, interface-names]
class-body ::= { [member-declarations] }
member-declarations = member-declaration [member-declarations]
member-declaration = field-declaration
| initializer
| constructor
| method-declaration
| class-declaration

The extends word is optional. If omitted, the class extends the Object class, as all Java
classes inherit from it.
See also:
• Java Programming/Keywords/new19

25.10 const
const is a reserved keyword, presently not being used.

16 [Link]
17 Chapter 18.8 on page 135
18 Chapter 33 on page 260
19 Chapter 25.37 on page 207

191
Keywords

In other programming languages, such as C, const is often used to declare a constant.


However, in Java, final is used instead.

25.11 continue
continue is a Java keyword. It skips the remainder of the loop and continues with the next
iteration.
For example:

int maxLoopIter = 7;

for (int i = 0; i < maxLoopIter; i++ ) {


if (i == 5) {
continue; // -- 5 iteration is skipped --
}
[Link]("Iteration = " + i);
}

results in

0
1
2
3
4
6
7

25.12 See also


• Java Programming/Statements20

25.13 default
default is a Java keyword.
This is an optional part of the switch statement, which only executes if none of the above
cases are matched.
See also:
• Java Programming/Keywords/switch21

20 Chapter 13.1 on page 92


21 Chapter 25.48 on page 214

192
do

25.14 do
do is a Java keyword.
It starts a do-while looping block. The do-while loop is functionally similar to the while
loop, except the condition is evaluated after the statements execute
Syntax:
do {
//statements;
} while (condition);

For example:
do {
i++;
} while ( i < maxLoopIter );

See also:
• Java Programming/Statements22
• Java Programming/Keywords/for23
• Java Programming/Keywords/while24

25.15 double
double is a keyword25 which designates the 64 bit float primitive type26 .
The [Link] class is the nominal wrapper class when you need to store a double
value but an object reference is required.
Syntax:

double <variable-name> = <float-value>;

For example:

double d = 65.55;

See also:
• Java Programming/Primitive Types27

22 Chapter 13.1 on page 92


23 Chapter 25.26 on page 201
24 Chapter 25.59 on page 223
25 Chapter 24.4.7 on page 184
26 Chapter 18.8 on page 135
27 Chapter 18.8 on page 135

193
Keywords

25.16 else
else is a Java keyword. It is an optional part of a branching statement. It starts the 'false'
statement block.
The general syntax of a if, using Extended Backus-Naur Form, is

branching-statement ::= if condition-clause


single-statement | block-statement
[ else
single-statement | block-statement ]

condition-clause ::= ( Boolean Expression )


single-statement ::= Statement28
block-statement ::= { Statement29 [ Statement30 ] }

For example:

if ( expression ) {
[Link]("'True' statement block");
} else {
[Link]("'False' statement block");
}

See also:
• Java Programming/Keywords/if31

25.17 enum

/** Grades of courses */


enum Grade { A, B, C, D, F };
// ...
private Grade gradeA = Grade.A;

This enumeration constant then can be passed in to methods:

[Link](gradeA);
/**
* Assigns the grade for this course to the student
* @param GRADE Grade to be assigned
*/
public void assignGrade(final Grade GRADE) {
grade = GRADE;
}

An enumeration may also have parameters:

31 Chapter 25.28 on page 202

194
extends

public enum DayOfWeek {


/** Enumeration constants */
MONDAY(1), TUESDAY(2), WEDNESDAY(3), THURSDAY(4), FRIDAY(5),
SATURDAY(6), SUNDAY(0);

/** Code for the days of the week */


private byte dayCode = 0;

/**
* Private constructor
* @param VALUE Value that stands for a day of the week.
*/
private DayOfWeek(final byte VALUE) {
dayCode = [Link](VALUE%7);
}

/**
* Gets the day code
* @return The day code
*/
public byte getDayCode() {
return dayCode;
}
}

It is also possible to let an enumeration implement interfaces other than


[Link] and [Link], which are already implicitly imple-
mented by each enumeration:

public enum DayOfWeek implements Runnable {


MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;

/**
* Run method prints all elements
*/
public void run() {
[Link]("name() = " + name() +
", toString() = \"" + toString() + "\"");
}
}

25.18 extends
extends is a Java keyword.
Used in class and interface definition to declare the class or interface that is to be extended.
Syntax:

public class MyClass extends SuperClass


{
//...
}

public interface MyInterface extends SuperInterface


{

195
Keywords

//...
}

In Java 1.5 and later, the ”extends” keyword is also used to specify an upper bound on a
type parameter in Generics.

class Foo<T extends Number> { /*...*/ }

See also:
• Java Programming/Creating Objects32
• Java Programming/Keywords/class33

25.19 final
final is a keyword. Beware! It has distinct meanings depending whether it is used for a
class, a method, or for a variable. It must be placed before the variable type or the method
return type. It is recommended to place it after the access modifier and after the static
keyword.

Example:
Code section 1: Keyword order.
1 private static final long serialVersionUID = -5437975414336623381L;

25.20 For a variable


The final keyword only allows a single assignment for the variable. That is to say, once
the variable has been assigned, its value is in read-only. If the variable is a primitive type,
its value will no longer change. If it is an object, only its reference will no longer change.
Keep in mind that its value can still be changed.

1 Warning
Code section 2: Forbidden double assignment.
1 final int a = 1;
2 a = 2;

32 [Link]
33 Chapter 25.9 on page 191

196
For a class

Example: Console:
Code section 3: Only modify the value of the object. Console for Code section 3
0
1 final ArrayList list = new ArrayList(); 1
2 [Link]([Link]());
3 [Link]("One item");
4 [Link]([Link]());

A final variable is often used for universal constants, such as pi34 :

Example:
Code section 4: Pi constant.
1 static final double PI = 3.1415926;

The final keyword can also be used for method parameters:

Example:
Code section 5: Final method parameter.
1 public int method(final int inputInteger) {
2 int outputInteger = inputInteger + 1;
3 return outputInteger;
4 }

It is useful for methods that use side effects to update some objects. Such methods modify
the content of an object passed in parameter. The method caller will receive the object
update. This will fail if the object parameter has been reassigned in the method. Another
object will be updated instead. Final method parameter can also be used to keep the code
clean.
The final keyword is similar to const in other languages and the readonly keyword in
C#35 . A final variable cannot be volatile.

25.21 For a class


The final keyword forbids the creation of a subclass. It is the case of the Integer or
String class.
Code listing 1: [Link]

1 public final class SealedClass {


2 public static void main(String[] args) {
3 }
4 }

34 [Link]
35 [Link]

197
Keywords

A final class cannot be abstract. The final keyword is similar to sealed keyword in
C#36 .

25.22 For a method


The final keyword forbids to overwrite the method in a subclass. It is useless if the class is
already final and a private method is implicitly final. A final method cannot be abstract.
Code listing 2: [Link]

1 public class NoOverwriting {


2 public final void sealedMethod() {
3 }
4 }

25.23 Interest
The final keyword is mostly used to guarantee a good usage of the code. For instance
(non-static) methods, this allows the compiler to expand the method (similar to an inline
function) if the method is small enough. Sometimes it is required to use it. For instance, a
nested class can only access the members of the top-level class if they are final.
See also Access Modifiers37 .

25.24 finally
finally is a keyword which is an optional ending part of the try block.

Example:
Code section 1: try block.
1 try {
2 // ...
3 } catch (MyException1 e) {
4 // Handle the Exception1 here
5 } catch (MyException2 e) {
6 // Handle the Exception2 here
7 } finally {
8 // This will always be executed no matter what happens
9 }

The code inside the finally block will always be executed. This is also true for cases when
there is an exception or even executed return statement in the try block.
Three things can happen in a try block. First, no exception is thrown:

36 [Link]
37 Chapter 39.5 on page 292

198
finally

Example: Console:
Code section 2: No exception is thrown. Console for Code section 2
Before the try block
1 [Link]("Before the try block"); Inside the try block
2 try { Execute the finally block
3 [Link]("Inside the try block"); Continue
4 } catch (MyException1 e) {
5 [Link]("Handle the Exception1");
6 } catch (MyException2 e) {
7 [Link]("Handle the Exception2");
8 } finally {
9 [Link]("Execute the finally block");
10 }
11 [Link]("Continue");

You can see that we have passed in the try block, then we have executed the finally block
and we have continued the execution. Now, a caught exception is thrown: |

Example:
Code section 3: A caught exception is thrown.
1 [Link]("Before the try block");
2 try {
3 [Link]("Enter inside the try block");
4 throw new MyException1();
5 [Link]("Terminate the try block");
6 } catch (MyException1 e) {
7 [Link]("Handle the Exception1");
8 } catch (MyException2 e) {
9 [Link]("Handle the Exception2");
10 } finally {
11 [Link]("Execute the finally block");
12 }
13 [Link]("Continue");

Console:
Console for Code section 3
Before the try block
Enter inside the try block
Handle the Exception1
Execute the finally block
Continue

We have passed in the try block until where the exception occurred, then we have executed
the matching catch block, the finally block and we have continued the execution. Now,
an uncaught exception is thrown:

199
Keywords

Example:
Code section 4: An uncaught exception is thrown.
1 [Link]("Before the try block");
2 try {
3 [Link]("Enter inside the try block");
4 throw new Exception();
5 [Link]("Terminate the try block");
6 } catch (MyException1 e) {
7 [Link]("Handle the Exception1");
8 } catch (MyException2 e) {
9 [Link]("Handle the Exception2");
10 } finally {
11 [Link]("Execute the finally block");
12 }
13 [Link]("Continue");

Console:
Console for Code section 4
Before the try block
Enter inside the try block
Execute the finally block

We have passed in the try block until where the exception occurred and we have executed
the finally block. NO CODE after the try-catch block has been executed. If there is an
exception that happens before the try-catch block, the finally block is not executed.
If return statement is used inside finally, it overrides the return statement in the try-catch
block. For instance, the construct

Example:
Code section 5: Return statement.
1 try {
2 return 11;
3 } finally {
4 return 12;
5 }

will return 12, not 11. Professional code almost never contains statements that alter exe-
cution order (like return, break, continue) inside the finally block, as such code is more
difficult to read and maintain.

25.25 float
float is a keyword38 which designates the 32 bit float primitive type39 .
The [Link] class is the nominal wrapper class when you need to store a float
value but an object reference is required.

38 Chapter 24.4.7 on page 184


39 Chapter 18.8 on page 135

200
for

Syntax:

float <variable-name> = <float-value>;

For example:

float price = 49.95;

See also:
• Java Programming/Primitive Types40

25.26 for
for is a Java keyword.
It starts a looping block.
The general syntax of a for, using Extended Backus-Naur Form, is

for-looping-statement ::= for condition-clause


single-statement | block-statement

condition-clause ::= ( before-statement; Boolean Expression ; after-statement )


single-statement ::= Statement41
block-statement ::= { Statement42 [ Statement43 ] }

For example:

for ( int i=0; i < maxLoopIter; i++ ) {


[Link]("Iter: " +i);
}

See also:
• Java Programming/Keywords/while44
• Java Programming/Keywords/do45

25.27 goto
goto is a reserved keyword, presently not being used.

40 Chapter 18.8 on page 135


44 Chapter 25.59 on page 223
45 Chapter 25.14 on page 193

201
Keywords

25.28 if
if is a Java keyword. It starts a branching statement.
The general syntax of a if, using Extended Backus-Naur Form, is

branching-statement ::= if condition-clause


single-statement | block-statement
[ else
single-statement | block-statement ]

condition-clause ::= ( Boolean Expression )


single-statement ::= Statement46
block-statement ::= { Statement47 [ Statements48 ] }

For example:

if ( boolean Expression )
{
[Link]("'True' statement block");
}
else
{
[Link]("'False' statement block");
}

See also:
• Java Programming/Keywords/else49

25.29 implements
implements is a Java keyword.
Used in class definition to declare the Interfaces that are to be implemented by the class.
Syntax:

public class MyClass implements MyInterface1, MyInterface2


{
...
}

See also:
• Java Programming/Creating Objects50
• Java Programming/Keywords/class51
• Java Programming/Keywords/interface52

49 Chapter 25.16 on page 194


50 [Link]
51 Chapter 25.9 on page 191
52 Chapter 25.33 on page 206

202
import

25.30 import
import is a Java keyword.
It declares a Java class to use in the code below the import statement. Once a Java class is
declared, then the class name can be used in the code without specifying the package the
class belongs to.
Use the '*' character to declare all the classes belonging to the package.
Syntax:

import [Link];
import package.*;

The static import construct allows unqualified access to static members without inheriting
from the type containing the static members:

import static [Link];

Once the static members have been imported, they may be used without qualification:

double r = cos(PI * theta);

Caveat: use static import very sparingly to avoid polluting the program's namespace!
See also:
• Java Programming/Packages53

25.31 instanceof
instanceof is a keyword.
It checks if an object reference is an instance of a type, and returns a boolean value;
The <object-reference> instanceof Object will return true for all non-null object refer-
ences, since all Java objects are inherited from Object. instanceof will always return
false if <object-reference> is null.
Syntax:

<object-reference> instanceof TypeName

For example:

53 Chapter 25.59 on page 223

203
Keywords

class Fruit
{
//...
}
class Apple extends Fruit
{
//...
}
class Orange extends Fruit
{
//...
}
public class Test
{
public static void main(String[] args)
{
Collection<Object> coll = new ArrayList<Object>();

Apple app1 = new Apple();


Apple app2 = new Apple();
[Link](app1);
[Link](app2);

Orange or1 = new Orange();


Orange or2 = new Orange();
[Link](or1);
[Link](or2);

printColl(coll);
}

private static String printColl( Collection<?> coll )


{
for (Object obj : coll)
{
if ( obj instanceof Object )
{
[Link]("It is a Java Object and");
}
if ( obj instanceof Fruit )
{
[Link]("It is a Fruit and");
}
if ( obj instanceof Apple )
{
[Link]("it is an Apple");
}
if ( obj instanceof Orange )
{
[Link]("it is an Orange");
}
}
}
}

Run the program:

java Test

The output:

204
int

It is a Java Object and It is a Fruit and it is an Apple


It is a Java Object and It is a Fruit and it is an Apple
It is a Java Object and It is a Fruit and it is an Orange
It is a Java Object and It is a Fruit and it is an Orange

Note that the instanceof operator can also be applied to interfaces. For example, if the
example above was enhanced with the interface

interface Edible
{
//...
}

and the classes modified such that they implemented this interface

class Orange extends Fruit implements Edible


{
...
}

we could ask if our object were edible.

if ( obj instanceof Edible )


{
[Link]("it is edible");
}

25.32 int
int is a keyword54 which designates the 32 bit signed integer primitive type55 .
The [Link] class is the nominal wrapper class when you need to store an int
value but an object reference is required.
Syntax:

int <variable-name> = <integer-value>;

For example:

int i = 65;

See also:

54 Chapter 24.4.7 on page 184


55 Chapter 18.8 on page 135

205
Keywords

• Java Programming/Primitive Types56

25.33 interface
interface is a Java keyword. It starts the declaration of a Java Interface.
For example:

public interface SampleInterface


{
public void method1();
//...
}

See also:
• Java Programming/Keywords/new57

25.34 long
long is a keyword58 which designates the 64 bit signed integer primitive type59 .
The [Link] class is the nominal wrapper class when you need to store a long
value but an object reference is required.
Syntax:

long <variable-name> = <integer-value>;

For example:

long timestamp = 1269898201;

See also:
• Java Programming/Primitive Types60

56 Chapter 18.8 on page 135


57 Chapter 25.37 on page 207
58 Chapter 24.4.7 on page 184
59 Chapter 18.8 on page 135
60 Chapter 18.8 on page 135

206
native

25.35 native
native is a java keyword. It marks a method, that it will be implemented in other languages,
not in Java. The method is declared without a body and cannot be abstract. It works
together with JNI (Java Native Interface)61 .
Syntax:

[public|protected|private] native method();

Native methods were used in the past to write performance critical sections but with java
getting faster this is now less common. Native methods are currently needed when
• You need to call from java a library, written in another language.
• You need to access system or hardware resources that are only reachable from the other
language (typically C62 ). Actually, many system functions that interact with real com-
puter (disk and network IO, for instance) can only do this because they call native code.
To complete writing native method, you need to process your class with javah tool that will
generate a header code in C. You then need to provide implementation of the header code,
produce dynamically loadable library (.so under Linux, .dll under Windows) and load it
(in the simplest case with [Link](library_file_name) . The code completion is
trivial if only primitive types like integers are passed but gets more complex if it is needed
to exchange strings or objects from the C code. In general, everything can be on C level,
including creation of the new objects and calling back methods, written in java.
To call the code in some other language (including C++63 ), you need to write a bridge from
C to that language. This is usually trivial as most of languages are callable from C.

25.36 See also


• [Link]
- JNI programming tutorial.
• [Link] - JNI spec-
ification.

25.37 new
new is a Java keyword. It creates a Java object and allocates memory for it on the heap.
new is also used for array creation, as arrays are also objects.
Syntax:

61 [Link]
62 [Link]
63 [Link]

207
Keywords

<JavaType> <variable> = new <JavaObject>();

For example:

LinkedList list = new LinkedList();


int[] intArray = new int[10];
String[][] stringMatrix = new String[5][10];

See also:
• Java Programming/Creating Objects64

25.38 package
package is a Java keyword. It declares a 'name space' for the Java class. It must be put
at the top of the Java file, it should be the first Java statement line.
To ensure that the package name will be unique across vendors, usually the company url is
used starting in backword.
Syntax:

package package;

For example:

package [Link];

See also:
• Java Programming/Packages65
• Java Programming/Keywords/import66

25.39 private
private is a Java keyword67 which declares a member's access68 as private. That is, the
member is only visible within the class, not from any other class (including subclasses).
The visibility of private members extends to nested classes69 .
Please note: Because access modifiers are not handled at instance level but at class level,
private members of an object are visible from other instances of the same class!

64 [Link]
65 Chapter 25.59 on page 223
66 Chapter 25.30 on page 203
67 Chapter 24.4.7 on page 184
68 [Link]
69 Chapter 40.4 on page 297

208
protected

Syntax:

private void method();

See also:
• Java Programming/Access Modifiers70

25.40 protected
protected is a Java keyword.
This keyword is an access modifier, used before a method or other class member to signify
that the method or variable can only be accessed by elements residing in its own class or
classes in the same package (as it would be for the default visibility level) but moreover
from subclasses of its own class, including subclasses in foreign packages (if the access is
made on an expression, whose type is the type of this subclass).
Syntax:

protected <returnType> <methodName>(<parameters>);

For example:

protected int getAge();

protected void setYearOfBirth(int year);

See also:
• Java Programming/Scope#Access modifiers71

25.41 public
public is a Java keyword72 which declares a member's access73 as public. Public members
are visible to all other classes. This means that any other class can access a public field
or method. Further, other classes can modify public fields unless the field is declared as
final.
A best practice is to give fields private access and reserve public access to only the
set of methods and final fields that define the class' public constants. This helps with

70 [Link]
71 Chapter 40.2 on page 294
72 Chapter 24.4.7 on page 184
73 Chapter 39.5 on page 292

209
Keywords

encapsulation and information hiding, since it allows you to change the implementation of
a class without affecting the consumers who use only the public API of the class.
Below is an example of an immutable public class named Length which maintains private
instance fields named units and magnitude but provides a public constructor and two
public accessor methods.
Code listing: [Link]

1 package [Link];
2
3 public class Length {
4 private double magnitude;
5 private String units;
6
7 public Length(double magnitude, String units) {
8 if ((units == null) || ([Link]().length() == 0)) {
9 throw new IllegalArgumentException(”non-null, non-empty units
10 required.”);
11 }
12
13 [Link] = magnitude;
14 [Link] = units;
15 }
16
17 public double getMagnitude() {
18 return [Link];
19 }
20
21 public String getUnits() {
22 return [Link];
23 }
24 }

25.42 return
return is a Java keyword.
Returns a primitive value, or an object reference, or nothing(void). It does not return object
values, only object references.
Syntax:

return variable; // --- Returns variable


or
return; // --- Returns nothing

25.43 short
short is a keyword. It defines a 16 bit signed integer primitive type.74

74 Chapter 18.8 on page 135

210
static

Syntax:

short <variable-name> = <integer-value>;

For example:

short age = 65;

See also:
• Java Programming/Primitive Types75

25.44 static
static is a Java keyword. It can be applied to a field, a method or an inner class76 . A static
field, method or class has a single instance for the whole class that defines it, even if there
is no instance of this class in the program. For instance, a Java entry point (main()) has
to be static. A static method cannot be abstract. It must be placed before the variable
type or the method return type. It is recommended to place it after the access modifier and
before the final keyword:

Example:
Code section 1: Static field and method.
1 public static final double PI = 3.1415926535;
2
3 public static void main(final String[] arguments) {
4 //…
5 }

The static items can be called on an instantiated object or directly on the class:

Example:
Code section 2: Static item calls.
1 double aNumber = [Link];
2 [Link](new String[0]);

Static methods cannot call nonstatic methods. The this current object reference is also not
available in static methods.

75 Chapter 18.8 on page 135


76 Chapter 40.4 on page 297

211
Keywords

25.45 Interest
• Static variables can be used as data sharing amongst objects of the same class. For
example to implement a counter that stores the number of objects created at a given
time can be defined as so:
Code listing 1: [Link]

1 public CountedObject {
2 private static int counter;
3 …
4 public AClass() {
5 …
6 counter++;
7 }
8 …
9 public int getNumberOfObjectsCreated() {
10 return counter;
11 }
12 }

The counter variable is incremented each time an object is created.


Public static variable should not be used, as these become global variables that can be
accessed from everywhere in the program. Global constants can be used, however. See
below:

Example:
Code section 3: Constant definition.
1 public static final String CONSTANT_VAR = "Const";

• Static methods can be used for utility functions or for functions that do not belong to
any particular object. For example:
Code listing 2: [Link]

1 public ArithmeticToolbox {
2 …
3 public static int addTwoNumbers(final int firstNumber, final int
4 secondNumber) {
5 return firstNumber + secondNumber;
6 }
7}

See also Static methods77

25.46 strictfp
strictfp is a java keyword, since Java 1.2 .

77 Chapter 22.5 on page 164

212
super

It makes sure that floating point calculations result precisely the same regardless of the un-
derlying operating system and hardware platform, even if more precision could be obtained.
This is compatible with the earlier version of Java 1.1 . If you need that use it.
Syntax for classes:

public strictfp class MyClass


{
//...
}

Syntax for methods:

public strictfp void method()


{
...
}

See also:
• [Link]

25.47 super
super is a keyword.
• It is used inside a sub-class method definition to call a method defined in the super class.
Private methods of the super-class cannot be called. Only public and protected methods
can be called by the super keyword.
• It is also used by class constructors to invoke constructors of its parent class78 .
• Super keyword are not used in static Method.
Syntax:

super.<method-name>([zero or more arguments]);

or:

super([zero or more arguments]);

For example: Code listing 1: [Link]

1 public class SuperClass {


2 public void printHello() {
3 [Link](”Hello from SuperClass”);

78 [Link]

213
Keywords

4 return;
5 }
6}

Code listing 2: [Link]

1 public class SubClass extends SuperClass {


2 public void printHello() {
3 [Link]();
4 [Link](”Hello from SubClass”);
5 return;
6 }
7 public static main(String[] args) {
8 SubClass obj = new SubClass();
9 [Link]();
10 }
11 }

Running the above program: Command for Code listing 2

$Java SubClass

Output of Code listing 2

Hello from SuperClass


Hello from SubClass

In Java 1.5 and later, the ”super” keyword is also used to specify a lower bound on a wildcard
type parameter in Generics.

Example:
Code section 1: A lower bound on a wildcard type parameter.
1 public void sort(Comparator<? super T> comp) {
2 ...
3 }

See also:
• extends

25.48 switch
switch is a Java keyword.
It is a branching operation, based on a number. The 'number' must be either char, byte,
short, or int primitive type.
Syntax:

214
switch

switch ( <integer-var> )
{
case <label1 >: <statements>;
case <label2 >: <statements>;
...
case <labeln >: <statements>;
default: <statements>;
}

When the <integer-var> value match one of the , then: The statements after the matched
label will be executed including the following label's statements, until the end of the switch
block, or until a break keyword is reached.
For example:

int var = 3;
switch ( var )
{
case 1:
[Link]( "Case: 1" );
[Link]( "Execute until break" );
break;
case 2:
[Link]( "Case: 2" );
[Link]( "Execute until break" );
break;
case 3:
[Link]( "Case: 3" );
[Link]( "Execute until break" );
break;
case 4:
[Link]( "Case: 4" );
[Link]( "Execute until break" );
break;
default:
[Link]( "Case: default" );
[Link]( "Execute until break" );
break;
}

The output from the above code is:

Case: 3
Execute until break

The same code can be written with if-else blocks”:

int var = 3;
if ( var == 1 ) {
[Link]( "Case: 1" );
[Link]( "Execute until break" );
} else if ( var == 2 ) {
[Link]( "Case: 2" );
[Link]( "Execute until break" );
} else if ( var == 3 ) {
[Link]( "Case: 3" );
[Link]( "Execute until break" );
} else if ( var == 4 ) {

215
Keywords

[Link]( "Case: 4" );


[Link]( "Execute until break" );
} else {
// -- This is the default part --
[Link]( "Case: default" );
[Link]( "Execute until break" );
}

See also:
• Java Programming/Keywords/if79

25.49 synchronized
synchronized is a keyword.
It marks a critical section. A critical section is where one and only one thread is executing.
So to enter into the marked code the threads are synchronized, only one can enter, the
others have to wait. For more information see Synchronizing Threads Methods80 or http:
//[Link]/docs/books/tutorial/essential/concurrency/[Link].
The synchronized keyword can be used in two ways:
• Create a synchronized block
• Mark a method synchronized
A synchronized block is marked as:

Example:
Code section 1: Synchronized block.
1 synchronized(<object_reference>) {
2 // [Link]() has a lock on object_reference. All other threads
3 trying to access it will
4 // be blocked until the current thread releases the lock.
5 }

The syntax to mark a method synchronized is:

Example:
Code section 2: Synchronized method.
1 public synchronized void method() {
2 // [Link]() has a lock on this object, i.e. a synchronized
3 method is the same as
4 // calling { synchronized(this) {…} }.
5 }

The synchronization is always associated to an object. If the method is static, the associated
object is the class. If the method is non-static, the associated object is the instance. While

79 Chapter 25.28 on page 202


80 [Link]

216
Singleton example

it is allowed to declare an abstract method as synchronized, it is meaningless to do so


since synchronization is an aspect of the implementation, not the declaration, and abstract
methods do not have an implementation.

25.50 Singleton example


As an example, we can show a thread-safe version of a singleton:
Code listing 1: [Link]

1 /**
2 * The singleton class that can be instantiated only once with lazy
3 instantiation
4 */
5 public class Singleton {
6 /** Static class instance */
7 private volatile static Singleton instance = null;
8
9 /**
10 * Standard private constructor
11 */
12 private Singleton() {
13 // Some initialisation
14 }
15
16 /**
17 * Getter of the singleton instance
18 * @return The only instance
19 */
20 public static Singleton getInstance() {
21 if (instance == null) {
22 // If the instance does not exist, go in time-consuming
23 // section:
24 synchronized ([Link]) {
25 if (instance == null) {
26 instance = new Singleton();
27 }
28 }
29 }
30
31 return instance;
32 }
33 }

25.51 this
this is a Java keyword. It contains the current object reference.
1. Solves ambiguity between instance variables and parameters .
2. Used to pass current object as a parameter to another method .
Syntax:

[Link]();
or

217
Keywords

[Link];

Example #1 for case 1:

public class MyClass


{
//...
private String value;
//...
public void setMemberVar( String value )
{
[Link]= value;
}
}

Example #2 for case 1:

public class MyClass


{
MyClass(int a, int b) {
[Link]("int a: " + a);
[Link]("int b: " + b);
}
MyClass(int a) {
this(a, 0);
}
//...
public static void main(String[] args) {
new MyClass(1, 2);
new MyClass(5);
}
}

25.52 throw
throw is a keyword; it 'throws' an exception. In a throw statement, the three types of
objects that can be thrown are: Exception, java:Throwable, and java:Error
Syntax:

throw <Exception Ref>;

For example:

public Customer findCustomer( String name ) throws


'''CustomerNotFoundException'''
{
Customer custRet = null;

Iterator iter = _customerList.iterator();


while ( [Link]() )
{

218
See also

Customer cust = (Customer) [Link]();


if ( [Link]().equals( name ) )
{
// --- Customer find --
custRet = cust;
break;
}
}
if ( custRet == null )
{
// --- Customer not found ---
throw new '''CustomerNotFoundException'''( "Customer "+ name + "
was not found" );
}

return custRet
}

25.53 See also


• Java Programming/Keywords/throws81

25.54 throws
throws is a Java keyword. It is used in a method definition to declare the Exceptions to
be thrown by the method.
Syntax:

public myMethod() throws MyException1, MyException2


{MyException1
...
}

Example:

class MyDefinedException extends Exception


{
public MyDefinedException(String str)
{
super(str);
}
}

public class MyClass


{
public static void showMyName(String str) throws MyDefinedException
{
if([Link]("What is your Name?"))
throw new MyDefinedException("My name is Blah Blah");
}

81 Chapter 25.54 on page 219

219
Keywords

public static void main(String a[])


{
try
{
showMyName("What is your Name?");
}
catch(MyDefinedException mde)
{
[Link]();
}
}
}

25.55 transient
transient is a Java keyword which marks a member variable not to be serialized when it is
persisted to streams of bytes. When an object is transferred through the network, the object
needs to be 'serialized'. Serialization converts the object state to serial bytes. Those bytes
are sent over the network and the object is recreated from those bytes. Member variables
marked by the java transient keyword are not transferred; they are lost intentionally.
Syntax:

private transient <member-variable>;


or
transient private <member-variable>;

For example:

public class Foo implements Serializable


{
private String saveMe;
private transient String dontSaveMe;
private transient String password;
//...
}

See also:
• Java language specification reference: jls82
• Serializable Interface. Serializable83

25.56 try
try is a keyword.

82 [Link]
83 [Link]

220
try

It starts a try block. If an Exception is thrown inside a try block, the Exception will be
compared to any of the catch part of the block. If the Exception matches with one of the
Exceptions in the catch part, the exception will be handled there.
Three things can happen in a try block:
• No exception is thrown:
• the code in the try block
• plus the code in the finally block will be executed
• plus the code after the try-catch block is executed
• An exception is thrown and a match is found among the catch blocks:
• the code in the try block until the exception occurred is executed
• plus the matched catch block is executed
• plus the finally block is executed
• plus the code after the try-catch block is executed
• An exception is thrown and no match found among the catch blocks:
• the code in the try block until the exception occurred is executed
• plus the finally block is executed
• NO CODE after the try-catch block is executed
For example:

public void method() throws NoMatchedException


{
try {
//...
throw new '''MyException_1'''();
//...
} catch ( MyException_1 e ) {
// --- '''Handle the Exception_1 here''' --
} catch ( MyException_2 e ) {
// --- Handle the Exception_2 here --
} finally {
// --- This will always be executed no matter what --
}
// --- Code after the try-catch block
}

How the catch-blocks are evaluated see Catching Rule84


See also:
• Java Programming/Keywords/catch85
• Java Programming/Keywords/finally86
• Java Programming/Throwing and Catching Exceptions#Catching Rule87

[Link]
84
20Exceptions%23Catching%20Rule
85 Chapter 25.7 on page 189
86 Chapter 25.24 on page 198
[Link]
87
20Exceptions%23Catching%20Rule

221
Keywords

25.57 void
void is a Java keyword.
Used at method declaration and definition to specify that the method does not return any
type, the method returns void. It is not a type and there is no void references/pointers as
in C/C++.
For example:

public void method()


{
//...
return; // -- In this case the return is optional
//and not necessary to use public but some changes will be there
}

See also:
• Java Programming/Keywords/return88

25.58 volatile
volatile is a keyword.
When member variables are marked with this keyword, it changes the runtime behavior in
a way that is noticeable when multiple threads access these variables. Without the volatile
keyword, one thread could observe another thread update member variables in an order that
is not consistent with what is specified in sourcecode. Unlike the synchronized keyword,
concurrent access to a volatile field is allowed.
Syntax:

private volatile <member-variable>;


or
volatile private <member-variable>;

For example:

private volatile changingVar;

See also:
• Java Programming/Keywords/synchronized89

88 Chapter 25.42 on page 210


89 Chapter 25.49 on page 216

222
while

25.59 while
while is a Java keyword.
It starts a looping block.
The general syntax of a while, using Extended Backus-Naur Form, is

while-looping-statement ::= while condition-clause


single-statement | block-statement

condition-clause ::= ( Boolean Expression )


single-statement ::= Statement90
block-statement ::= { Statement91 [ Statements92 ] }

For example:

while ( i < maxLoopIter )


{
[Link]("Iter=" +i++);
}

See also:
• Java Programming/Statements93
• Java Programming/Keywords/for94
• Java Programming/Keywords/do95

93 Chapter 13.1 on page 92


94 Chapter 25.26 on page 201
95 Chapter 25.14 on page 193

223
26 Packages

If your application becomes quite big, you may have many classes. Although you can browse
them in their alphabetic order, it becomes confusing. So your application classes can be
sorted into packages.
A package is a name space that mainly contains classes and interfaces. For in-
stance, the standard class ArrayList is in the package [Link]. For this class,
[Link] is called its fully qualified name because this syntax has no am-
biguity. Classes in different packages can have the same name. For example, you have the
two classes [Link] and [Link] which are not the same. If no package is
declared in a class, its package is the default package.

26.1 Package declaration


In a class, a package is declared at the top of the source code using the keyword package:
Code listing 3.14: [Link]

1 package business;
2
3 public class BusinessClass {
4}

If your class is declared in a package, say business, your class must be placed in a subfolder
called business from the root of your application folder. This is how the compiler and
the class loader find the Java files on the file system. You can declare your class in a
subpackage, say engine. So the full package is [Link] and the class must be
placed in a subsubfolder called engine in the subfolder business (not in a folder called
[Link]).

26.2 Import and class usage


The simpliest way to use a class declared in a package is to prefix the class name with its
package:

Example:
Code section 3.88: Package declaration.
1 [Link] myBusinessClass = new [Link]();

225
Packages

If you are using the class from a class in the same package, you don't have to specify the
package. If another class with the same name exists in another package, it will use the local
class.
The syntax above is a bit verbose. You can import the class by using the import Java
keyword at the top of the file and then only specify its name:
Code listing 3.15: [Link]

1 import [Link];
2
3 public class MyClass {
4 public static void main(String[] args) {
5 BusinessClass myBusinessClass = new BusinessClass();
6 }
7 }

Note that you can't import two classes with the same name in two different packages.
The classes Integer and String belongs to the package [Link] but they don't need to
be imported as the [Link] package is implicitly imported in all classes.

26.3 Wildcard imports


It is possible to import an entire package, using an asterisk:

Example:
Code section 3.89: Wildcard imports.
1 import [Link].*;

While it may seem convenient, it may cause problems if you make a typographical error. For
example, if you use the above import to use JFrame, but then type JFraim frame = new
JFraim();, the Java compiler will report an error similar to ”Cannot find symbol: JFraim”.
Even though it seems as if it was imported, the compiler is giving the error report at the
first mention of JFraim, which is half-way through your code, instead of the point where
you imported JFrame along with everything else in [Link].
If you change this to import [Link]; the error will be at the import instead
of within your code.
Furthermore, if you import [Link].*; and import [Link].*;, and
[Link] is later added in a future version of Java, your code that uses
Queue ([Link]) will fail to compile. This particular example is fairly unlikely, but if you
are working with non-Oracle libraries, it may be more likely to happen.

226
Package convention

26.4 Package convention


A package name should start with a lower character. This eases to distinguish a package
from a class name. In some operating systems, the directory names are not case sensitive.
So package names should be lowercase.
The Java package needs to be unique across Vendors to avoid name collisions. For that
reason Vendors usually use their domain name in reverse order. That is guaranteed to be
unique. For example a company called Your Company Inc., would use a package name
something like this: [Link].

26.5 Importing packages from .jar files


If you are importing library packages and classes that reside in a .jar file, you must ensure
that the file is in the current classpath (both at compile- and execution-time). Apart from
this requirement, importing these packages and classes is the same as if they were in their
full, expanded, directory structure.

26.5.1 Javac
For example, to compile and run a class from a project's top directory (that contains the
two directories /source and /libraries) you could use the following command:
Compilation

$ javac -classpath libraries/[Link] source/[Link]

And then to run it, similarly:


Execution

$ java -classpath libraries/[Link] source/MainClass

The above is simplified, and demands that MainClass be in the default package, or a
package called source, which isn't very desirable.

26.5.2 BlueJ
With BlueJ just click on Tools, Preferences, Libraries, and add the .jar one by one.

26.6 Class loading/package


The runtime identity of a class in Java is defined by the fully qualified class name and its
defining class loader. This means that the same class, loaded by two different class loaders,
is seen by the Virtual Machine as two completely different types.

227
27 Arrays

An array is similar to a table of objects or primitive types, keyed by index. You may have
noticed the strange parameter of the default main() method (String[] args) since the
beginning of the book. It is an array. Let's handle this parameter:

Code listing 3.15: The default array parameter. Console for Code listing 3.15
$ java ArrayExample This is a test
1 public class ArrayExample { Argument #1 This
2 public static void main(String[] args) { Argument #2 is
3 for (int i = 0; i < [Link]; ++i) { Argument #3 a
4 [Link](”Argument #” + (i + 1) + ”: ” + args[i]); Argument #4 test
5 }
6 }
7 }

In the code listing 3.151 , the array is args. It is an array of String objects (here those
objects are the words that have been typed by the user at the program launching). At line
4, One contained object is accessed using its index in the array. You can see that its value
is printed on the standard output. Note that the strings have been put in the array with
the right order.

27.1 Fundamentals
In Java, an array is an object. This object has a given type for the contained primitive
types or objects (int, char, String, ...). An array can be declared in several ways:

Example:
Code section 3.52: Array declarations.
1 int[] array1 = null;
2 int array2[] = null;

Those syntaxes are identical but the first one is recommended. It can also be instantiated
in several ways:

1 Chapter 27.2 on page 231

229
Arrays

Example:
Code section 3.53: Array instantiations.
1 array1 = new int[10];
2 int[] array0 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; //this only works in the
3 declaration
4 array1 = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

At line 1, we instantiate an array of 10 items that get the default value (which is 0 for int
). At lines 2 and 3, we instantiate arrays of 10 given items. It will each be given an index
according to its order. We can know the size of the array using the length attribute:

Output for Code section 3.54


10
Example:
Code section 3.54: The array size.
1 int nbItems = 10;
2 Object[] array3 = new Object[nbItems];
3 [Link]([Link]);

Arrays are allocated at runtime, so the specified size in an array creation expression may be
a variable (rather than a constant expression as in C). However, the size of an instantiated
array never changes. If you need to change the size, you have to create a new instance.
Items can be accessed by their index. Beware! The first index is 0:

Output for Code section 3.55


c
Example: z
Code section 3.55: The array indexes.
1 char[] array4 = {'a', 'b', 'c', 'd', 'e'};
2 [Link](array4[2]);
3 array4[4] = 'z';
4 [Link](array4[4]);

If you attempt to access to a too high index or negative index, you will get an
ArrayIndexOutOfBoundsException.
Question 3.20: Consider the following code:
Question 3.20: [Link]

1 public class Question20 {


2 public static void main(String[] args) {
3 String[] listOfWord = {”beggars”, ”can't”, ”be”, ”choosers”};
4 [Link](listOfWord[1]);
5 [Link](listOfWord[[Link]-1]);
6 }
7 }

What will be printed in the standard output? Output for Question 3.20

230
Two-Dimensional Arrays

can't
choosers

Indexes start at 0. So the index 1 point at the second string (can't). There are 4 items so
the size of the array is 4. Hence the item pointed by the index 3 is the last one (choosers
).

27.2 Two-Dimensional Arrays


Actually, there are no two-dimensional arrays in Java. However, an array can contain any
class of object, including an array:

Example:
Code section 3.56: Two-dimensional arrays.
1 String[][] twoDimArray = {{"a", "b", "c", "d", "e"},
2 {"f", "g", "h", "i", "j"},
3 {"k", "l", "m", "n", "o"}};
4
5 int[][] twoDimIntArray = {{ 0, 1, 2, 3, 4},
6 {10, 11, 12, 13, 14},
7 {20, 21, 22, 23, 24}};

It's not exactly equivalent to two-dimensional arrays because the size of the sub-arrays may
vary. The sub-array reference can even be null. Consider:

Example:
Code section 3.57: Weird two-dimensional array.
1 String[][] weirdTwoDimArray = {{"10", "11", "12"},
2 null,
3 {"20", "21", "22", "23", "24"}};

Note that the length of a two-dimensional array is the number of one-dimensional ar-
rays it contains. In the above example, [Link] is 3, whereas
weirdTwoDimArray[2].length is 5.
In the code section 3.582 , we defined an array that has three elements, each element contains
an array having 5 elements. We could create the array having the 5 elements first and use
that one in the initialize block.

Example:
Code section 3.58: Included array.
1 String[] "01", "02", "03", "04"};
2 String[][] twoDimArray = {oneDimArray,
3 {"10", "11", "12", "13", "14"},
4 {"20", "21", "22", "23", "24"}};

2 Chapter 27.2 on page 231

231
Arrays

Question 3.21: Consider the following code:

Example:
Question 3.21: The alphabet.
1 String[][] alphabet = {{"a", "b", "c", "d", "e"},
2 {"f", "g", "h", "i", "j"},
3 {"k", "l", "m", "n", "o"},
4 {"p", "q", "r", "s", "t"},
5 {"u", "v", "w", "x", "y"},
6 {"z"}};

Print the whole alphabet in the standard output. Question 3.21: [Link]

1 public class Answer21 {


2 public static void main(String[] args) {
3 String[][] alphabet = {{”a”, ”b”, ”c”, ”d”, ”e”},
4 {”f”, ”g”, ”h”, ”i”, ”j”},
5 {”k”, ”l”, ”m”, ”n”, ”o”},
6 {”p”, ”q”, ”r”, ”s”, ”t”},
7 {”u”, ”v”, ”w”, ”x”, ”y”},
8 {”z”}};
9
10 for (int i = 0; i < [Link]; i++) {
11 for (int j = 0; j < alphabet[i].length; j++) {
12 [Link](alphabet[i][j]);
13 }
14 }
15 }
16 }

i will be the indexes of the main array and j will be the indexes of all the sub-arrays. We
have to first iterate on the main array. We have to read the size of the array. Then we
iterate on each sub-array. We have to read the size of each array as it may vary. Doing so,
we iterate on all the sub-array items using the indexes. All the items will be read in the
right order.

27.3 Multidimensional Array


Going further any number of dimensional array can be defined.

elementType[][]...[] arrayName

or
elementType arrayName[][]...[]

232
28 Mathematical functions

The [Link] class allows the use of many common mathematical functions that
can be used while creating programs.
Since it is in the [Link] package, the Math class does not need to be imported. However,
in programs extensively utilizing these functions, a static import can be used.

28.1 Math constants


There are two constants in the Math class that are fairly accurate approximations of irra-
tional mathematical numbers.

28.1.1 Math.E
The Math.E constant represents the value of Euler's number1 (e), the base of the natural
logarithm.

Example:
Code section 3.20: Math.E
1 public static final double E = 2.718281828459045;

28.1.2 [Link]
The [Link] constant represents the value of pi2 , the ratio of a circle's circumference to its
diameter.

Example:
Code section 3.21: [Link]
1 public static final double PI = 3.141592653589793;

28.2 Math methods


28.2.1 Exponential methods
There are several methods in the Math class that deal with exponential functions.

1 [Link]
2 [Link]

233
Mathematical functions

Exponentiation
The power method, double [Link](double, double), returns the first parameter to
the power of the second parameter. For example, a call to [Link](2, 10) will return a
value of 1024 (210 ).
The [Link](double) method, a special case of pow, returns e to the power of the param-
eter. In addition, double Math.expm1(double) returns (ex - 1). Both of these methods
are more accurate and convenient in these special cases.
Java also provides special cases of the pow function for square roots and cube roots of
doubles, double [Link](double) and double [Link](double).

28.2.2 Logarithms
Java has no general logarithm function; when needed this can be simulated using the change-
of-base theorem.
double [Link](double) returns the natural logarithm of the parameter (not the com-
mon logarithm, as its name suggests!).
double Math.log10(double) returns the common (base-10) logarithm of the parameter.
double Math.log1p(double) returns ln(parameter+1). It is recommended for small val-
ues.

28.2.3 Trigonometric and hyperbolic methods


The trigonometric methods of the Math class allow users to easily deal with trigonometric
functions in programs. All accept only doubles. Please note that all values using these
methods are initially passed and returned in radians, not degrees. However, conversions
are possible.

Trigonometric functions
The three main trigonometric methods are [Link](x), [Link](x), and [Link](x),
which are used to find the sine, cosine, and tangent, respectively, of any given number. So,
for example, a call to [Link]([Link]/2) would return a value of about 1. Although
methods for finding the cosecant, secant, and cotangent are not available, these values can
be found by taking the reciprocal of the sine, cosine, and tangent, respectively. For example,
the cosecant of pi/2 could be found using 1/[Link]([Link]/2).

Inverse trigonometric functions


Java provides inverse counterparts to the trigonometric functions: [Link](x), and
[Link](x), [Link](x).

234
Functions dealing with floating-point representation

Hyperbolic functions
In addition, hyperbolic functions are available: [Link](x), [Link](x), and Math.
tanh(x).

Radian/degree conversion
To convert between degree and radian measures of angles, two methods are available, Math.
toRadians(x) and [Link](x). While using [Link](x), a degrees value
must be passed in, and that value in radians (the degree value multiplied by pi/180) will
be returned. The [Link](x) method takes in a value in radians and the value in
degrees (the radian value multiplied by 180/pi) is returned.

28.2.4 Absolute value: [Link]


The absolute value method of the Math class is compatible with the int, long, float, and
double types. The data returned is the absolute value of parameter (how far away it is
from zero) in the same data type. For example:

Example:
Code section 3.22: [Link]
1 int result = [Link](-3);

In this example, result will contain a value of 3.

28.2.5 Maximum and minimum values


These methods are very simple comparing functions. Instead of using if...else statements,
one can use the [Link](x1, x2) and [Link](x1, x2) methods. The [Link](x1,
x2) simply returns the greater of the two values, while the [Link](x1, x2) returns the
lesser of the two. Acceptable types for these methods include int, long, float, and double.

28.3 Functions dealing with floating-point representation


Java 1.5 and 1.6 introduced several non-mathematical functions specific to the computer
floating-point representation of numbers.
[Link](double) and [Link](float) return an ulp, the smallest value which, when
added to the argument, would be recognized as larger than the argument.
[Link] returns the value of the first argument with the sign of the second argument.
It can be used to determine the sign of a zero value.
[Link] returns (as an int) the exponent used to scale the floating-point argu-
ment in computer representation.

235
Mathematical functions

28.4 Rounding number example


Sometimes, we are not only interested in mathematically correct rounded numbers, but we
want that a fixed number of significant digits are always displayed, regardless of the number
used. Here is an example program that returns always the correct string. You are invited
to modify it such that it does the same and is simpler!
The constant class contains repeating constants that should exist only once in the code so
that to avoid inadvertent changes. (If the one constant is changed inadvertently, it is most
likely to be seen, as it is used at several locations.) Code listing 3.20: [Link]

1 /**
2 * Class that comprises of constant values & string utilities.
3 *
4 * @since 2013-09-05
5 * @version 2014-10-14
6 */
7 public class StringUtils {
8 /** Dash or minus constant */
9 public static final char DASH = '-';
10 /** The exponent sign in a scientific number, or the capital letter E */
11 public static final char EXPONENT = 'E';
12 /** The full stop or period */
13 public static final char PERIOD = '.';
14 /** The zero string constant used at several places */
15 public static final String ZERO = ”0”;
16
17 /**
18 * Removes all occurrences of the filter character in the text.
19 *
20 * @param text Text to be filtered
21 * @param filter The character to be removed.
22 * @return the string
23 */
24 public static String filter(final String text, final String filter) {
25 final String[] words = [Link](”[” + filter + ”]”);
26
27 switch ([Link]) {
28 case 0: return text;
29 case 1: return words[0];
30 default:
31 final StringBuilder filteredText = new StringBuilder();
32
33 for (final String word : words) {
34 [Link](word);
35 }
36
37 return [Link]();
38 }
39 }
40 }

The MathsUtils class is like an addition to the [Link] class and contains the
rounding calculations. Code listing 3.21: [Link]

1 package string;
2
3 /**
4 * Class for special mathematical calculations.<br/>
5 * ATTENTION:<br/>Should depend only on standard Java libraries!
6 *
7 * @since 2013-09-05

236
Rounding number example

8 * @version 2014-10-14
9 */
10 public class MathsUtils {
11
12 // CONSTANTS
13 // ——————————————
14
15 /** The exponent sign in a scientific number, or the capital letter E.
*/
16 public static final char EXPONENT = 'E';
17
18 /** Value after which the language switches from scientific to double */
19 private static final double E_TO_DOUBLE = 1E-3;
20
21 /** The zero string constant used at several places. */
22 public static final String ZERO = ”0”;
23
24 /** The string of zeros */
25 private static final String ZEROS = ”000000000000000000000000000000000”;
26
27 // METHODS
28 // ——————————————
29
30 /**
31 * Determines, if the number uses a scientific representation.
32 *
33 * @param number the number
34 * @return true, if it is a scientific number, false otherwise
35 */
36 private static boolean isScientific(final double number) {
37 return ((new Double(number)).toString().indexOf(EXPONENT) > 0);
38 }
39
40 /**
41 * Determines how many zeros are to be appended after the decimal
digits.
42 *
43 * @param significantsAfter Requested significant digits after decimal
44 * @param separator Language-specific decimal separator
45 * @param number Rounded number
46 * @return Requested value
47 */
48 private static byte calculateMissingSignificantZeros(
49 final byte significantsAfter,
50 final char separator,
51 final double number) {
52
53 final byte after = findSignificantsAfterDecimal(separator, number);
54
55 final byte zeros =
56 (byte) (significantsAfter - ((after == 0) ? 1 : after));
57
58 return ((zeros >= 0) ? zeros : 0);
59 }
60
61 /**
62 * Finds the insignificant zeros after the decimal separator.
63 *
64 * @param separator Language-specific decimal separator
65 * @param number the number
66 * @return the byte
67 */
68 private static byte findInsignificantZerosAfterDecimal(
69 final char separator,
70 final double number) {
71

237
Mathematical functions

72 if (([Link](number) >= 1) || isScientific(number)) {


73 return 0;
74 } else {
75 final StringBuilder string = new StringBuilder();
76
77 [Link](number);
78 [Link](0,
79 [Link](new Character(separator).toString()) +
1);
80
81 // Determine what to match:
82 final String regularExpression = ”[1-9]”;
83
84 final String[] split =
[Link]().split(regularExpression);
85
86 return ([Link] > 0) ? (byte) split[0].length() : 0;
87 }
88 }
89
90 /**
91 * Calculates the number of all significant digits (without the sign and
92 * the decimal separator).
93 *
94 * @param significantsAfter Requested significant digits after decimal
95 * @param separator Language-specific decimal separator
96 * @param number Value where the digits are to be counted
97 * @return Number of significant digits
98 */
99 private static byte findSignificantDigits(final byte significantsAfter,
100 final char separator,
101 final double number) {
102
103 if (number == 0) { return 0; }
104 else {
105 String mantissa =
106 findMantissa(separator, new Double(number).toString());
107
108 if (number == (long)number) {
109 mantissa = [Link](0, [Link]() - 1);
110 }
111
112 mantissa = retrieveDigits(separator, mantissa);
113 // Find the position of the first non-zero digit:
114 short nonZeroAt = 0;
115
116 for (; (nonZeroAt < [Link]())
117 && ([Link](nonZeroAt) == '0'); nonZeroAt++) ;
118
119 return (byte)[Link](nonZeroAt).length();
120 }
121 }
122
123 /**
124 * Determines the number of significant digits after the decimal
separator
125 * knowing the total number of significant digits and the number before
the
126 * decimal separator.
127 *
128 * @param significantsBefore Number of significant digits before
separator
129 * @param significantDigits Number of all significant digits
130 * @return Number of significant decimals after the separator
131 */
132 private static byte findSignificantsAfterDecimal(

238
Rounding number example

133 final byte significantsBefore,


134 final byte significantDigits) {
135
136 final byte afterDecimal =
137 (byte) (significantDigits - significantsBefore);
138
139 return (byte) ((afterDecimal > 0) ? afterDecimal : 0);
140 }
141
142 /**
143 * Determines the number of digits before the decimal point.
144 *
145 * @param separator Language-specific decimal separator
146 * @param number Value to be scrutinised
147 * @return Number of digits before the decimal separator
148 */
149 private static byte findSignificantsBeforeDecimal(final char separator,
150 final double number) {
151
152 final String value = new Double(number).toString();
153
154 // Return immediately, if result is clear: Special handling at
155 // crossroads of floating point and exponential numbers:
156 if ((number == 0) || ([Link](number) >= E_TO_DOUBLE)
157 && ([Link](number) < 1)) {
158
159 return 0;
160 } else if (([Link](number) > 0) && ([Link](number) <
E_TO_DOUBLE)) {
161 return 1;
162 } else {
163 byte significants = 0;
164 // Significant digits to the right of decimal separator:
165 for (byte b = 0; b < [Link](); b++) {
166 if ([Link](b) == separator) {
167 break;
168 } else if ([Link](b) != [Link]) {
169 significants++;
170 }
171 }
172
173 return significants;
174 }
175 }
176
177 /**
178 * Returns the exponent part of the double number.
179 *
180 * @param number Value of which the exponent is of interest
181 * @return Exponent of the number or zero.
182 */
183 private static short findExponent(final double number) {
184 return new Short(findExponent((new Double(number)).toString()));
185 }
186
187 /**
188 * Finds the exponent of a number.
189 *
190 * @param value Value where an exponent is to be searched
191 * @return Exponent, if it exists, or ”0”.
192 */
193 private static String findExponent(final String value) {
194 final short exponentAt = (short) [Link](EXPONENT);
195
196 if (exponentAt < 0) { return ZERO; }
197 else {

239
Mathematical functions

198 return [Link](exponentAt + 1);


199 }
200 }
201
202 /**
203 * Finds the mantissa of a number.
204 *
205 * @param separator Language-specific decimal separator
206 * @param value Value where the mantissa is to be found
207 * @return Mantissa of the number
208 */
209 private static String findMantissa(final char separator,
210 final String value) {
211
212 String strValue = value;
213
214 final short exponentAt = (short) [Link](EXPONENT);
215
216 if (exponentAt > -1) {
217 strValue = [Link](0, exponentAt);
218 }
219 return strValue;
220 }
221
222 /**
223 * Retrieves the digits of the value without decimal separator or sign.
224 *
225 * @param separator
226 * @param number Mantissa to be scrutinised
227 * @return The digits only
228 */
229 private static String retrieveDigits(final char separator, String
number) {
230 // Strip off exponent part, if it exists:
231 short eAt = (short)[Link](EXPONENT);
232
233 if (eAt > -1) {
234 number = [Link](0, eAt);
235 }
236
237 return [Link]((new Character([Link])).toString(),
” ”).
238 replace((new Character(separator)).toString(), ” ”);
239 }
240
241
242 // —- Public methods ———————-
243
244 /**
245 * Returns the number of digits in the long value.
246 *
247 * @param value the value
248 * @return the byte
249 */
250 public static byte digits(final long value) {
251 return (byte) [Link]([Link](value),
”.,”).length();
252 }
253
254 /**
255 * Finds the significant digits after the decimal separator of a
mantissa.
256 *
257 * @param separator Language-specific decimal separator
258 * @param number Value to be scrutinised
259 * @return Number of significant zeros after decimal separator.

240
Rounding number example

260 */
261 public static byte findSignificantsAfterDecimal(final char separator,
262 final double number) {
263
264 if (number == 0) { return 1; }
265 else {
266 String value = (new Double(number)).toString();
267
268 final short separatorAt = (short) [Link](separator);
269
270 if (separatorAt > -1) {
271 value = [Link](separatorAt + 1);
272 }
273
274 final short exponentAt = (short) [Link](EXPONENT);
275
276 if (exponentAt > 0) {
277 value = [Link](0, exponentAt);
278 }
279
280 final Long longValue = new Long(value).longValue();
281
282 if ([Link](number) < 1) {
283 return (byte) [Link]().length();
284 } else if (longValue == 0) {
285 return 0;
286 } else {
287 return (byte) ((”0.” + value).length() - 2);
288 }
289 }
290 }
291
292 /**
293 * Calculates the power of the base to the exponent without changing the
294 * least-significant digits of a number.
295 *
296 * @param basis
297 * @param exponent
298 * @return basis to power of exponent
299 */
300 public static double power(final int basis, final short exponent) {
301 return power((short) basis, exponent);
302 }
303
304 /**
305 * Calculates the power of the base to the exponent without changing the
306 * least-significant digits of a number.
307 *
308 * @param basis the basis
309 * @param exponent the exponent
310 * @return basis to power of exponent
311 */
312 public static double power(final short basis, final short exponent) {
313 if (basis == 0) {
314 return (exponent != 0) ? 1 : 0;
315 } else {
316 if (exponent == 0) {
317 return 1;
318 } else {
319 // The Math method power does change the least significant
320 // digits after the decimal separator and is therefore
useless.
321 double result = 1;
322 short s = 0;
323
324 if (exponent > 0) {

241
Mathematical functions

325 for (; s < exponent; s++) {


326 result *= basis;
327 }
328 } else if (exponent < 0) {
329 for (s = exponent; s < 0; s++) {
330 result /= basis;
331 }
332 }
333
334 return result;
335 }
336 }
337 }
338
339 /**
340 * Rounds a number to the decimal places.
341 *
342 * @param significantsAfter Requested significant digits after decimal
343 * @param separator Language-specific decimal separator
344 * @param number Number to be rounded
345 * @return Rounded number to the requested decimal places
346 */
347 public static double round(final byte significantsAfter,
348 final char separator,
349 final double number) {
350
351 if (number == 0) { return 0; }
352 else {
353 final double constant = power(10, (short)
354 (findInsignificantZerosAfterDecimal(separator, number)
355 + significantsAfter));
356 final short dExponent = findExponent(number);
357
358 short exponent = dExponent;
359
360 double value = number*constant*[Link](10, -exponent);
361 final String exponentSign =
362 (exponent < 0) ? [Link]([Link]) : ” ”;
363
364 if (exponent != 0) {
365 exponent = (short) [Link](exponent);
366
367 value = round(value);
368 } else {
369 value = round(value)/constant;
370 }
371
372 // Power method cannot be used, as the exponentiated number may
373 // exceed the maximal long value.
374 exponent -= [Link](dExponent)*(findSignificantDigits
375 (significantsAfter, separator, value) - 1);
376
377 if (dExponent != 0) {
378 String strValue = [Link](value);
379
380 strValue = [Link](0,
[Link](separator))
381 + EXPONENT + exponentSign +
[Link](exponent);
382
383 value = new Double(strValue);
384 }
385
386 return value;
387 }
388 }

242
Rounding number example

389
390 /**
391 * Rounds a number according to mathematical rules.
392 *
393 * @param value the value
394 * @return the double
395 */
396 public static double round(final double value) {
397 return (long) (value + .5);
398 }
399
400 /**
401 * Rounds to a fixed number of significant digits.
402 *
403 * @param significantDigits Requested number of significant digits
404 * @param separator Language-specific decimal separator
405 * @param dNumber Number to be rounded
406 * @return Rounded number
407 */
408 public static String roundToString(final byte significantDigits,
409 final char separator,
410 double dNumber) {
411
412 // Number of significants that *are* before the decimal separator:
413 final byte significantsBefore =
414 findSignificantsBeforeDecimal(separator, dNumber);
415 // Number of decimals that *should* be after the decimal separator:
416 final byte significantsAfter = findSignificantsAfterDecimal(
417 significantsBefore, significantDigits);
418 // Round to the specified number of digits after decimal separator:
419 final double rounded = [Link](significantsAfter,
separator,
420 dNumber);
421
422 final String exponent = findExponent((new
Double(rounded)).toString());
423 final String mantissa = findMantissa(separator,
424 (new Double(rounded)).toString());
425
426 final double dMantissa = new Double(mantissa).doubleValue();
427 final StringBuilder result = new StringBuilder(mantissa);
428 // Determine the significant digits in this number:
429 final byte significants = findSignificantDigits(significantsAfter,
430 separator, dMantissa);
431 // Add lagging zeros, if necessary:
432 if (significants <= significantDigits) {
433 if (significantsAfter != 0) {
434 [Link]([Link](0,
435 calculateMissingSignificantZeros(significantsAfter,
436 separator, dMantissa)));
437 } else {
438 // Cut off the decimal separator & after decimal digits:
439 final short decimal = (short) [Link](
440 new Character(separator).toString());
441
442 if (decimal > -1) {
443 [Link](decimal);
444 }
445 }
446 } else if (significantsBefore > significantDigits) {
447 dNumber /= power(10, (short) (significantsBefore -
448 significantDigits));
449
450 dNumber = round(dNumber);
451
452 final short digits =

243
Mathematical functions

453 (short) (significantDigits + ((dNumber < 0) ? 1 : 0));


454
455 final String strDouble = (new
456 Double(dNumber)).toString().substring(0, digits);
457
458 [Link](0);
459 [Link](strDouble + [Link](0,
460 significantsBefore - significantDigits));
461 }
462
463 if (new Short(exponent) != 0) {
464 [Link](EXPONENT + exponent);
465 }
466
467 return [Link]();
468 } // public static String roundToString(…)
469
470 /**
471 * Rounds to a fixed number of significant digits.
472 *
473 * @param separator Language-specific decimal separator
474 * @param significantDigits Requested number of significant digits
475 * @param value Number to be rounded
476 * @return Rounded number
477 */
478 public static String roundToString(final char separator,
479 final int significantDigits,
480 float value) {
481
482 return roundToString((byte)significantDigits, separator,
483 (double)value);
484 }
485 } // class MathsUtils

The code is tested with the following JUnit3 test:


Code listing 3.22: [Link]

1 package string;
2
3 import static [Link];
4 import static [Link];
5 import static [Link];
6
7 import [Link];
8
9 import [Link];
10
11 /**
12 * The JUnit test for the <code>MathsUtils</code> class.
13 *
14 * @since 2013-03-26
15 * @version 2014-10-14
16 */
17 public class MathsUtilsTest {
18
19 /**
20 * Method that adds a negative and a positive value to values.
21 *
22 * @param d the double value
23 * @param values the values

3 [Link]

244
Rounding number example

24 */
25 private static void addValue(final double d, Vector<Double> values) {
26 [Link](-d);
27 [Link](d);
28 }
29
30 // Public methods ——
31
32 /**
33 * Tests the round method with a double parameter.
34 */
35 @Test
36 public void testRoundToStringDoubleByteCharDouble() {
37 // Test rounding
38 final Vector<Double> values = new Vector<Double>();
39 final Vector<String> strValues = new Vector<String>();
40
41 [Link](0.0);
42 [Link](”0.00000”);
43 addValue(1.4012984643248202e-45, values);
44 [Link](”-1.4012E-45”);
45 [Link](”1.4013E-45”);
46 addValue(1.999999757e-5, values);
47 [Link](”-1.9999E-5”);
48 [Link](”2.0000E-5”);
49 addValue(1.999999757e-4, values);
50 [Link](”-1.9999E-4”);
51 [Link](”2.0000E-4”);
52 addValue(1.999999757e-3, values);
53 [Link](”-0.0019999”);
54 [Link](”0.0020000”);
55 addValue(0.000640589, values);
56 [Link](”-6.4058E-4”);
57 [Link](”6.4059E-4”);
58 addValue(0.3396899998188019, values);
59 [Link](”-0.33968”);
60 [Link](”0.33969”);
61 addValue(0.34, values);
62 [Link](”-0.33999”);
63 [Link](”0.34000”);
64 addValue(7.07, values);
65 [Link](”-7.0699”);
66 [Link](”7.0700”);
67 addValue(118.188, values);
68 [Link](”-118.18”);
69 [Link](”118.19”);
70 addValue(118.2, values);
71 [Link](”-118.19”);
72 [Link](”118.20”);
73 addValue(123.405009, values);
74 [Link](”-123.40”);
75 [Link](”123.41”);
76 addValue(30.76994323730469, values);
77 [Link](”-30.769”);
78 [Link](”30.770”);
79 addValue(130.76994323730469, values);
80 [Link](”-130.76”);
81 [Link](”130.77”);
82 addValue(540, values);
83 [Link](”-539.99”);
84 [Link](”540.00”);
85 addValue(12345, values);
86 [Link](”-12344”);
87 [Link](”12345”);
88 addValue(123456, values);
89 [Link](”-123450”);

245
Mathematical functions

90 [Link](”123460”);
91 addValue(540911, values);
92 [Link](”-540900”);
93 [Link](”540910”);
94 addValue(9.223372036854776e56, values);
95 [Link](”-9.2233E56”);
96 [Link](”9.2234E56”);
97
98 byte i = 0;
99 final byte significants = 5;
100
101 for (final double element : values) {
102 final String strValue;
103
104 try {
105 strValue = [Link](significants,
106 [Link], element);
107
108 [Link](” [Link](” + significants + ”,
'”
109 + [Link] + ”', ” + element + ”) ==> ”
110 + strValue + ” = ” + [Link](i));
111 assertEquals(”Testing roundToString”, strValue,
112 [Link](i++));
113 } catch (final Exception e) {
114 // TODO Auto-generated catch block
115 [Link]();
116 }
117 }
118 }
119
120 } // class MathsUtilsTest

The output of the JUnit test follows:


Output for code listing 3.22

[Link](5, '.', 0.0) ==> 0.00000 = 0.00000


[Link](5, '.', -1.4012984643248202E-45) ==> -1.4012E-45 = -1.4012E-45
[Link](5, '.', 1.4012984643248202E-45) ==> 1.4013E-45 = 1.4013E-45
[Link](5, '.', -1.999999757E-5) ==> -1.9999E-5 = -1.9999E-5
[Link](5, '.', 1.999999757E-5) ==> 2.0000E-5 = 2.0000E-5
[Link](5, '.', -1.999999757E-4) ==> -1.9999E-4 = -1.9999E-4
[Link](5, '.', 1.999999757E-4) ==> 2.0000E-4 = 2.0000E-4
[Link](5, '.', -0.001999999757) ==> -0.0019999 = -0.0019999
[Link](5, '.', 0.001999999757) ==> 0.0020000 = 0.0020000
[Link](5, '.', -6.40589E-4) ==> -6.4058E-4 = -6.4058E-4
[Link](5, '.', 6.40589E-4) ==> 6.4059E-4 = 6.4059E-4
[Link](5, '.', -0.3396899998188019) ==> -0.33968 = -0.33968
[Link](5, '.', 0.3396899998188019) ==> 0.33969 = 0.33969
[Link](5, '.', -0.34) ==> -0.33999 = -0.33999
[Link](5, '.', 0.34) ==> 0.34000 = 0.34000
[Link](5, '.', -7.07) ==> -7.0699 = -7.0699
[Link](5, '.', 7.07) ==> 7.0700 = 7.0700
[Link](5, '.', -118.188) ==> -118.18 = -118.18
[Link](5, '.', 118.188) ==> 118.19 = 118.19
[Link](5, '.', -118.2) ==> -118.19 = -118.19
[Link](5, '.', 118.2) ==> 118.20 = 118.20
[Link](5, '.', -123.405009) ==> -123.40 = -123.40
[Link](5, '.', 123.405009) ==> 123.41 = 123.41
[Link](5, '.', -30.76994323730469) ==> -30.769 = -30.769
[Link](5, '.', 30.76994323730469) ==> 30.770 = 30.770
[Link](5, '.', -130.7699432373047) ==> -130.76 = -130.76
[Link](5, '.', 130.7699432373047) ==> 130.77 = 130.77

246
Rounding number example

[Link](5, '.', -540.0) ==> -539.99 = -539.99


[Link](5, '.', 540.0) ==> 540.00 = 540.00
[Link](5, '.', -12345.0) ==> -12344 = -12344
[Link](5, '.', 12345.0) ==> 12345 = 12345
[Link](5, '.', -123456.0) ==> -123450 = -123450
[Link](5, '.', 123456.0) ==> 123460 = 123460
[Link](5, '.', -540911.0) ==> -540900 = -540900
[Link](5, '.', 540911.0) ==> 540910 = 540910
[Link](5, '.', -9.223372036854776E56) ==> -9.2233E56 = -9.2233E56
[Link](5, '.', 9.223372036854776E56) ==> 9.2234E56 = 9.2234E56

If you are interested in a comparison with C#4 , take a look at the rounding number example5
there. If you are interested in a comparison with C++6 , you can compare this code here
with the same example7 over there.
Notice that in the expression starting with if ((D == 0), I have to use OR instead of the
|| because of a bug in the source template.

4 [Link]
[Link]
5
2FConsole%20Programming%23Rounding%20number%20example
6 [Link]
[Link]
7
8ERounding_number_example

247
29 Large numbers

The integer primitive type with the largest range of value is the long, from -263 to 263 -1.
If you need greater or lesser values, you have to use the BigInteger class in the package
[Link]. A BigInteger object can represent any integer (as large as the RAM on the
computer can hold) as it is not mapped on a primitive type. Respectively, you need to use
the BigDecimal class for great decimal numbers.
However, as these perform much slower than primitive types, it is recommended to use
primitive types when it is possible.

29.1 BigInteger
The BigInteger class represents integers of almost any size. As with other objects, they
need to be constructed. Unlike regular numbers, the BigInteger represents an immutable
object - methods in use by the BigInteger class will return a new copy of a BigInteger.
To instantiate a BigInteger, you can create it from either byte array, or from a string. For
example:

Example:
Code section 3.23: 1 quintillion, or 10^18. Too large to fit in a long.
1 BigInteger i = new BigInteger("1000000000000000000");

BigInteger cannot use the normal Java operators1 . They use the methods provided by the
class.

Example:
Code section 3.24: Multiplications and an addition.
1 BigInteger a = new BigInteger("3");
2 BigInteger b = new BigInteger("4");
3
4 // c = a^2 + b^2
5 BigInteger c = [Link](a).add([Link](b));

It is possible to convert to a long, but the long may not be large enough.

1 Chapter 27.3 on page 232

249
Large numbers

Example:
Code section 3.25: Conversion.
1 BigInteger aBigInteger = new BigInteger("3");
2 long aLong = [Link]();

29.2 BigDecimal
The BigInteger class cannot handle decimal numbers. The BigDecimal class represents
a floating point value of arbitrary precision. It is composed of both a BigInteger, and a
scale value (represented by a 32-bit integer).
Googol = 10100

250
30 Random numbers

To generate random numbers the [Link]() method can be used, which returns a
double, greater than or equal to 0.0 and less than 1.0.
The following code returns a random integer between n and m (where n <= randomNumber
< m):

Example:
Code section 3.30: A random integer.
1 int randomNumber = n + (int)([Link]() * ( m - n ));

Alternatively, the [Link] class provides methods for generating random


booleans, bytes, floats, ints, longs and 'Gaussians' (doubles from a normal distribution
with mean 0.0 and standard deviation 1.0). For example, the following code is equivalent
to that above:

Example:
Code section 3.31: A random integer with Gaussian.
1 Random random = new Random();
2 int randomNumber = n + [Link](m - n);

As an example using random numbers, we can make a program that uses a Random object
to simulate flipping a coin 20 times:

251
Random numbers

Code listing 3.25: [Link] Possible output for code listing 3.25
Heads
1 import [Link]; Tails
2 Tails
3 public class CoinFlipper { Tails
4 Heads
5 public static void main(String[] args) { Tails
6 // The number of times to flip the coin Heads
7 final int TIMES_TO_FLIP = 20; Heads
8 int heads = 0; Heads
9 int tails = 0; Heads
10 // Create a Random object Heads
11 Random random = new Random(); Heads
12 for (int i = 0; i < TIMES_TO_FLIP; i++) { Tails
13 // 0 or 1 Tails
14 int result = [Link](2); Tails
15 if (result == 1) { Tails
16 [Link](”Heads”); Heads
17 heads++; Tails
18 } else { Tails
19 [Link](”Tails”); Tails
20 tails++; There were 9 heads and 11 tails
21 }
22 }
23 [Link](”There were ”
24 + heads
25 + ” heads and ”
26 + tails
27 + ” tails”);
28 }
29 }

Of course, if you run the program you will probably get different results.

30.1 Truly random numbers


Both [Link]() and the Random class produce pseudorandom numbers. This is good
enough for a lot of applications, but remember that it is not truly random. If you want a
more secure random number generator, Java provides the [Link]
package. What happens with [Link]() and the Random class is that a 'seed' is chosen
from which the pseudorandom numbers are generated. SecureRandom increases the security
to ensure that the seed which is used by the pseudorandom number generator is non-
deterministic — that is, you cannot simply put the machine in the same state to get the
same set of results. Once you have created a SecureRandom instance, you can use it in the
same way as you can the Random class.
If you want truly random numbers, you can get a hardware random number generator or
use a randomness generation service.

252
31 Unicode

Most Java program text consists of w:ASCII1 characters, but any Unicode character can
be used as part of identifier names, in comments, and in character and string literals. For
example, π (which is the Greek Lowercase Letter pi) is a valid Java identifier:

Example:
Code section 3.100: Pi.
1 double π = [Link];

and in a string literal:

Example:
Code section 3.101: Pi literal.
1 String pi = "π";

31.1 Unicode escape sequences


Unicode characters can also be expressed through Unicode Escape Sequences. Unicode
escape sequences may appear anywhere in a Java source file (including inside identifiers,
comments, and string literals2 ).
Unicode escape sequences consist of
1. a backslash '\' (ASCII character 92, hex 0x5c),
2. a 'u' (ASCII 117, hex 0x75)
3. optionally one or more additional 'u' characters, and
4. four hexadecimal digits (the characters '0' through '9' or 'a' through 'f' or 'A' through
'F').
Such sequences represent the UTF-16 encoding of a Unicode character. For example, 'a' is
equivalent to '\u0061'. This escape method does not support characters beyond U+FFFF
or you have to make use of surrogate pairs.3
Any and all characters in a program may be expressed in Unicode escape characters, but
such programs are not very readable, except by the Java compiler - in addition, they are
not very compact.

1 [Link]
2 Chapter 20.1 on page 150
3 ”3.1 Unicode”, The Java™ Language Specification [Link]
mr3-fullv-oth-JSpec/[Link], Java SE 7 Edition, pp. 15-16.

253
Unicode

One can find a full list of the characters here4 .


π may also be represented in Java as the Unicode escape sequence \u03C0. Thus, the
following is a valid, but not very readable, declaration and assignment:

Example:
Code section 3.102: Unicode escape sequences for Pi.
1 double \u03C0 = [Link];

The following demonstrates the use of Unicode escape sequences in other Java syntax:

Example:
Code section 3.103: Unicode escape sequences in a string literal.
1 // Declare Strings pi and quote which contain \u03C0 and \u0027 respectively:
2 String pi = "\u03C0";
3 String quote = "\u0027";

Note that a Unicode escape sequence functions just like any other character in the source
code. E.g., \u0022 (double quote, ”) needs to be quoted in a string just like ”.

Example:
Code section 3.104: Double quote.
1 // Declare Strings doubleQuote1 and doubleQuote2 which both contain " (double
2 quote):
3 String doubleQuote1 = "\"";
4 String doubleQuote2 = "\\u0022"; // "\u0022" doesn't work since """ doesn't
5 work.





31.2 International language support
The language distinguishes between bytes and characters. Characters are stored internally
using UCS-2, although as of J2SE 5.0, the language also supports using UTF-16 and its
surrogates. Java program source may therefore contain any Unicode character.
The following is thus perfectly valid Java code; it contains Chinese characters in the class
and variable5 names as well as in a string literal6 : Code listing 3.50: .java

1 public class 哈嘍世界 {


2 private String 文本 = ”哈嘍世界”;
3}

31.3 References

4 [Link]
5 Chapter 17.2 on page 128
6 Chapter 20.1 on page 150

254
32 Comments

A comment allows to insert text that will not be compiled nor interpreted. It can appear
anywhere in the source code where whitespaces are allowed.
It is useful for explaining what the source code does by:
• explaining the adopted technical choice: why this given algorithm and not another, why
calling this given method...
• explaining what should be done in the next steps (the TODO list): improvement, issue
to fix...
• giving the required explanation to understand the code and be able to update it yourself
later or by other developers.
It can also be used to make the compiler ignore a portion of code: temporary code for
debugging, code under development...

32.1 Syntax
The comments in Java use the same syntax as in C++1 .
An end-of-line comment starts with two slashes and ends with the end of the line. This
syntax can be used on a single line too.

Example:
Code section 3.105: Slash-slash comment.
1 // A comment to give an example
2
3 int n = 10; // 10 articles

A comment on several lines is framed with '/' + '*' and '*' + '/'.

1 [Link]

255
Comments

Example:
Code section 3.106: Slash-star comment in multiple lines.
1 /*
2 * This is a comment
3 * on several lines.
4 */
5
6 /* This also works; slash-star comments may be on a single line. */
7
8 /*
9 Disable debugging code:
10
11 int a = 10;
12 while (a-- > 0) [Link]("DEBUG: tab["+a+"]=" + tab[a]);
13 */

By convention, subsequent lines of slash-star comments begin with a star aligned under
the star in the open comment sequence, but this is not required. Never nest a slash-star
comment in another slash-star comment. If you accidentally nest such comments, you will
probably get a syntax error from the compiler soon after the first star-slash sequence.

1 Warning
Code section 3.107: Nested slash-star comment.
1 /* This comment appears to contain /* a nested comment. */
2 * The comment ends after the first star-slash and
3 * everything after the star-slash sequence is parsed
4 * as non-comment source.
5 */

If you need to have the sequence */ inside a comment you can use html numeric entities:
*&#47;.
Slash-star comments may also be placed between any Java tokens, though not recommended:

Example:
Code section 3.108: Inline slash-star comment.
1 int i = /* maximum integer */ Integer.MAX_VALUE;

However, comments are not parsed as comments when they occur in string literals.

Example:
Code section 3.109: String literal.
1 String text = "/* This is not a comment. */";

It results in a 33 character string.


Question 3.26: Consider the following code:

256
Comments and unicode

Example:
Question 3.26: Commented code.
int a = 0;
// a = a + 1;
a = a + 1;
/*
a = a + 1;
*/
a = a + 1;
// /*
a = a + 1;
// */
a = a /*+ 1*/;
a = a + 1; // a = a + 1;
[Link]("a=" + a);

What is printed in the standard output?

Console:
Output for Answer 3.26
a=4

Example:
Answer 3.26: Commented code.
1 int a = 0;
2 // a = a + 1;
3 a = a + 1;
4 /*
5 a = a + 1;
6 */
7 a = a + 1;
8 // /*
9 a = a + 1;
10 // */
11 a = a /*+ 1*/;
12 a = a + 1; // a = a + 1;
13 [Link]("a=" + a);

The highlighted lines are code lines but line 11 does nothing and only the first part of line
12 is code.

32.2 Comments and unicode


Be aware that Java still interprets Unicode sequences within comments. For example, the
Unicode sequence \u002a\u002f (whose codepoints correspond to */) is processed early in
the Java compiler's lexical scanning of the source file, even before comments are processed,
so this is a valid star-slash comment in Java:

257
Comments

Example:
Code section 3.110: Unicode sequence interruption.
1 /* This is a comment. \u002a\u002f
2 String statement = "This is not a comment.";

and is lexically equivalent to

Example:
Code section 3.111: Unicode sequence interruption effect.
1 /* This is a comment. */
2 String statement = "This is not a comment.";

(The '*' character is Unicode 002A and the '/' character is Unicode 002F.)
Similar caveats apply to newline characters in slash-slash comments.
For example:

1 Warning
Code section 3.112: New line.
1 // This is a single line comment \u000a This is code

That is because \u000a is Unicode for a new line, making the compiler think that you have
added a new line when you haven't.

32.3 Javadoc comments


Javadoc2 comments are a special case of slash-star comments.

Example:
Code section 3.113: Javadoc comment.
1 /**
2 * Comments which start with slash-star-star are Javadoc comments.
3 * These are used to extract documentation from the Java source.
4 * More on javadoc will be covered later.
5 */

2 [Link]

258
33 Coding conventions

The Java code conventions are defined by Oracle in the coding conventions1 document. In
short, these conventions ask the user to use camel case2 when defining classes, methods, or
variables. Classes start with a capital letter and should be nouns, like CalendarDialogView
. For methods, the names should be verbs in imperative form, like getBrakeSystemType,
and should start with a lowercase letter.
It is important to get used to and follow coding conventions, so that code written by multiple
programmers will appear the same. Projects may re-define the standard code conventions
to better fit their needs. Examples include a list of allowed abbreviations, as these can often
make the code difficult to understand for other designers. Documentation should always
accompany code.
One example from the coding conventions is how to define a constant. Constants should be
written with capital letters in Java, where the words are separated by an underscore ('_')
character. In the Java coding conventions, a constant is a static final field in a class.
The reason for this diversion is that Java discerns between ”simple” and ”complex” types.
These will be handled in detail in the following sections. An example for a simple type is
the byte type. An example for a complex type is a class. A subset of the complex types
are classes that cannot be modified after creation, like a String, which is a concatenation
of characters.
For instance, consider the following ”constants”:
1 public class MotorVehicle {
2 /** Number of motors */
3 private static final int MOTORS = 1;
4
5 /** Name of a motor */
6 private static final String MOTOR_NAME = "Mercedes V8";
7
8 /** The motor object */
9 private static final Motor THE_MOTOR = new MercedesMotor();
10
11 /**
12 * Constructor
13 */
14 public MotorVehicle() {
15 MOTORS = 2; // Gives a syntax error as MOTORS has
16 already been assigned a value.
17 THE_MOTOR = new ToshibaMotor(); // Gives a syntax error as THE_MOTOR has
18 already been assigned a value.
19 MOTOR_NAME.toLowercase(); // Does not give a syntax error, because
it
20 returns a new String rather than editing the MOTOR_NAME variable.

1 [Link]
2 [Link]

259
Coding conventions

21 THE_MOTOR.fillFuel(20.5); // Does not give a syntax error, as it


22 changes a variable in the motor object, not the variable itself.
23 }
24 }

260
34 Classes and Objects

34.1 Classes and Objects


An object-oriented program is built from objects. A class is a ”template” that is used to
create objects. The class defines the values the object can contain and the operations that
can be performed on the object.
After compilation, a class is stored on the file system in a '(class-name).class' file.
The class is loaded1 into memory when we are about to create2 the first object from that
class, or when we call one of its static functions.
During class loading all the class static variables are initialized. Also operations defined in
a static { ... } block are executed. Once a class is loaded it stays in memory, and the
class static variables won't be initialized again.
After the class is loaded into memory, objects can be created from that class. When an
object is created, its member variables are initialized, but the class static variables are not.
When there are no more references to an object, the garbage collector will destroy3 the
object and free its memory, so that the memory can be reused to hold new objects.

1 [Link]
2 [Link]
3 [Link]

261
35 Defining Classes

35.1 Fundamentals
Every class in Java can be composed of the following elements:
• fields, member variables or instance variables — Fields are variables that hold data
specific to each object. For example, an employee might have an ID number. There is
one field for each object of a class.
• member methods or instance methods — Member methods perform operations on
an object. For example, an employee might have a method to issue his paycheck or to
access his name.
• static fields or class fields — Static fields are common to any object of the same class.
For example, a static field within the Employee class could keep track of the last ID
number issued. Each static field exists only once in the class, regardless of how many
objects are created for that class.
• static methods or class methods — Static methods are methods that do not affect a
specific object.
• inner classes — Sometimes it is useful to contain a class within another one if it is
useless outside of the class or should not be accessed outside the class.
• Constructors — A special method that generates a new object.
• Parameterized types — Since 1.5, parameterized types can be assigned to a class during
definition. The parameterized types will be substituted with the types specified at the
class's instantiation. It is done by the compiler. It is similar to the C language macro
'#define' statement, where a preprocessor evaluates the macros.
Code listing 4.1: [Link]

1 public class Employee { // This defines the Employee class.


2 // The public modifier indicates that
3 // it can be accessed by any other class
4
5 private static int nextID; // Define a static field. Only one copy
of
6 this will exist,
7 // no matter how many Employees are
8 created.
9
10 private int myID; // Define fields that will be stored
11 private String myName; // for each Employee. The private
modifier
12 indicates that
13 // only code inside the Employee class
can
14 access it.
15
16 public Employee(String name) { // This is a constructor. You can pass a
17 name to the constructor

263
Defining Classes

18 // and it will give you a newly created


19 Employee object.
20 myName = name;
21 myID = nextID; // Automatically assign an ID to the
object
22 nextID++; // Increment the ID counter
23 }
24
25 public String getName() { // This is a member method that returns
the
26 // Employee object's name.
27 return myName; // Note how it can access the private
field
28 myName.
29 }
30
31 public int getID() { // This is another member method.
32
33 return myID;
34 }
35
36 public static int getNextID() { // This is a static method that returns
the
37 next ID
38 // that will be assigned if another
39 Employee is created.
40 return nextID;
41 }
42 }

The following Java code would produce this output:


Code listing 4.2: [Link]
Console:
1 public class EmployeeList { Console for Code listing 4.2
2 public static void main(String[] args) { 0
3 3
4 [Link]([Link]()); 0: John Doe
5 1: Jane Smith
6 Employee a = new Employee(”John Doe”); 2: Sally Brown
7 Employee b = new Employee(”Jane Smith”);
8 Employee c = new Employee(”Sally Brown”);
9
10 [Link]([Link]());
11
12 [Link]([Link]() + ”: ” + [Link]-
Name());
13 [Link]([Link]() + ”: ” + [Link]-
Name());
14 [Link]([Link]() + ”: ” + [Link]-
Name());
15 }
16 }

35.2 Constructors
A constructor is called to initialize an object immediately after the object has been
allocated: Code listing 4.3: [Link]

264
Constructors

1 public class Cheese {


2 // This is a constructor
3 public Cheese() {
4 [Link](”Construct an instance”);
5 }
6 }

Typically, a constructor is invoked using the new keyword:

Example:
Code section 4.1: A constructor call.
1 Cheese cheese = new Cheese();

The constructor syntax is close to the method syntax. However, the constructor has the
same name as the name of the class (with the same case) and the constructor has no return
type. The second point is the most important difference as a method can also have the
same name as the class, which is not recommended: Code listing 4.4: [Link]

1 public class Cheese {


2 // This is a method with the same name as the class
3 public void Cheese() {
4 [Link](”A method execution.”);
5 }
6 }

The returned object is always a valid, meaningful object, as opposed to relying on a


separate initialization method. A constructor cannot be abstract, final, native, static,
strictfp nor synchronized. However, a constructor, like methods, can be overloaded and
take parameters. Code listing 4.5: [Link]

1 public class Cheese {


2 // This is a constructor
3 public Cheese() {
4 doStuff();
5 }
6
7 // This is another constructor
8 public Cheese(int weight) {
9 doStuff();
10 }
11
12 // This is yet another constructor
13 public Cheese(String type, int weight) {
14 doStuff();
15 }
16 }

By convention, a constructor that accepts an object of its own type as a parameter


and copies the data members is called a copy constructor. One interesting feature of
constructors is that if and only if you do not specify a constructor in your class, the
compiler will create one for you. This default constructor, if written out would look like:
Code listing 4.6: [Link]

265
Defining Classes

1 public class Cheese {


2 public Cheese() {
3 super();
4 }
5}

The super() command calls the constructor of the superclass. If there is no explicit call
to super(...) or this(...), then the default superclass constructor super(); is called
before the body of the constructor is executed. That said, there are instances where you
need to add in the call manually. For example, if you write even one constructor, no matter
what parameters it takes, the compiler will not add a default constructor. The code listing
4.81 results in a runtime error: Code listing 4.7: [Link]

1 public class Cheese {


2 public Cheese(int weight, String type) {
3 doStuff();
4 }
5}

Code listing 4.8: [Link]

1 public class Mouse {


2 public void eatCheese() {
3 Cheese c = new Cheese(); // Oops, compile time error!
4 }
5}

This is something to keep in mind when extending existing classes. Either make a default
constructor, or make sure every class that inherits your class uses the correct constructor.

35.3 Initializers
Initializers are blocks of code that are executed at the same time as initializers for fields.

35.3.1 Static initializers


Static initializers are blocks of code that are executed at the same time as initializers for
static fields. Static field initializers and static initializers are executed in the order declared.
The static initialization is executed after the class is loaded.

1 Chapter 35.3.1 on page 266

266
Initializers

Example:
Code section 4.2: Static initializer.
1 static int count = 20;
2 static int[] squares;
3 static { // a static initializer
4 squares = new int[count];
5 for (int i = 0; i < count; i++)
6 squares[i] = i * i;
7 }
8 static int x = squares[5]; // x is assigned the value 25

35.3.2 Instance initializers


Instance initializers are blocks of code that are executed at the same time as initializers for
instance (non-static) fields. Instance field initializers and instance initializers are executed
in the order declared. Both instance initializers and instance field initializers are executed
during the invocation of a constructor. The initializers are executed immediately after the
superclass constructor and before the body of the constructor.

267
36 Inheritance

Inheritance is one of the most powerful mechanisms of the Object Oriented Programming1 .
It allows the reuse of the members of a class (called the superclass or the mother class) in
another class (called subclass, child class or the derived class) that inherits from it. This
way, classes can be built by successive inheritance.
In Java, this mechanism is enabled by the extends keyword. Example: Code listing 4.9:
[Link]

1 public class Vehicle {


2 public int speed;
3 public int numberOfSeats;
4}

Code listing 4.10: [Link]

1 public class Car extends Vehicle {


2 public Car() {
3 [Link] = 90;
4 [Link] = 5;
5 }
6}

In the Code listing 4.102 , the class Car inherits from Vehicle, which means that the at-
tributes speed and numberOfSeats are present in the class Car, whereas they are defined
in the class Vehicle. Also, the constructor defined in the class Car allows to initialize those
attributes. In Java, the inheritance mechanism allows to define a class hierarchy with all
the classes. Without explicit inheritance, a class implicitly inherits from the Object class.
This Object class is the root of the class hierarchy.
Some classes can't be inherited. Those classes are defined with the final keyword. For
instance, the Integer class can't have subclasses. It is called a final class.

36.1 The Object class


At the instantiating, the child class receives the features inherited from its superclass, which
also has received the features inherited from its own superclass and so on to the Object class.
This mechanism allows to define reusable global classes, whose user details the behavior in
the derived more specific classes.

1 [Link]
2 Chapter 36.2 on page 270

269
Inheritance

In Java, a class can only inherit from one class. Java does not allow you to create a subclass
from two classes, as that would require creating complicated rules to disambiguate fields
and methods inherited from multiple superclasses. If there is a need for Java to inherit from
multiple sources, the best option is through interfaces, described in the next chapter.

36.2 The super keyword


The super keyword allows access to the members of the superclass of a class, as you
can use this to access the members of the current class. Example: Code listing 4.11: [Link]

1 public class Plane extends Vehicle {


2 public Plane() {
3 super();
4 }
5}

In this example, the constructor of the Plane class calls the constructor of its superclass
Vehicle. You can only use super to access the members of the superclass inside the child
class. If you use it from another class, it accesses the superclass of the other class. This
keyword also allows you to explicitly access the members of the superclass, for instance,
in the case where there is a method with the same name in your class (overriding, ...).
Example : Code listing 4.12: [Link]

1 public class Vehicle {


2 // ...
3 public void run() throws Exception {
4 position += speed;
5 }
6}

Code listing 4.13: [Link]

1 public class Plane extends Vehicle {


2 // ...
3 public void run() throws Exception {
4 if (0 < height) {
5 throw new Exception(”A plane can't run in flight.”);
6 } else {
7 [Link]();
8 }
9 }
10 }

Question 4.1: Consider the following classes. Question 4.1: [Link]

1 public class Class1 {


2 public static final int CONSTANT_OF_CLASS_1 = 9;
3 public int myAttributeOfClass1 = 40;
4 public void myMethodOfClass1(int i) {
5 }
6}

270
The super keyword

Question 4.1: [Link]

1 public class Class2 extends Class1 {


2 public int myAttributeOfClass2 = 10;
3 public void myMethodOfClass2(int i) {
4 }
5}

Question 4.1: [Link]

1 public class Class3 {


2 public static final int CONSTANT_OF_CLASS_3 = 9;
3 public void myMethodOfClass3(int i) {
4 }
5}

Question 4.1: [Link]

1 public class Question1 extends Class2 {


2 public static final int CONSTANT = 2;
3 public int myAttribute = 20;
4 public void myMethod(int i) {
5 }
6}

List all the attributes and methods that can be accessed in the class Question1.
• CONSTANT_OF_CLASS_1
• myAttributeOfClass1
• myMethodOfClass1(int)
• myAttributeOfClass2
• myMethodOfClass2(int)
• CONSTANT
• myAttribute
• myMethod(int)
Question1 inherits from Class1 and Class2 but not from Class3.
See also the Object Oriented Programming3 book about the inheritance concept.

3 [Link]

271
37 Interfaces

An interface is an abstraction of class with no implementation details. For example,


[Link] is a standard interface in Java. You cannot instantiate an interface.
An interface is not a class but it is written the same way. The first difference is that you do
not use the class keyword but the interface keyword to define it. Then, there are fields
and methods you cannot define here:
• A field is always a constant: it is always public, static and final, even if you do not
mention it.
• A method must be public and abstract, but it is not required to write the public and
abstract keywords.
• Constructors are forbidden.
An interface represents a contract: Code listing 4.14: [Link]

1 public interface SimpleInterface {


2 public static final int CONSTANT1 = 1;
3 int method1(String parameter);
4}

You can see that the method1() method is abstract (unimplemented). To use an interface,
you have to define a class that implements it, using the implements keyword: Code listing
4.15: [Link]

1 public class ClassWithInterface implements SimpleInterface {


2 public int method1(String parameter) {
3 return 0;
4 }
5}

A class can implement several interfaces, separated by a comma. Java interfaces behave
much like the concept of the Objective-C1 protocol. It is recommended to name an interface
<verb>able, to mean the type of action this interface would enable on a class. However, it
is not recommended to start the name of an interface by I as in C++2 . It is useless. Your
IDE will help you instead.

37.1 Interest
If you have objects from different classes that do not have a common superclass, you can't
call the same method in those classes, even if the two classes implement a method with the

1 [Link]
2 [Link]

273
Interfaces

same signature. Code listing 4.16: [Link]

1 public class OneClass {


2 public int method1(String parameter) {
3 return 1;
4 }
5}

Code listing 4.17: [Link]

1 public class AnotherClass {


2 public int method1(String parameter) {
3 return 2;
4 }
5}

1 Warning
Code section 4.16: Impossible call.
1 public static void main(String[] args) {
2 doAction(new OneClass());
3 doAction(new AnotherClass());
4 }
5
6 public void doAction(Object anObject) {
7 anObject.method1("Hello!");
8 }

The solution is to write an interface that defines the method that should be implemented
in the two classes as the SimpleInterface in the Code listing 4.143 and then both classes
can implement the interface as in the Code listing 4.154 .

Example:
Code section 4.17: Interface use.
1 public static void main(String[] args) {
2 doAction(new ClassWithInterface());
3 doAction(new AnotherClassWithInterface());
4 }
5
6 public void doAction(SimpleInterface anObject) {
7 anObject.method1("Hello!");
8 }

You can also implement this using a common super class but a class can only inherit from
one super class whereas it can implement several interfaces.

3 Chapter 37 on page 273


4 Chapter 37 on page 273

274
Extending interfaces

Java does not support full orthogonal multiple inheritance (i.e. Java does not allow you to
create a subclass from two classes). Multiple inheritance in C++5 has complicated rules
to disambiguate fields and methods inherited from multiple superclasses and types that
are inherited multiple times. By separating interface from implementation, interfaces offer
much of the benefit of multiple inheritance with less complexity and ambiguity. The price of
no multiple inheritance is some code redundancy; since interfaces only define the signature
of a class but cannot contain any implementation, every class inheriting an interface must
provide the implementation of the defined methods, unlike in pure multiple inheritance,
where the implementation is also inherited. The major benefit of that is that all Java
objects can have a common ancestor (a class called Object).
When overriding methods defined in interfaces there are several rules to be followed:
• Checked exceptions should not be declared on implementation methods other than the
ones declared by the interface method or subclasses of those declared by the interface
method.
• The signature of the interface method and the same return type or subtype should be
maintained when implementing the methods.
• All the methods of the interface need to be defined in the class, unless the class that
implements the interface is abstract.

37.2 Extending interfaces


An interface can extend several interfaces, similar to the way that a class can extend
another class, using the extends keyword: Code listing 4.18: [Link]

1 public interface InterfaceA {


2 public void methodA();
3}

Code listing 4.19: [Link]

1 public interface InterfaceB {


2 public void methodB();
3}

Code listing 4.20: [Link]

1 public interface InterfaceAB extends InterfaceA, InterfaceB {


2 public void otherMethod();
3}

This way, a class implementing the InterfaceAB interface has to implement the methodA()
, the methodB() and the otherMethod() methods: Code listing 4.21: [Link]

5 [Link]

275
Interfaces

1 public class ClassAB implements InterfaceAB {


2 public void methodA() {
3 [Link](”A”);
4 }
5
6 public void methodB() {
7 [Link](”B”);
8 }
9
10 public void otherMethod() {
11 [Link](”foo”);
12 }
13
14 public static void main(String[] args) {
15 ClassAB classAb = new ClassAB();
16 [Link]();
17 [Link]();
18 [Link]();
19 }
20 }

Doing so, a ClassAB object can be casted into InterfaceA, InterfaceB and InterfaceAB
. Question 4.2: Consider the following interfaces. Question 4.2: [Link]

1 public interface Walkable {


2 void walk();
3}

Question 4.2: [Link]

1 public interface Jumpable {


2 void jump();
3}

Question 4.2: [Link]

1 public interface Swimable {


2 void swim();
3}

Question 4.2: [Link]

1 public interface Movable extends Walkable, Jumpable {


2}

List all the methods that an implementing class of Movable should implement.
• walk()
• jump()
Answer 4.2: [Link]

276
Extending interfaces

1 public class Person implements Movable {


2 public void walk() {
3 [Link](”Do something.”);
4 }
5
6 public void jump() {
7 [Link](”Do something.”);
8 }
9}

Question 4.3: Consider the following classes and the following code. Question 4.3:
[Link]

1 import [Link];
2
3 public class ConsoleLogger {
4 public void printLog(String log) {
5 [Link](new Date() + ”: ” + log);
6 }
7}

Question 4.3: [Link]

1 import [Link];
2 import [Link];
3
4 public class FileLogger {
5 public void printLog(String log) {
6 try {
7 File file = new File(”[Link]”);
8 FileOutputStream stream = new FileOutputStream(file);
9 byte[] logInBytes = (new Date() + ”: ” + log).getBytes();
10
11 [Link](logInBytes);
12
13 [Link]();
14 [Link]();
15 } catch (Exception e) {
16 [Link]();
17 }
18 }
19 }

Example:
Question 4.3: Common code.
1 Object[] loggerArray = new Object[2];
2 loggerArray[0] = new ConsoleLogger();
3 loggerArray[1] = new FileLogger();
4
5 for (Object logger : loggerArray) {
6 // [Link]("Check point.");
7 }

Change the implementation of the code in order to be able to uncomment the commented
line without compile error. You have to create an interface that defines the method
printLog(String) and makes ConsoleLogger and FileLogger implement it: Answer 4.3:

277
Interfaces

[Link]

1 public interface Logger {


2 void printLog(String log);
3}

Answer 4.3: [Link]

1 import [Link];
2
3 public class ConsoleLogger implements Logger {
4 public void printLog(String log) {
5 [Link](new Date() + ”: ” + log);
6 }
7}

Answer 4.3: [Link]

1 import [Link];
2 import [Link];
3
4 public class FileLogger implements Logger {
5 public void printLog(String log) {
6 try {
7 File file = new File(”[Link]”);
8 FileOutputStream stream = new FileOutputStream(file);
9 byte[] logInBytes = (new Date() + ”: ” + log).getBytes();
10
11 [Link](logInBytes);
12
13 [Link]();
14 [Link]();
15 } catch (Exception e) {
16 [Link]();
17 }
18 }
19 }

Now your code has to cast the objects to the Logger type and then you can uncomment
the code.

Example:
Answer 4.3: Common code.
1 Logger[] loggerArray = new Logger[2];
2 loggerArray[0] = new ConsoleLogger();
3 loggerArray[1] = new FileLogger();
4
5 for (Logger logger : loggerArray) {
6 [Link]("Check point.");
7 }

278
38 Overloading Methods and
Constructors

38.1 Method overloading


In a class, there can be several methods with the same name. However they must have a
different signature. The signature of a method is comprised of its name, its parameter types
and the order of its parameters. The signature of a method is not comprised of its return
type nor its visibility nor the exceptions it may throw. The practice of defining two or more
methods within the same class that share the same name but have different parameters is
called overloading methods.
Methods with the same name in a class are called overloaded methods. Overloading methods
offers no specific benefit to the JVM but it is useful to the programmer to have several
methods do the same things but with different parameters. For example, we may have the
operation runAroundThe represented as two methods with the same name, but different
input parameter types:

Example:
Code section 4.22: Method overloading.
1 public void runAroundThe(Building block) {
2 ...
3 }
4
5 public void runAroundThe(Park park) {
6 ...
7 }

One type can be the subclass of the other:

279
Overloading Methods and Constructors

Code listing 4.11: [Link] Console for Code listing 4.11


String
1 public class ClassName { Object
2
3 public static void sayClassName(Object aObject) {
4 [Link](”Object”);
5 }
6
7 public static void sayClassName(String aString) {
8 [Link](”String”);
9 }
10
11 public static void main(String[] args) {
12 String aString = new String();
13 sayClassName(aString);
14
15 Object aObject = new String();
16 sayClassName(aObject);
17 }
18 }

Although both methods would be fit to call the method with the String parameter, it is the
method with the nearest type that will be called instead. To be more accurate, it will call
the method whose parameter type is a subclass of the parameter type of the other method.
So, aObject will output Object. Beware! The parameter type is defined by the declared
type of an object, not its instantiated type!
The following two method definitions are valid

Example:
Code section 4.23: Method overloading with the type order.
1 public void logIt(String param, Error err) {
2 ...
3 }
4
5 public void logIt(Error err, String param) {
6 ...
7 }

because the type order is different. If both input parameters were type String, that would
be a problem since the compiler would not be able to distinguish between the two:

1 Warning
Code section 4.24: Bad method overloading.
1 public void logIt(String param, String err) {
2 ...
3 }
4
5 public void logIt(String err, String param) {
6 ...
7 }

The compiler would give an error for the following method definitions as well:

280
Method overloading

1 Warning
Code section 4.25: Another bad method overloading.
1 public void logIt(String param) {
2 ...
3 }
4
5 public String logIt(String param) {
6 String retValue;
7 ...
8 return retValue;
9 }

Note, the return type is not part of the unique signature. Why not? The reason is that a
method can be called without assigning its return value to a variable. This feature came
from C and C++. So for the call:

1 Warning
Code section 4.26: Ambiguous method call.
1 logIt(msg);

the compiler would not know which method to call. It is also the case for the thrown
exceptions.
Question 4.6: Which methods of the Question6 class will cause compile errors?
[Link]

1 public class Question6 {


2
3 public void example1() {
4 }
5
6 public int example1() {
7 }
8
9 public void example2(int x) {
10 }
11
12 public void example2(int y) {
13 }
14
15 private void example3() {
16 }
17
18 public void example3() {
19 }
20
21 public String example4(int x) {
22 return null;
23 }
24
25 public String example4() {
26 return null;

281
Overloading Methods and Constructors

27 }
28 }

[Link]

1 public class Question6 {


2
3 public void example1() {
4 }
5
6 public int example1() {
7 }
8
9 public void example2(int x) {
10 }
11
12 public void example2(int y) {
13 }
14
15 private void example3() {
16 }
17
18 public void example3() {
19 }
20
21 public String example4(int x) {
22 return null;
23 }
24
25 public String example4() {
26 return null;
27 }
28 }

The example1, example2 and example3 methods will cause compile errors. The example1
methods cannot co-exist because they have the same signature (remember, return type
is not part of the signature). The example2 methods cannot co-exist because the names
of the parameters are not part of the signature. The example3 methods cannot co-exist
because the visibility of the methods are not part of the signature. The example4 methods
can co-exist, because they have different method signatures.

38.2 Variable Argument


Instead of overloading, you can use a dynamic number of arguments. After the last param-
eter, you can pass optional unlimited parameters of the same type. These parameters are
defined by adding a last parameter and adding ... after its type. The dynamic arguments
will be received as an array:

282
Constructor overloading

Example:
Code section 4.27: Variable argument.
1 public void registerPersonInAgenda(String firstName, String lastName,
Date...
2 meeting) {
3 String[] person = {firstName, lastName};
4 lastPosition = lastPosition + 1;
5 contactArray[lastPosition] = person;
6
7 if ([Link] > 0) {
8 Date[] temporaryMeetings = new Date[[Link] +
9 [Link]];
10 for (i = 0; i < [Link]; i++) {
11 temporaryMeetings[i] = registeredMeetings[i];
12 }
13 for (i = 0; i < [Link]; i++) {
14 temporaryMeetings[[Link] + i] = meeting[i];
15 }
16 registeredMeetings = temporaryMeetings;
17 }
18 }

The above method can be called with a dynamic number of arguments, for example:

Example:
Code section 4.27: Constructor calls.
1 registerPersonInAgenda("John", "Doe");
2 registerPersonInAgenda("Mark", "Lee", new Date(), new Date());

This feature was not available before Java 1.5 .

38.3 Constructor overloading


The constructor can be overloaded. You can define more than one constructor with different
parameters. For example:
Code listing 4.12: Constructors.

1 public class MyClass {


2
3 private String memberField;
4
5 /**
6 * MyClass Constructor, there is no input parameter
7 */
8 public MyClass() {
9 ...
10 }
11
12 /**
13 * MyClass Constructor, there is one input parameter
14 */
15 public MyClass(String param1) {
16 memberField = param1;
17 ...

283
Overloading Methods and Constructors

18 }
19 }

In the code listing 4.121 , we defined two constructors, one with no input parameter, and
one with one input parameter. You may ask which constructor will be called. Its depends
how the object is created with the new keyword. See below:

Example:
Code section 4.29: Constructor calls.
1 // The constructor with no input parameter will be called
2 MyClass obj1 = new MyClass();
3
4 // The constructor with one input param. will be called
5 MyClass obj2 = new MyClass("Init Value");

In the code section 4.292 , we created two objects from the same class, or we can also say
that obj1 and obj2 both have the same type. The difference between the two is that in
the first one the memberField field is not initialized, in the second one that is initialized to
"Init Value". A constructor may also be called from another constructor, see below:
Code listing 4.13: Constructor pooling.

1 public class MyClass {


2
3 private String memberField;
4
5 /**
6 * MyClass Constructor, there is no input parameter
7 */
8 public MyClass() {
9 MyClass(”Default Value”);
10 }
11
12 /**
13 * MyClass Constructor, there is one input parameter
14 */
15 public MyClass(String param1) {
16 memberField = param1;
17 ...
18 }
19 }

In the code listing 4.133 , the constructor with no input parameter calls the other constructor
with the default initial value. This call must be the first instruction of a constructor or else
a compiler error will occur. The code gives an option to the user, to create the object with
the default value or create the object with a specified value. The first constructor could
have been written using the this keyword as well:

1 Chapter 38.2 on page 282


2 Chapter 38.2 on page 282
3 Chapter 38.2 on page 282

284
Method overriding

Example:
Code section 4.30: Another constructor pooling.
1 public MyClass() {
2 this("Default Value");
3 }

Such a call reduces the code repetition.

38.4 Method overriding


To easily remember what can be done in method overriding, keep in mind that all you can
do in an object of a class, you can also do in an object of a subclass, only the behavior can
change. A subclass should be covariant.
Although a method signature has to be unique inside a class, the same method signature
can be defined in different classes. If we define a method that exists in the super class
then we override the super class method. It is called method overriding. This is different
from method overloading. Method overloading happens with methods with the same name
but different signature. Method overriding happens with methods with the same name and
same signature between inherited classes.
The return type can cause the same problem we saw above. When we override a super class
method the return type also must be the same. If that is not the same, the compiler will
give you an error.
Beware! If a class declares two public methods with the same name, and a subclass over-
rides one of them, the subclass still inherits the other method. In this respect, the Java
programming language differs from C++.
Method overriding is related to dynamic linking, or runtime binding. In order for the Method
Overriding to work, the method call that is going to be called can not be determined at
compilation time. It will be decided at runtime, and will be looked up in a table.

Example:
Code section 4.31: Runtime binding.
1 MyClass obj;
2
3 if (new [Link]().get([Link].AM_PM) ==
4 [Link]) {
5 // Executed during a morning
6 obj = new SubOfMyClass();
7 } else {
8 // Executed during an afternoon
9 obj = new MyClass();
10 }
11
12 [Link]();

285
Overloading Methods and Constructors

In the code section 4.314 , the expression at line 3 is true if it is executed during a morning
and false if it is executed during an afternoon. Thus, the instance of obj will be a MyClass
or a SubOfMyClass depending on the execution time. So it is impossible to determine the
method address at compile time. Because the obj reference can point to an object and all
its sub objects, and that will be known only at runtime, a table is kept with all the possible
method addresses to be called. Do not confuse:

Example:
Code section 4.32: Declared type and instantiated type.
1 [Link](myParameter);

The implementation of this method is searched using the instantiated type of the called
object (obj) and the declared type of the parameter object (myParameter).
Also another rule is that when you do an override, the visibility of the new method that
overrides the super class method can not be reduced. The visibility can be increased,
however. So if the super class method visibility is public, the override method can not be
package, or private. An override method must throw the same exceptions as the super
class, or their subexceptions.
super references to the parent class (i.e. [Link]()). It can be used in a subclass
to access inherited methods that the subclass has overridden or inherited fields that the
subclass has hidden.

Note:
A common mistake is to think that if we can override methods, we could also override
member variables. This is not the case, as it is useless. You can not redefine a variable
that is private in the super class as such a variable is not visible.

4 Chapter 38.2 on page 282

286
39 Object Lifecycle

Before a Java object can be created the class byte code must be loaded from the file system
(with .class extension) to memory. This process of locating the byte code for a given class
name and converting that code into a Java class1 instance is known as class loading. There
is one class created for each type of Java class.
All objects in Java programs are created on heap memory. An object is created based on
its class. You can consider a class as a blueprint, template, or a description how to create
an object. When an object is created, memory is allocated to hold the object properties.
An object reference pointing to that memory location is also created. To use the object in
the future, that object reference has to be stored as a local variable or as an object member
variable.
The Java Virtual Machine (JVM) keeps track of the usage of object references. If there
are no more reference to the object, the object can not be used any more and becomes
garbage. After a while the heap memory will be full of unused objects. The JVM collects
those garbage objects and frees the memory they allocated, so the memory can be reused
again when a new object is created. See below a simple example:

Example:
Code section 4.30: Object creation.
1 {
2 // Create an object
3 MyObject obj = new MyObject();
4
5 // Use the object
6 [Link]();
7 }

The obj variable contains the object reference pointing to an object created from the
MyObject class. The obj object reference is in scope2 inside the { }. After the } the object
becomes garbage. Object references can be passed in to methods and can be returned from
methods.

39.1 Creating object with the new keyword


99% of new objects are created using the new keyword. Code listing 4.13: [Link]

1 [Link]
2 Chapter 39.5 on page 292

287
Object Lifecycle

1 public class MyProgram {


2
3 public static void main(String[] args) {
4 // Create 'MyObject' for the first time the application is started
5 MyObject obj = new MyObject();
6 }
7 }

When an object from the MyObject class is created for the first time, the JVM searches
the file system for the definition of the class, that is the Java byte code. The file has the
extension of *.class. The CLASSPATH environment variable contains locations where
Java classes are stored. The JVM is looking for the [Link] file. Depending on
which package the class belongs to, the package name will be translated to a directory path.
When the [Link] file is found, the JVM's class loader loads the class in mem-
ory, and creates a [Link].Class3 object. The JVM stores the code in memory, allocates
memory for the static variables, and executes any static initialize block. Memory is not
allocated for the object member variables at this point, memory will be allocated for them
when an instance of the class, an object, is created.
There is no limit on how many objects from the same class can be created. Code and
static variables are stored only once, no matter how many objects are created. Memory
is allocated for the object member variables when the object is created. Thus, the size of
an object is determined not by its code's size but by the memory it needs for its member
variables to be stored.

39.2 Creating object by cloning an object


Cloning is not automatically available to classes. There is some help though, as all Java
objects inherit the protected Object clone() method. This base method would allocate
the memory and do the bit by bit copying of the object's states.
You may ask why we need this clone method. Can't we create a constructor, pass in the
same object and do the copying variable by variable? An example would be (note that
accessing the private memberVar variable of obj is legal as this is in the same class): Code
listing 4.14: [Link]

1 public class MyObject {


2 private int memberVar;
3 ...
4 MyObject(MyObject obj) {
5 [Link] = [Link];
6 ...
7 }
8 ...
9 }

This method works but object creation with the new keyword is time-consuming. The
clone() method copies the whole object's memory in one operation and this is much faster

3 [Link]

288
Creating object by cloning an object

than using the new keyword and copying each variable so if you need to create lots of objects
with the same type, performance will be better if you create one object and clone new ones
from it. See below a factory method that will return a new object using cloning.

Example:
Code section 4.31: Object cloning.
1 HashTable cacheTemplate = new HashTable();
2 ...
3 /** Clone Customer object for performance reason */
4 public Customer createCustomerObject() {
5 // See if a template object exists in our cache
6 Customer template = [Link]("Customer");
7 if (template == null) {
8 // Create template
9 template = new Customer();
10 [Link]("Customer", template);
11 }
12 return [Link]();
13 }

Now, let's see how to make the Customer object cloneable.


1. First the Customer class needs to implement the Cloneable Interface.
2. Override and make the clone() method public, as that is protected in the Object
class.
3. Call the [Link]()method at the beginning of your clone method.
4. Override the clone() method in all the subclasses of Customer.
Code listing 4.15: [Link]

1 public class Customer implements Cloneable {


2 ...
3 public Object clone() throws CloneNotSupportedException {
4 Object obj = [Link]();
5
6 return obj;
7 }
8}

In the code listing 4.154 we used cloning for speed up object creation. Another use of
cloning could be to take a snapshot of an object that can change in time. Let's say we want
to store Customer objects in a collection, but we want to disassociate them from the 'live'
objects. So before adding the object, we clone them, so if the original object changes from
that point forward, the added object won't. Also let's say that the Customer object has a
reference to an Activity object that contains the customer activities. Now we are facing a
problem, it is not enough to clone the Customer object, we also need to clone the referenced
objects. The solution:
1. Make the Activity class also cloneable
2. Make sure that if the Activity class has other 'changeable' object references, those
have to be cloned as well, as seen below

4 Chapter 39.4.1 on page 291

289
Object Lifecycle

3. Change the Customer class clone() method as follows:


Code listing 4.16: [Link]

1 public class Customer implements Cloneable {


2 Activity activity;
3 ...
4 public Customer clone() throws CloneNotSupportedException {
5 Customer clonedCustomer = (Customer) [Link]();
6
7 // Clone the object referenced objects
8 if (activity != null) {
9 [Link]((Activity) [Link]());
10 }
11 return clonedCustomer;
12 }
13 }

Note that only mutable objects need to be cloned. References to unchangeable objects such
as a String can be used in the cloned object without worry.

39.3 Re-creating an object received from a remote source


When an object is sent through a network, the object needs to be re-created at the re-
ceiving host.
Object Serialization
The term Object Serialization refers to the act of converting the object to a byte stream.
The byte stream can be stored on the file system or can be sent through a network.
At a later time the object can be re-created from that stream of bytes. The only require-
ment is that the same class has to be available both times, when the object is serialized
and also when the object is re-created. If that happens on different servers, then the same
class must be available on both servers. Same class means that exactly the same version
of the class must be available, otherwise the object won't be able to be re-created. This
is a maintenance problem for those applications where java serialization is used to make
objects persistent or to sent the object through the network.
When a class is modified, there could be a problem re-creating those objects that were
serialized using an earlier version of the class.
Java has built-in support for serialization, using the Serializable interface; however, a
class must first implement the Serializable interface.
By default, a class will have all of its fields serialized when converted into a data stream
(with transient fields being skipped). If additional handling is required beyond the default
of writing all fields, you need to provide an implementation for the following three methods:
private void writeObject([Link] out) throws
IOException;
private void readObject([Link] in) throws IOException,
ClassNotFoundException;

290
Destroying objects

private void readObjectNoData() throws ObjectStreamException;


If the object needs to write or provide a replacement object during serialization, it needs to
implement the following two methods, with any access specifier:
Object writeReplace() throws ObjectStreamException;
Object readResolve() throws ObjectStreamException;
Normally, a minor change to the class can cause the serialization to fail. You can still allow
the class to be loaded by defining the serialization version id:

Example:
Code section 4.32: Serialization version id.
1 private static final long serialVersionUID = 42L;

39.4 Destroying objects


Unlike in many other object-oriented programming languages, Java performs automatic
garbage collection — any unreferenced objects are automatically erased from memory —
and prohibits the user from manually destroying objects.

39.4.1 finalize()
When an object is garbage-collected, the programmer may want to manually perform
cleanup, such as closing any open input/output streams. To accomplish this, the
finalize() method is used. Note that finalize() should never be manually called, ex-
cept to call a super class' finalize method from a derived class' finalize method. Also, we
can not rely on when the finalize() method will be called. If the java application exits
before the object is garbage-collected, the finalize() method may never be called.

Example:
Code section 4.33: Finalization.
1 protected void finalize() throws Throwable {
2 try {
3 doCleanup(); // Perform some cleanup. If it fails for some reason,
4 it is ignored.
5 } finally {
6 [Link](); // Call finalize on the parent object
7 }
8 }

The garbage-collector thread runs in a lower priority than the other threads. If the applica-
tion creates objects faster than the garbage-collector can claim back memory, the program
can run out of memory.
The finalize method is required only if there are resources beyond the direct control of
the Java Virtual Machine that needs to be cleaned up. In particular, there is no need
to explicitly close an OutputStream, since the OutputStream will close itself when it gets

291
Object Lifecycle

finalized. Instead, the finalize method is used to release either native or remote resources
controlled by the class.

39.5 Class loading


One of the main concerns of a developer writing hot re-deployable applications is to under-
stand how class loading works. Within the internals of the class loading mechanism lies the
answer to questions like:
• What happens if I pack a newer version of an utility library with my application, while
an older version of the same library lingers somewhere in the server's lib directory?
• How can I use two different versions of the same utility library, simultaneously, within
the same instance of the application server?
• What version of an utility class I am currently using?
• Why do I need to mess with all this class loading stuff anyway?

292
40 Scope

40.1 Scope
The scope of a class, a variable or a method is its visibility and its accessibility. The visibility
or accessibility means that you can use the item from a given place.

40.1.1 Scope of method parameters


A method parameter is visible inside of the entire method but not visible outside the method.
Code listing 3.14: [Link]

1 public class Scope {


2
3 public void method1(int i) {
4 i = i++;
5 method2();
6 int j = i * 2;
7 }
8
9 public void method2() {
10 int k = 20;
11 }
12
13 public static void main(String[] args) {
14 method1(10);
15 }
16 }

In code listing 3.141 , i is visible within the entire method1 method but not in the method2
and the main methods.

40.1.2 Scope of local variables


A local variable is visible after its declaration until the end of the block in which the local
variable has been created.

1 Chapter 40.3 on page 295

293
Scope

Example:
Code section 3.50: Local variables.
1 {
2 ...
3 // myNumber is NOT visible
4 {
5 // myNumber is NOT visible
6 int myNumber;
7 // myNumber is visible
8 {
9 ...
10 // myNumber is visible
11 }
12 // myNumber is visible
13 }
14 // myNumber is NOT visible
15 ...
16 }

40.2 Access modifiers


You surely would have noticed by now, the words public, protected and private at the
beginning of class's method declarations used in this book. These keywords are called the
access modifiers in the Java language syntax, and they define the scope of a given item.

40.2.1 For a class


• If a class has public visibility, the class can be referenced by anywhere in the program.
• If a class has protected visibility, the class can be referenced only in the package where
the class is defined.
• If a class has private visibility, (it can happen only if the class is defined nested in another
class) the class can be accessed only in the outer class.

40.2.2 For a variable


• If a variable is defined in a public class and it has public visibility, the variable can be
referenced anywhere in the application through the class it is defined in.
• If a variable has protected visibility, the variable can be referenced only in the sub-
classes and in the same package through the class it is defined in.
• If a variable has package visibility, the variable can be referenced only in the same
package through the class it is defined in.
• If a variable has private visibility, the variable can be accessed only in the class it is
defined in.

40.2.3 For a method


• If a method is defined in a public class and it has public visibility, the method can be
called anywhere in the application through the class it is defined in.

294
Utility

• If a method has protected visibility, the method can be called only in the sub-classes
and in the same package through the class it is defined in.
• If a method has package visibility, the method can be called only in the same package
through the class it is defined in.
• If a method has private visibility, the method can be called only in the class it is defined
in.

40.2.4 For an interface


The interface methods and interfaces are always public. You do not need to specify the
access modifier. It will default to public. For clarity it is considered a good practice to put
the public keyword.
The same way all member variables defined in the Interface by default will become static
final once inherited in a class.

40.2.5 Summary
Class Nested class Method, or Member Interface Interface method
variable signature
public visible from anywhere same as its class same as its class visible from anywhere visible from anywhere
protected N/A its class and its subclass its class and its subclass, N/A N/A
and from its package
package only from its package only from its package only from its package N/A N/A
private N/A only from its class only from its class N/A N/A

The cases in bold are the default.

40.3 Utility
A general guideline for visibilities is to only make a member as visible as it needs to be.
Don't make a member public if it only needs to be private.
Doing so, you can rewrite a class and change all the private members without making
compilation errors, even you don't know all the classes that will use your class as long as
you do not change the signature of the public members.

40.4 Field encapsulation


Generally, it is best to make data private or protected. Access to the data is controlled
by setter and getter methods. This lets the programmer control access to data, allowing
him/her to check for and handle invalid data.

295
Scope

Example:
Code section 3.51: Encapsulation.
1 private String name;
2
3 /**
4 * This is a getter method because it accesses data from the object.
5 */
6 public String getName() {
7 return name;
8 }
9
10 /**
11 * This is a setter method because it changes data in the object.
12 */
13 public boolean setName(String newName) {
14 if (newName == null) {
15 return false;
16 } else {
17 name = newName;
18 return true;
19 }
20 }

In the code section 3.512 , the setName() method will only change the value of name if the
new name is not null. Because setName() is conditionally changing name, it is wise to
return a boolean to let the program know if the change was successful.
Question 3.15: Consider the following class.
Question 3.15: [Link]

1 public class Question15 {


2
3 public static final int QKQKQKQK_MULTIPLIER = 2;
4
5 public int ijijijijijijijijijAwfulName = 20;
6
7 private int unununununununununCrummyName = 10;
8
9 private void mememememememeUglyName(int i) {
10 i = i++;
11 tltltltltltltltltlBadName();
12 int j = i * QKQKQKQK_MULTIPLIER;
13 }
14
15 public void tltltltltltltltltlBadName() {
16 int k = ijijijijijijijijijAwfulName;
17 }
18
19 public static void main(String[] args) {
20 mememememememeUglyName(unununununununununCrummyName);
21 }
22 }

List the fields and methods of this class that can be renamed without changing or even
knowing the client classes.
1. unununununununununCrummyName

2 Chapter 40.3 on page 295

296
Field encapsulation

2. mememememememeUglyName()
Every field or method that is public can be directly called by a client class so this class
would return a compile error if the field or the method has a new name.

297
41 Nested Classes

In Java you can define a class inside another class. A class can be nested inside another
class or inside a method. A class that is not nested is called a top-level class and a class
defining a nested class is an outer class.

41.1 Inner classes


41.1.1 Nesting a class inside a class
When a class is declared inside another class, the nested class' access modifier can be
public, private, protected or package(default). Code listing 4.10: [Link]

1 public class OuterClass {


2 private String outerInstanceVar;
3
4 public class InnerClass {
5 public void printVars() {
6 [Link](”Print Outer Class Instance Var.:” +
7 outerInstanceVar);
8 }
9 }
10 }

The inner class has access to the enclosing class instance's variables and methods, even
private ones, as seen above. This makes it very different from the nested class in C++,
which are equivalent to the ”static” inner classes, see below.
An inner object has a reference to the outer object. In other words, all inner objects are
tied to the outer object. The inner object can only be created through a reference to the
'outer' object. See below.

Example:
Code section 4.20: Outer class call.
1 public void testInner() {
2 ...
3 OuterClass outer = new OuterClass();
4 [Link] inner = [Link] InnerClass();
5 ...
6 }

Note that inner objects, because they are tied to the outer object, cannot contain static
variables or methods.
When in a non-static method of the outer class, you can directly use new InnerClass(),
since the class instance is implied to be this.

299
Nested Classes

You can directly access the reference to the outer object from within an inner class with the
syntax [Link]; although this is usually unnecessary because you already have
access to its fields and methods.
Inner classes compile to separate ”.class” bytecode files, with the name of the enclosing class,
followed by a ”$”, followed by the name of the inner class. So for example, the above inner
class would be compiled to a file named ”OuterClass$[Link]”.

41.1.2 Static nested classes


A nested class can be declared static. These classes are not bound to an instance of the
outer defining class. A static nested class has no enclosing instance, and therefore cannot
access instance variables and methods of the outer class. You do not specify an instance
when creating a static inner class. This is equivalent to the inner classes in C++.

41.1.3 Nesting a class inside a method


These inner classes, also called local classes, cannot have access modifiers, like local
variables, since the class is 'private' to the method. The inner class can be only abstract
or final. Code listing 4.11: [Link]

1 public class OuterClass {


2 public void method() {
3 class InnerClass {
4
5 }
6 }
7}

In addition to instance variables of the enclosing class, local classes can also access local
variables of the enclosing method, but only ones that are declared final. This is because
the local class instance might outlive the invocation of the method, and so needs its own
copy of the variable. To avoid problems with having two different copies of a mutable
variable with the same name in the same scope, it is required to be final, so it cannot be
changed.

41.2 Anonymous Classes


In Java, a class definition and its instantiation can be combined into a single step. By doing
that the class does not require a name. Those classes are called anonymous classes. An
anonymous class can be defined and instantiated in contexts where a reference can be used,
and it is a nested class to an existing class. Anonymous class is a special case of a class
local to a method; hence they also can access final local variables of the enclosing method.
Anonymous classes are most useful to create an instance of an interface or adapter class
without needing a brand new class. Code listing 4.12: [Link]

300
Anonymous Classes

1 public interface ActionListener {


2 public void actionPerformed();
3}

Example:
Code section 4.21: Anonymous class.
1 ActionListener listener = new ActionListener() {
2 public void actionPerformed() {
3 // Implementation of the action event
4 ...
5 return;
6 }
7 };

In the above example the class that implements the ActionListener is anonymous. The
class is defined where it is instantiated.
The above code is harder to read than if the class is explicitly defined, so why use it? If many
implementations are needed for an interface, those classes are used only in one particular
place, and it would be hard to come up with names for them, using an anonymous inner
class makes sense.
The following example uses an anonymous inner class to implement an action listener.
Code listing 4.13: [Link]

1 import [Link];
2 import [Link];
3 import [Link];
4
5 class MyApp {
6 Button aButton = new Button();
7
8 MyApp() {
9 [Link](new ActionListener() {
10 public void actionPerformed(ActionEvent e) {
11 [Link](”Hello There”);
12 }
13 }
14 );
15 }
16 }

The following example does the same thing, but it names the class that implements the
action listener. Code listing 4.14: [Link]

1 import [Link];
2 import [Link];
3 import [Link];
4
5 class MyApp {
6 Button aButton = new Button();
7
8 // Nested class to implement the action listener
9 class MyActionListener implements ActionListener {
10 public void actionPerformed(ActionEvent e) {
11 [Link](”Hello There”);
12 }

301
Nested Classes

13 }
14 MyApp() {
15 [Link](new MyActionListener());
16 }
17 }

Using anonymous classes is especially preferable when you intend to use many different
classes that each implement the same interface.

302
42 Generics
Java is a strongly typed language, so a field in a class may be typed like this: Code listing
4.34: [Link]

1 public class Repository {


2
3 public Integer item;
4
5 public Integer getItem() {
6 return item;
7 }
8
9 public void setItem(Integer newItem) {
10 item = newItem;
11 }
12 }

This ensures that, only Integer objects can be put in the field and a ClassCastException
can't occur at runtime, only compile-time error can occur. Unfortunately, it can be used
only with Integer objects. If you want to use the same class in another context with
Strings, you have to generalize the type like this: Code listing 4.35: [Link]

1 public class Repository {


2
3 public Object item;
4
5 public Object getItem() {
6 return item;
7 }
8
9 public void setItem(Integer newItem) {
10 item = newItem;
11 }
12
13 public void setItem(String newItem) {
14 item = newItem;
15 }
16 }

But you will have ClassCastException at runtime again and you can't easily use your
field. The solution is to use Generics1 .

42.1 Generic class


A generic class does not hard code the type of a field, a return value or a parameter. The
class only indicates that a generic type should be the same, for a given object instance.
The generic type is not specified in the class definition. It is specified during object

1 [Link]

303
Generics

instantiation. This allows the generic type to be different from an instance to another. So
we should write our class this way: Code listing 4.36: [Link]

1 public class Repository<T> {


2
3 public T item;
4
5 public T getItem() {
6 return item;
7 }
8
9 public void setItem(T newItem) {
10 item = newItem;
11 }
12 }

Here, the generic type is defined after the name of the class. Any new identifier can be
chosen. Here, we have chosen T, which is the most common choice. The actual type is
defined at the object instantiation:

Example:
Code section 4.35: Instantiation.
1 Repository<Integer> arithmeticRepository = new Repository<Integer>();
2 [Link](new Integer(1));
3 Integer number = [Link]();
4
5 Repository<String> textualRepository = new Repository<String>();
6 [Link]("Hello!");
7 String message = [Link]();

Although each object instance has its own type, each object instance is still strongly typed:

1 Warning
Code section 4.36: Compile error.
1 Repository<Integer> arithmeticRepository = new Repository<Integer>();
2 [Link]("Hello!");

A class can define as many generic types as you like. Choose a different identifier for each
generic type and separate them by a comma:
Code listing 4.37: [Link]

1 public class Repository<T, U> {


2
3 public T item;
4
5 public U anotherItem;
6
7 public T getItem() {
8 return item;
9 }
10
11 public void setItem(T newItem) {

304
Generic method

12 item = newItem;
13 }
14
15 public U getAnotherItem() {
16 return anotherItem;
17 }
18
19 public void setAnotherItem(U newItem) {
20 anotherItem = newItem;
21 }
22 }

When a type that is defined with generic (for example, Collection<T>) is not used with
generics (for example, Collection) is called a raw type.

42.2 Generic method


A generic type can be defined for just a method:

Example:
Code section 4.37: Generic method.
1 public <D> D assign(Collection<D> generic, D obj) {
2 [Link](obj);
3 return obj;
4 }

Here a new identifier (D) has been chosen at the beginning of the method declaration.
The type is specific to a method call and different types can be used for the same object
instance:

Example:
Code section 4.38: Generic method call.
1 Collection<Integer> numbers = new ArrayList<Integer>();
2 Integer number = assign(numbers, new Integer(1));
3 Collection<String> texts = new ArrayList<String>();
4 String text = assign(texts, "Store it.");

The actual type will be defined by the type of the method parameter. Hence, the generic
type can't be defined only for the return value as it wouldn't be resolved. See the
Class<T>2 section for a solution. Question 4.8: Consider the following class. Question
4.8: [Link]

1 public class Question8<T> {


2 public T item;
3
4 public T getItem() {
5 return item;
6 }
7

2 Chapter 42.4 on page 309

305
Generics

8 public void setItem(T newItem) {


9 item = newItem;
10 }
11
12 public static void main(String[] args) {
13 Question8<String> aQuestion = new Question8<String>();
14 [Link](”Open your mind.”);
15 [Link]([Link]());
16 }
17
18 public void display(String parameter) {
19 [Link](”Here is the text: ” + parameter);
20 }
21
22 public void display(Integer parameter) {
23 [Link](”Here is the number: ” + parameter);
24 }
25
26 public void display(Object parameter) {
27 [Link](”Here is the object: ” + parameter);
28 }
29 }

What will be displayed on the console?

Console:
Console for Answer 4.8
Here is the text: Open your mind.

[Link]() is typed as a string.

42.3 Wildcard Types


As we have seen above, generics give the impression that a new container type is created
with each different type parameter. We have also seen that in addition to the normal type
checking, the type parameter has to match as well when we assign generics variables. In
some cases this is too restrictive. What if we would like to relax this additional checking?
What if we would like to define a collection variable that can hold any generic collection,
regardless of the parameter type it holds? The wildcard type is represented by the character
<?>, and pronounced Unknown, or Any-Type. Any-Type can be expressed also by <?
extends Object>. Any-Type includes Interfaces, not only Classes. So now we can define
a collection whose element type matches anything. See below:

Example:
Code section 4.39: Wildcard type.
1 Collection<?> collUnknown;

306
Wildcard Types

42.3.1 Upper bounded wildcards


You can specify a restriction on the types of classes that may be used. For example, <?
extends ClassName> only allows objects of class ClassName or a subclass. For example,
to create a collection that may only contain ”Serializable” objects, specify:

Example:
Code section 4.40: Collection of serializable subobjects.
1 Collection<String> textColl = new ArrayList<String>();
2
3 Collection<? extends Serializable> serColl = textColl;

The above code is valid because the String class is serializable. Use of a class that is
not serializable would cause a compilation error. The added items can be retrieved as
Serializable object. You can call methods of the Serializable interface or cast it to
String. The following collection can only contain objects that extend the class Animal.
Code listing 4.38: [Link]

1 class Dog extends Animal {


2}

Example:
Code section 4.41: Example of subclass.
1 // Create "Animal Collection" variable
2 Collection<? extends Animal> animalColl = new ArrayList<Dog>();

42.3.2 Lower bounded wildcards


<? super ClassName> specifies a restriction on the types of classes that may be used. For
example, to declare a Comparator that can compare Dogs, you use:

Example:
Code section 4.42: Superclass.
1 Comparator<? super Dog> myComparator;

Now suppose you define a comparator that can compare Animals:

Example:
Code section 4.43: Comparator.
1 class AnimalComparator implements Comparator<Animal> {
2 int compare(Animal a, Animal b) {
3 //...
4 }
5 }

307
Generics

Since Dogs are Animals, you can use this comparator to compare Dogs also. Comparators
for any superclass of Dog can also compare Dog; but comparators for any strict subclass
cannot.

Example:
Code section 4.44: Generic comparator.
1 Comparator<Animal> myAnimalComparator = new AnimalComparator();
2
3 static int compareTwoDogs(Comparator<? super Dog> comp, Dog dog1, Dog dog2) {
4 return [Link](dog1, dog2);
5 }

The above code is valid because the Animal class is a supertype of the Dog class. Use of a
class that is not a supertype would cause a compilation error.

42.3.3 Unbounded wildcard


The advantage of the unbounded wildcard (i.e. <?>) compared to a raw type (i.e. without
generic) is to explicitly say that the parameterized type is unknown, not any type. That way,
all the operations that implies to know the type are forbidden to avoid unsafe operation.
Consider the following code:

Example:
Code section 4.45: Unsafe operation.
1 public void addAtBottom(Collection anyCollection) {
2 [Link](new Integer(1));
3 }

This code will compile but this code may corrupt the collection if the collection only contains
strings:

Example:
Code section 4.46: Corruption of list.
1 List<String> col = new ArrayList<String>();
2 addAtBottom(col);
3 [Link](0).endsWith(".");

Console:
Console for Code section 4.46
Exception in thread "main" [Link]: [Link]
incompatible with [Link]
at [Link]([Link])

This situation could have been avoided if the addAtBottom(Collection) method was de-
fined with an unbounded wildcard: addAtBottom(Collection<?>). With this signature,
it is impossible to compile a code that is dependent of the parameterized type. Only inde-
pendent methods of a collection (clear(), isEmpty(), iterator(), remove(Object o),

308
Class<T>

size(), ...) can be called. For instance, addAtBottom(Collection<?>) could contain the
following code:

Example:
Code section 4.47: Safe operation.
1 public void addAtBottom(Collection<?> anyCollection) {
2 Iterator<?> iterator = [Link]();
3 while ([Link]()) {
4 [Link]([Link]());
5 }
6 }

42.4 Class<T>
Since Java 1.5, the class [Link] is generic. It is an interesting example of using
generics for something other than a container class. For example, the type of [Link]
is Class<String>, and the type of [Link] is Class<Serializable>. This can
be used to improve the type safety of your reflection code. In particular, since the
newInstance() method in Class now returns T, you can get more precise types when creat-
ing objects reflectively. Now we can use the newInstance() method to return a new object
with exact type, without casting. An example with generics:

Example:
Code section 4.48: Automatic cast.
1 Customer cust = [Link]([Link]); // No casting
2 ...
3 public static <T> T createAnyObject(Class<T> cls) {
4 T ret = null;
5 try {
6 ret = [Link]();
7 } catch (Exception e) {
8 // Exception Handling
9 }
10 return ret;
11 }

The same code without generics:

309
Generics

Example:
Code section 4.49: Former version.
1 Customer cust = (Customer) [Link]([Link]); //
Casting
2 is needed
3 ...
4 public static Object createAnyObject(Class cls) {
5 Object ret = null;
6 try {
7 ret = [Link]();
8 } catch (Exception e) {
9 // Exception Handling
10 }
11 return ret;
12 }

42.5 Motivation
Java was long criticized for the need to explicitly type-cast an element when it was taken
out of a ”container/collection” class. There was no way to enforce that a ”collection” class
contains only one type of object (e.g., to forbid at compile time that an Integer object
is added to a Collection that should only contain Strings). This is possible since Java
1.5. In the first couple of years of Java evolution, Java did not have a real competitor.
This has changed by the appearance of Microsoft C#. With Generics Java is better suited
to compete against C#. Similar constructs to Java Generics exist in other languages, see
Generic programming3 for more information. Generics were added to the Java language
syntax in version 1.5. This means that code using Generics will not compile with Java 1.4
and less. Use of generics is optional. For backwards compatibility with pre-Generics code,
it is okay to use generic classes without the generics type specification (<T>). In such a case,
when you retrieve an object reference from a generic object, you will have to manually cast
it from type Object to the correct type.

42.6 Note for C++ programmers


Java Generics are similar to C++ Templates in that both were added for the same reason.
The syntax of Java Generic and C++ Template are also similar. There are some differences
however. The C++ template can be seen as a kind of macro, in that a new copy of the
code is generated for each generic type referenced. All extra code for templates is generated
at compiler time. In contrast, Java Generics are built into the language. The same code is
used for each generic type. For example:

Example:
Code section 4.50: Java generics.
1 Collection<String> collString = new ArrayList<String>();
2 Collection<Integer> collInteger = new ArrayList<Integer>();

3 [Link]

310
Note for C++ programmers

Both these objects appear as the same type at runtime (both ArrayList's). The generic
type information is erased during compilation (type erasure). For example:

Example:
Code section 4.51: Type erasure.
1 public <T> void method(T argument) {
2 T variable;
3 …
4 }

is transformed by erasure into:

Example:
Code section 4.52: Transformation.
1 public void method(Object argument) {
2 Object variable;
3 …
4 }

Question 4.9: Consider the following class.


Question 4.9: [Link]

1 import [Link];
2 import [Link];
3
4 public class Question9 {
5 public static void main(String[] args) {
6 Collection<String> collection1 = new ArrayList<String>();
7 Collection<? extends Object> collection2 = new ArrayList<String>();
8 Collection<? extends String> collection3 = new ArrayList<String>();
9 Collection<? extends String> collection4 = new ArrayList<Object>();
10 Collection<? super Object> collection5 = new ArrayList<String>();
11 Collection<? super Object> collection6 = new ArrayList<Object>();
12 Collection<?> collection7 = new ArrayList<String>();
13 Collection<? extends Object> collection8 = new ArrayList<?>();
14 Collection<? extends Object> collection9 = new ArrayList<Object>();
15 Collection<? extends Integer> collection10 = new ArrayList<String>();
16 Collection<String> collection11 = new ArrayList<? extends String>();
17 Collection collection12 = new ArrayList<String>();
18 }
19 }

Which lines will generate a compile error?


Answer 4.9: [Link]

1 import [Link];
2 import [Link];
3
4 public class Answer9 {
5 public static void main(String[] args) {
6 Collection<String> collection1 = new ArrayList<String>();
7 Collection<? extends Object> collection2 = new ArrayList<String>();
8 Collection<? extends String> collection3 = new ArrayList<String>();
9 Collection<? extends String> collection4 = new ArrayList<Object>();
10 Collection<? super Object> collection5 = new ArrayList<String>();

311
Generics

11 Collection<? super Object> collection6 = new ArrayList<Object>();


12 Collection<?> collection7 = new ArrayList<String>();
13 Collection<? extends Object> collection8 = new ArrayList<?>();
14 Collection<? extends Object> collection9 = new ArrayList<Object>();
15 Collection<? extends Integer> collection10 = new ArrayList<String>();
16 Collection<String> collection11 = new ArrayList<? extends String>();
17 Collection collection12 = new ArrayList<String>();
18 }
19 }

• Line 9: Object does not extend String.


• Line 10: String is not a superclass of Object.
• Line 13: ArrayList<?> can't be instantiated.
• Line 15: Integer does not extend String.
• Line 16: ArrayList<? extends String> can't be instantiated.

312
43 Contributors

Edits User
1 1997kB1
1 94rain2
29 A.T.M.Schipperijn3
1 AGMS4
1 Abdull5
2 Abhijaju6
1 AdRiley7
36 Adrignola8
1 Akumm2k9
3 Alainr34510
3 Albmont11
1 Alexander.orlov12
1 Alextejthompson13
1 Alexy1014
2 AllenZh15
1 Alsocal16
1 Amokk.zgz17
1 Andrybak18
1 Anjishnu1519
1 Antidrugue20

1 [Link]
2 [Link]
[Link]
3
redlink=1
4 [Link]
5 [Link]
6 [Link]
7 [Link]
8 [Link]
9 [Link]
10 [Link]
11 [Link]
[Link]
12
redlink=1
13 [Link]
14 [Link]
15 [Link]
16 [Link]
17 [Link]
18 [Link]
19 [Link]
20 [Link]

313
Contributors

1 Aphonik~enwikibooks21
1 Apocalyte22
1 Arsenalfan23
240 Arunreginald24
7 Ashmailit25
4 Atcovi26
6 Audriusa27
1 Avicennasis28
2 Az156829
1 Badgecrimson30
6 BenTels31
1 Beno100032
5 Bheilig33
1 Billy the Goat II34
1 Boaz Benzvi35
5 Brzlnboi36
1 Bxj37
1 CSB radio38
6 Callkalpa39
1 Case45140
1 Ceclauson41
1 Cg0def~enwikibooks42
1 Chaofan2543
1 Chazz44

[Link]
21
redlink=1
22 [Link]
23 [Link]
24 [Link]
25 [Link]
26 [Link]
27 [Link]
28 [Link]
29 [Link]
30 [Link]
31 [Link]
32 [Link]
33 [Link]
[Link]
34
redlink=1
35 [Link]
36 [Link]
37 [Link]
38 [Link]
39 [Link]
40 [Link]
41 [Link]
[Link]
42
redlink=1
43 [Link]
44 [Link]

314
Note for C++ programmers

2 Chiapr~enwikibooks45
1 Chornge46
1 Cleartrain47
8 Colfulus48
1 CommonsDelinker49
2 Corax2650
1 Crcarter51
1 Cshinx52
1 Cspurrier53
1 Cyoub54
3 Dallas127855
2 Dan Polansky56
1 DanCherek57
1 Danny8129958
43 DannyS71259
1 Darklama60
5 Darkxxxxillusion61
5 Davemulkey62
2 DavidBourguignon63
4 DavidCary64
1 DavidL65
1 Deanonwiki66
1 Deathlaser67
1 Derbeth68
1 Desb4269

[Link]
45
redlink=1
46 [Link]
47 [Link]
48 [Link]
49 [Link]
50 [Link]
51 [Link]
52 [Link]
53 [Link]
54 [Link]
55 [Link]
56 [Link]
57 [Link]
58 [Link]
59 [Link]
60 [Link]
[Link]
61
redlink=1
62 [Link]
[Link]
63
redlink=1
64 [Link]
65 [Link]
66 [Link]
67 [Link]
68 [Link]
69 [Link]

315
Contributors

1 Devourer0970
1 Dino Bronto Rex71
25 Dirk Hünniger72
3 Dirk gently~enwikibooks73
12 DisturbedNerd999~enwikibooks74
93 Djb75
1 Dmonego76
1 Dooyamind77
1 Drawde83~enwikibooks78
1 Dumbmoron201879
2 Dwightdda80
1 EG1681
1 Elton82
714 Ervinn83
1 Escortkeel84
3 Esquivalience85
1 Evj86
6 Exabyte~enwikibooks87
2 Explanator88
1 FJalchemist89
1 Federhalter90
3 Felipe Dachshund91
5 Felipeochoa091892
4 Fishpi93
1 Fkereki94

70 [Link]
71 [Link]
72 [Link]
[Link]
73
edit&redlink=1
74 [Link]
75 [Link]
76 [Link]
77 [Link]
78 [Link]
79 [Link]
80 [Link]
81 [Link]
82 [Link]
83 [Link]
84 [Link]
85 [Link]
86 [Link]
87 [Link]
88 [Link]
89 [Link]
90 [Link]
[Link]
91
redlink=1
[Link]
92
redlink=1
93 [Link]
94 [Link]

316
Note for C++ programmers

5 Forage95
2 Fraterm~enwikibooks96
2 Fredmaranhao97
238 Ftiercel98
3 Gandalfxviv99
1 Geekler~enwikibooks100
1 Gilward Kukel101
4 Glaisher102
2 GoLLoXp103
1 Good afternoon104
1 Greenbreen105
2 Grokus106
1 Grunny107
6 Guangpu.huang108
1 Gxwarr109
1 HMPerson1110
1 Harrybrowne1986111
6 Haukli112
6 Hbar~enwikibooks113
1 Hermione1980114
1 HethrirBot115
2 Hillowrym116
1 HotBBQ117
3 Hungariantoast118

95 [Link]
[Link]
96
redlink=1
97 [Link]
98 [Link]
99 [Link]
[Link]
100
redlink=1
101 [Link]
102 [Link]
103 [Link]
104 [Link]
105 [Link]
106 [Link]
107 [Link]
108 [Link]
109 [Link]
110 [Link]
111 [Link]
112 [Link]
[Link]
113
redlink=1
114 [Link]
115 [Link]
116 [Link]
117 [Link]
[Link]
118
1

317
Contributors

2 IMacWin95~enwikibooks119
2 IT Enginner120
1 Ian Kelling121
1 Israelzeffren122
1 Itsborin123
1 J. Finkelstein124
1 J36miles125
1 JMentis126
39 JackBot127
62 JackPotte128
2 JamesNZ129
1 JayC130
1 Jecruzs~enwikibooks131
2 Jellysandwich0132
41 Jguk133
1 Jimmyatic134
1 Jk33135
6 Jomegat136
1 Jonathan Webley137
1 Jpkotta138
1 Jspring139
1 Juanvldz140
1 Jwy141
3 Kaktus142
2 Kelti143

119 [Link]
120 [Link]
121 [Link]
122 [Link]
123 [Link]
124 [Link]
125 [Link]
126 [Link]
127 [Link]
128 [Link]
129 [Link]
130 [Link]
[Link]
131
redlink=1
[Link]
132
1
133 [Link]
134 [Link]
135 [Link]
136 [Link]
137 [Link]
138 [Link]
139 [Link]
140 [Link]
141 [Link]
142 [Link]
143 [Link]

318
Note for C++ programmers

1 Kfitzner144
2 Kittycataclysm145
1 Lcawte146
3 Leaderboard147
1 Lkesteloot148
1 LlamaAl149
1 MCEmperor150
6 Malfist151
1 Manvinsh152
32 MarcGarver153
1 Marcobiscaro2112154
15 Maths314155
2 Matiia156
3 MatrixFrog~enwikibooks157
38 Mattylaws158
1 MaxBowsher159
1 MetaBohemian~enwikibooks160
1 Migcrown161
27 Mikm~enwikibooks162
1 Mild Bill Hiccup163
1 MinderBender164
3 Minorax165
8 Mmurdoch~enwikibooks166
6 MoWasHere167

144 [Link]
145 [Link]
146 [Link]
147 [Link]
148 [Link]
149 [Link]
150 [Link]
151 [Link]
152 [Link]
153 [Link]
[Link]
154
redlink=1
155 [Link]
156 [Link]
[Link]
157
redlink=1
158 [Link]
159 [Link]
160 [Link]
161 [Link]
162 [Link]
[Link]
163
redlink=1
164 [Link]
165 [Link]
[Link]
166
redlink=1
167 [Link]

319
Contributors

3 Mrjulesd168
2 Ms2ger169
1 Mstenta170
2 N8chz171
1 Nikai172
1 Nikjava173
1 Nimrodmilo174
1 Nitrodist175
2 Nkrumpe176
3 NoToleranceForIntolerance177
1 Olivier.deste178
2 Omzig89179
5 Panic2k4180
1 PantheraLeo1359531181
1 Parthajava182
1 Pedant65183
6 Pi zero184
2 QUBot185
1 Qubodup186
8 Rappo187
1 Ravichandar84188
1 Razr Nation189
10 Recent Runes190
1 Red4tribe191
2 Ricky clarkson192

168 [Link]
169 [Link]
170 [Link]
171 [Link]
172 [Link]
173 [Link]
174 [Link]
175 [Link]
176 [Link]
[Link]
177
edit&redlink=1
178 [Link]
179 [Link]
180 [Link]
[Link]
181
redlink=1
182 [Link]
183 [Link]
184 [Link]
185 [Link]
186 [Link]
187 [Link]
188 [Link]
189 [Link]
190 [Link]
191 [Link]
[Link]
192
1

320
Note for C++ programmers

1 Robert Horning193
5 RockyJasper194
2 SHB2000195
1 Sabhandari196
117 Sae1962197
1 Salix alba198
1 Samuel Castrillo Dom.199
5 Samwilson200
1 Scratchy201
9 Sdiq77202
3 Sgxi203
10 ShakespeareFan00204
2 SharingNotes205
78 Sigma 7206
4 Spitfyre55207
1 Splibubay208
10 Spongebob88209
19 Spoon!210
1 Staph aureus211
1 Staticshakedown212
1 Stephan Kulla213
1 Stephanvaningen214
3 Steveth45215
54 Strange quark216
1 Stryn217

193 [Link]
194 [Link]
195 [Link]
196 [Link]
197 [Link]
198 [Link]
[Link]
199
redlink=1
200 [Link]
201 [Link]
202 [Link]
203 [Link]
204 [Link]
205 [Link]
206 [Link]
207 [Link]
208 [Link]
209 [Link]
210 [Link]
211 [Link]
212 [Link]
213 [Link]
214 [Link]
215 [Link]
216 [Link]
217 [Link]

321
Contributors

4 Sundar22in218
5 Sunnychan~enwikibooks219
6 Superfly Jon220
2 Surachit221
1 Svartava222
2 Synoman Barris223
5 Syum90224
1 Tahmid225
2 TamaraDeClassic226
1 Tegel227
1 TenWhile6228
2 ThatNateGuy229
3 Thephilwells230
1 Thisnamestaken~enwikibooks231
1 Tom Morris232
4 Tomz0rs233
1 Tropicalkitty234
2 Turkmen235
2 UNV236
1 Ubuprofaan237
1 Ursalist238
1 User000name239
2 Van der Hoorn240
1 Viartola241
1 Vinay h242

218 [Link]
[Link]
219
redlink=1
220 [Link]
221 [Link]
222 [Link]
223 [Link]
224 [Link]
225 [Link]
226 [Link]
227 [Link]
228 [Link]
229 [Link]
230 [Link]
[Link]
231
edit&redlink=1
232 [Link]
233 [Link]
234 [Link]
235 [Link]
236 [Link]
237 [Link]
238 [Link]
239 [Link]
240 [Link]
241 [Link]
242 [Link]

322
Note for C++ programmers

1 Vipingupta74243
1 Vitaly Zdanevich244
2 Volvens245
2 WOSlinker246
1 Wargo247
17 Weirdsomebody248
3 WereSpielChequers249
1 Whiteknight250
1 WikiBayer251
2 WikiWizard~enwikibooks252
1 WiseEyes253
1 Wjcw254
1 Wur-dene255
2 Wutzofant256
2 Xania257
2 YMS258
2 Yabobay259
5 Yuuki Mayuki~enwikibooks260
2 ZI Jony261
2 ZacharyJ262
62 ZeroOne263
1 Zorak1103264
1 Łukasz Palka~enwikibooks265
4 266

243 [Link]
244 [Link]
245 [Link]
246 [Link]
247 [Link]
248 [Link]
249 [Link]
250 [Link]
251 [Link]
[Link]
252
redlink=1
253 [Link]
254 [Link]
255 [Link]
256 [Link]
257 [Link]
258 [Link]
259 [Link]
[Link]
260
edit&redlink=1
261 [Link]
262 [Link]
263 [Link]
264 [Link]
[Link]
265
action=edit&redlink=1
266 [Link]

323
Contributors

1 267

1 268

1 269

[Link]
267 %25E4%25B8%2587%25E7%2589%25A9%25E3%2581%25AE%25E9%259C%258A%25E9%2595%25B7&action=
edit&redlink=1
[Link]
268
action=edit&redlink=1
269 [Link]

324
List of Figures

• GFDL: Gnu Free Documentation License. [Link]


html
• cc-by-sa-4.0: Creative Commons Attribution ShareAlike 4.0 License. https://
[Link]/licenses/by-sa/4.0/[Link]
• cc-by-sa-3.0: Creative Commons Attribution ShareAlike 3.0 License. http://
[Link]/licenses/by-sa/3.0/
• cc-by-sa-2.5: Creative Commons Attribution ShareAlike 2.5 License. http://
[Link]/licenses/by-sa/2.5/
• cc-by-sa-2.0: Creative Commons Attribution ShareAlike 2.0 License. http://
[Link]/licenses/by-sa/2.0/
• cc-by-sa-1.0: Creative Commons Attribution ShareAlike 1.0 License. http://
[Link]/licenses/by-sa/1.0/
• cc-by-1.0: Creative Commons Attribution 1.0 License. [Link]
org/licenses/by/1.0/[Link]
• cc-by-2.0: Creative Commons Attribution 2.0 License. [Link]
org/licenses/by/2.0/
• cc-by-2.5: Creative Commons Attribution 2.5 License. [Link]
org/licenses/by/2.5/[Link]
• cc-by-3.0: Creative Commons Attribution 3.0 License. [Link]
org/licenses/by/3.0/[Link]
• cc-by-4.0: Creative Commons Attribution 4.0 License. [Link]
org/licenses/by/4.0/[Link]
• GPL: GNU General Public License. [Link]
• LGPL: GNU Lesser General Public License. [Link]
html
• PD: This image is in the public domain.
• ATTR: The copyright holder of this file allows anyone to use it for any purpose,
provided that the copyright holder is properly attributed. Redistribution, derivative
work, commercial use, and all other use is permitted.
• EURO: This is the common (reverse) face of a euro coin. The copyright on the design
of the common face of the euro coins belongs to the European Commission. Authorised

325
List of Figures

is reproduction in a format without relief (drawings, paintings, films) provided they


are not detrimental to the image of the euro.
• LFK: Lizenz Freie Kunst. [Link]
• CFR: Copyright free use.
• EPL: Eclipse Public License. [Link]
php
Copies of the GPL, the LGPL as well as a GFDL are included in chapter Licenses270 . Please
note that images in the public domain do not require attribution. You may click on the
image numbers in the following table to open the webpage of the images in your webbrower.

270 Chapter 44 on page 331

326
List of Figures

1 NASA PD
2 Peter Campbell271 , Peter Campbell272 CC-BY-SA-3.0
3 SVG by Gregory Maxwell273 , simplified by Sarang274 , SVG PD
by Gregory Maxwell275 , simplified by Sarang276
4 SVG by Gregory Maxwell277 , simplified by Sarang278 , SVG PD
by Gregory Maxwell279 , simplified by Sarang280
5 SVG by Gregory Maxwell281 , simplified by Sarang282 , SVG PD
by Gregory Maxwell283 , simplified by Sarang284
6 SVG by Gregory Maxwell285 , simplified by Sarang286 , SVG PD
by Gregory Maxwell287 , simplified by Sarang288
7 SVG by Gregory Maxwell289 , simplified by Sarang290 , SVG PD
by Gregory Maxwell291 , simplified by Sarang292
8 SVG by Gregory Maxwell293 , simplified by Sarang294 , SVG PD
by Gregory Maxwell295 , simplified by Sarang296
9 SVG by Gregory Maxwell297 , simplified by Sarang298 , SVG PD
by Gregory Maxwell299 , simplified by Sarang300
10 SVG by Gregory Maxwell301 , simplified by Sarang302 , SVG PD
by Gregory Maxwell303 , simplified by Sarang304

271 [Link]
272 [Link]
273 [Link]
274 [Link]
275 [Link]
276 [Link]
277 [Link]
278 [Link]
279 [Link]
280 [Link]
281 [Link]
282 [Link]
283 [Link]
284 [Link]
285 [Link]
286 [Link]
287 [Link]
288 [Link]
289 [Link]
290 [Link]
291 [Link]
292 [Link]
293 [Link]
294 [Link]
295 [Link]
296 [Link]
297 [Link]
298 [Link]
299 [Link]
300 [Link]
301 [Link]
302 [Link]
303 [Link]
304 [Link]

327
List of Figures

11 SVG by Gregory Maxwell305 , simplified by Sarang306 , SVG PD


by Gregory Maxwell307 , simplified by Sarang308
12 SVG by Gregory Maxwell309 , simplified by Sarang310 , SVG PD
by Gregory Maxwell311 , simplified by Sarang312
13 SVG by Gregory Maxwell313 , simplified by Sarang314 , SVG PD
by Gregory Maxwell315 , simplified by Sarang316
14 SVG by Gregory Maxwell317 , simplified by Sarang318 , SVG PD
by Gregory Maxwell319 , simplified by Sarang320
15 SVG by Gregory Maxwell321 , simplified by Sarang322 , SVG PD
by Gregory Maxwell323 , simplified by Sarang324
16 en:User:Arunreginald325 CC-BY-SA-3.0
17 No machine-readable author provided. Noodlez84326 as- PD
sumed (based on copyright claims)., No machine-readable
author provided. Noodlez84327 assumed (based on copyright
claims).
18 David Vignoni LGPL
19 David Vignoni LGPL
20 David Vignoni328 / ICON KING LGPL
21 David Vignoni LGPL
22 David Vignoni329 / ICON KING LGPL
23 Kazukiokumura330 (talk331 ), Kazukiokumura332 (talk333 ) CC-BY-SA-3.0
24 David Vignoni LGPL
25 David Vignoni334 / ICON KING LGPL

305 [Link]
306 [Link]
307 [Link]
308 [Link]
309 [Link]
310 [Link]
311 [Link]
312 [Link]
313 [Link]
314 [Link]
315 [Link]
316 [Link]
317 [Link]
318 [Link]
319 [Link]
320 [Link]
321 [Link]
322 [Link]
323 [Link]
324 [Link]
325 [Link]
326 [Link]
327 [Link]
328 [Link]
329 [Link]
330 [Link]
331 [Link]
332 [Link]
333 [Link]
334 [Link]

328
List of Figures

26 Kazukiokumura335 (talk336 ), Kazukiokumura337 (talk338 ) CC-BY-SA-3.0


27 David Vignoni LGPL
28 David Vignoni339 / ICON KING LGPL
29 David Vignoni340 / ICON KING LGPL
30 David Vignoni341 / ICON KING LGPL
31 User:ZyMOS342 , User:ZyMOS343 PD
32 Apple Inc. PD
33 Larry Ewing344 , Simon Budig345 , Garrett LeSage346 PD
34 Vinicius Depizzol347 GPL
35 The people from the Tango! project348 PD
36 Vinicius Depizzol349 GPL
37 The people from the Tango! project350 PD
38 User:ZyMOS351 , User:ZyMOS352 PD
39 Apple Inc. PD
40 Larry Ewing353 , Simon Budig354 , Garrett LeSage355 PD
41 Nameless23, [Link], Tacsipacsi, Virtualeuser, GPL
Bridget, BotMultichill, JarektBot, Hazard-Bot, LoStrango-
latore, Володимир Груша, Wutsje, Wknight94, Emijrpbot,
Theoteryi, Sven, Faxe
42 Dragon 280356 , Dragon 280357 PD
43 Arun Reginald358 , Arun Reginald359 CC-BY-SA-3.0
44 Arun Reginald360 , Arun Reginald361 CC-BY-SA-3.0

335 [Link]
336 [Link]
337 [Link]
338 [Link]
339 [Link]
340 [Link]
341 [Link]
342 [Link]
343 [Link]
344 [Link]
345 [Link]
346 [Link]
347 [Link]
348 [Link]
349 [Link]
350 [Link]
351 [Link]
352 [Link]
353 [Link]
354 [Link]
355 [Link]
356 [Link]
357 [Link]
[Link]
358
1
359 [Link]
[Link]
360
1
361 [Link]

329
List of Figures

45 Arun Reginald362 , Arun Reginald363 CC-BY-SA-3.0


46 Arun Reginald364 , Arun Reginald365 CC-BY-SA-3.0
47 Arun Reginald366 , Arun Reginald367 CC-BY-SA-3.0
48 Arun Reginald368 , Arun Reginald369 CC-BY-SA-3.0
49 Arun Reginald370 , Arun Reginald371 CC-BY-SA-3.0
50 Arun Reginald372 , Arun Reginald373 CC-BY-SA-3.0
51 Arun Reginald374 , Arun Reginald375 CC-BY-SA-3.0
52 Ervinn376 at English Wikibooks377 CC-BY-SA-2.5
53 Ervinn378 at English Wikibooks379 CC-BY-SA-2.5
54 Ervinn380 at English Wikibooks381 CC-BY-SA-2.5
55 Ervinn382 at English Wikibooks383 CC-BY-SA-2.5
56 Ervinn384 at English Wikibooks385 CC-BY-SA-2.5
57 Arun Reginald386 , Arun Reginald387 CC-BY-3.0
58 Arun Reginald388 , Arun Reginald389 CC-BY-SA-3.0

[Link]
362
1
363 [Link]
[Link]
364
1
365 [Link]
[Link]
366
1
367 [Link]
[Link]
368
1
369 [Link]
[Link]
370
1
371 [Link]
[Link]
372
1
373 [Link]
[Link]
374
1
375 [Link]
376 [Link]
377 [Link]
378 [Link]
379 [Link]
380 [Link]
381 [Link]
382 [Link]
383 [Link]
384 [Link]
385 [Link]
[Link]
386
1
387 [Link]
[Link]
388
1
389 [Link]

330
44 Licenses

44.1 GNU GENERAL PUBLIC LICENSE


Version 3, 29 June 2007 The “Corresponding Source” for a work in object code form means all different server (operated by you or a third party) that supports equiv- your license, and (b) permanently, if the copyright holder fails to no-
the source code needed to generate, install, and (for an executable alent copying facilities, provided you maintain clear directions next to tify you of the violation by some reasonable means prior to 60 days
Copyright © 2007 Free Software Foundation, Inc. <[Link] work) run the object code and to modify the work, including scripts the object code saying where to find the Corresponding Source. Re- after the cessation.
to control those activities. However, it does not include the work’s gardless of what server hosts the Corresponding Source, you remain
System Libraries, or general-purpose tools or generally available free obligated to ensure that it is available for as long as needed to satisfy Moreover, your license from a particular copyright holder is reinstated
Everyone is permitted to copy and distribute verbatim copies of this
programs which are used unmodified in performing those activities but these requirements. * e) Convey the object code using peer-to-peer permanently if the copyright holder notifies you of the violation by
license document, but changing it is not allowed. Preamble
which are not part of the work. For example, Corresponding Source transmission, provided you inform other peers where the object code some reasonable means, this is the first time you have received notice
includes interface definition files associated with source files for the and Corresponding Source of the work are being offered to the general of violation of this License (for any work) from that copyright holder,
The GNU General Public License is a free, copyleft license for software work, and the source code for shared libraries and dynamically linked public at no charge under subsection 6d. and you cure the violation prior to 30 days after your receipt of the
and other kinds of works. subprograms that the work is specifically designed to require, such as notice.
by intimate data communication or control flow between those sub- A separable portion of the object code, whose source code is excluded
The licenses for most software and other practical works are designed programs and other parts of the work. from the Corresponding Source as a System Library, need not be in- Termination of your rights under this section does not terminate the
to take away your freedom to share and change the works. By con- cluded in conveying the object code work. licenses of parties who have received copies or rights from you under
trast, the GNU General Public License is intended to guarantee your The Corresponding Source need not include anything that users can re- this License. If your rights have been terminated and not permanently
freedom to share and change all versions of a program–to make sure generate automatically from other parts of the Corresponding Source.
A “User Product” is either (1) a “consumer product”, which means any reinstated, you do not qualify to receive new licenses for the same
it remains free software for all its users. We, the Free Software Foun-
tangible personal property which is normally used for personal, family, material under section 10. 9. Acceptance Not Required for Having
dation, use the GNU General Public License for most of our software;
The Corresponding Source for a work in source code form is that same or household purposes, or (2) anything designed or sold for incorpora- Copies.
it applies also to any other work released this way by its authors. You
work. 2. Basic Permissions. tion into a dwelling. In determining whether a product is a consumer
can apply it to your programs, too.
product, doubtful cases shall be resolved in favor of coverage. For a You are not required to accept this License in order to receive or run
All rights granted under this License are granted for the term of copy- particular product received by a particular user, “normally used” refers a copy of the Program. Ancillary propagation of a covered work oc-
When we speak of free software, we are referring to freedom, not price.
right on the Program, and are irrevocable provided the stated condi- to a typical or common use of that class of product, regardless of the curring solely as a consequence of using peer-to-peer transmission to
Our General Public Licenses are designed to make sure that you have
tions are met. This License explicitly affirms your unlimited permission status of the particular user or of the way in which the particular receive a copy likewise does not require acceptance. However, nothing
the freedom to distribute copies of free software (and charge for them
to run the unmodified Program. The output from running a covered user actually uses, or expects or is expected to use, the product. A other than this License grants you permission to propagate or modify
if you wish), that you receive source code or can get it if you want
work is covered by this License only if the output, given its content, product is a consumer product regardless of whether the product has any covered work. These actions infringe copyright if you do not accept
it, that you can change the software or use pieces of it in new free
constitutes a covered work. This License acknowledges your rights of substantial commercial, industrial or non-consumer uses, unless such this License. Therefore, by modifying or propagating a covered work,
programs, and that you know you can do these things.
fair use or other equivalent, as provided by copyright law. uses represent the only significant mode of use of the product. you indicate your acceptance of this License to do so. 10. Automatic
Licensing of Downstream Recipients.
To protect your rights, we need to prevent others from denying you
You may make, run and propagate covered works that you do not con- “Installation Information” for a User Product means any methods, pro-
these rights or asking you to surrender the rights. Therefore, you have
vey, without conditions so long as your license otherwise remains in cedures, authorization keys, or other information required to install Each time you convey a covered work, the recipient automatically re-
certain responsibilities if you distribute copies of the software, or if you
force. You may convey covered works to others for the sole purpose and execute modified versions of a covered work in that User Product ceives a license from the original licensors, to run, modify and prop-
modify it: responsibilities to respect the freedom of others.
of having them make modifications exclusively for you, or provide you from a modified version of its Corresponding Source. The information agate that work, subject to this License. You are not responsible for
with facilities for running those works, provided that you comply with must suffice to ensure that the continued functioning of the modified enforcing compliance by third parties with this License.
For example, if you distribute copies of such a program, whether gratis the terms of this License in conveying all material for which you do not object code is in no case prevented or interfered with solely because
or for a fee, you must pass on to the recipients the same freedoms that control copyright. Those thus making or running the covered works modification has been made. An “entity transaction” is a transaction transferring control of an or-
you received. You must make sure that they, too, receive or can get for you must do so exclusively on your behalf, under your direction ganization, or substantially all assets of one, or subdividing an orga-
the source code. And you must show them these terms so they know and control, on terms that prohibit them from making any copies of
If you convey an object code work under this section in, or with, or nization, or merging organizations. If propagation of a covered work
their rights. your copyrighted material outside their relationship with you.
specifically for use in, a User Product, and the conveying occurs as part results from an entity transaction, each party to that transaction who
of a transaction in which the right of possession and use of the User receives a copy of the work also receives whatever licenses to the work
Developers that use the GNU GPL protect your rights with two steps: Conveying under any other circumstances is permitted solely under the party’s predecessor in interest had or could give under the previous
Product is transferred to the recipient in perpetuity or for a fixed term
(1) assert copyright on the software, and (2) offer you this License the conditions stated below. Sublicensing is not allowed; section 10 paragraph, plus a right to possession of the Corresponding Source of
(regardless of how the transaction is characterized), the Corresponding
giving you legal permission to copy, distribute and/or modify it. makes it unnecessary. 3. Protecting Users’ Legal Rights From Anti- the work from the predecessor in interest, if the predecessor has it or
Source conveyed under this section must be accompanied by the In-
Circumvention Law. stallation Information. But this requirement does not apply if neither can get it with reasonable efforts.
For the developers’ and authors’ protection, the GPL clearly explains you nor any third party retains the ability to install modified object
that there is no warranty for this free software. For both users’ and No covered work shall be deemed part of an effective technological code on the User Product (for example, the work has been installed in You may not impose any further restrictions on the exercise of the
authors’ sake, the GPL requires that modified versions be marked as measure under any applicable law fulfilling obligations under article ROM). rights granted or affirmed under this License. For example, you may
changed, so that their problems will not be attributed erroneously to 11 of the WIPO copyright treaty adopted on 20 December 1996, or not impose a license fee, royalty, or other charge for exercise of rights
authors of previous versions. similar laws prohibiting or restricting circumvention of such measures. granted under this License, and you may not initiate litigation (in-
The requirement to provide Installation Information does not include
a requirement to continue to provide support service, warranty, or up- cluding a cross-claim or counterclaim in a lawsuit) alleging that any
Some devices are designed to deny users access to install or run mod- dates for a work that has been modified or installed by the recipient, patent claim is infringed by making, using, selling, offering for sale, or
When you convey a covered work, you waive any legal power to forbid
ified versions of the software inside them, although the manufacturer or for the User Product in which it has been modified or installed. importing the Program or any portion of it. 11. Patents.
circumvention of technological measures to the extent such circumven-
can do so. This is fundamentally incompatible with the aim of protect- tion is effected by exercising rights under this License with respect Access to a network may be denied when the modification itself ma-
ing users’ freedom to change the software. The systematic pattern of to the covered work, and you disclaim any intention to limit opera- terially and adversely affects the operation of the network or violates A “contributor” is a copyright holder who authorizes use under this
such abuse occurs in the area of products for individuals to use, which tion or modification of the work as a means of enforcing, against the the rules and protocols for communication across the network. License of the Program or a work on which the Program is based. The
is precisely where it is most unacceptable. Therefore, we have designed work’s users, your or third parties’ legal rights to forbid circumvention work thus licensed is called the contributor’s “contributor version”.
this version of the GPL to prohibit the practice for those products. If of technological measures. 4. Conveying Verbatim Copies.
such problems arise substantially in other domains, we stand ready to Corresponding Source conveyed, and Installation Information pro-
vided, in accord with this section must be in a format that is publicly A contributor’s “essential patent claims” are all patent claims owned
extend this provision to those domains in future versions of the GPL,
You may convey verbatim copies of the Program’s source code as you documented (and with an implementation available to the public in or controlled by the contributor, whether already acquired or hereafter
as needed to protect the freedom of users.
receive it, in any medium, provided that you conspicuously and appro- source code form), and must require no special password or key for acquired, that would be infringed by some manner, permitted by this
priately publish on each copy an appropriate copyright notice; keep in- unpacking, reading or copying. 7. Additional Terms. License, of making, using, or selling its contributor version, but do
Finally, every program is threatened constantly by software patents. not include claims that would be infringed only as a consequence of
tact all notices stating that this License and any non-permissive terms
States should not allow patents to restrict development and use of soft- further modification of the contributor version. For purposes of this
added in accord with section 7 apply to the code; keep intact all no-
ware on general-purpose computers, but in those that do, we wish to “Additional permissions” are terms that supplement the terms of this definition, “control” includes the right to grant patent sublicenses in a
tices of the absence of any warranty; and give all recipients a copy of
avoid the special danger that patents applied to a free program could License by making exceptions from one or more of its conditions. Ad- manner consistent with the requirements of this License.
this License along with the Program.
make it effectively proprietary. To prevent this, the GPL assures that ditional permissions that are applicable to the entire Program shall be
patents cannot be used to render the program non-free. treated as though they were included in this License, to the extent that
You may charge any price or no price for each copy that you con- they are valid under applicable law. If additional permissions apply Each contributor grants you a non-exclusive, worldwide, royalty-free
vey, and you may offer support or warranty protection for a fee. 5. only to part of the Program, that part may be used separately under patent license under the contributor’s essential patent claims, to make,
The precise terms and conditions for copying, distribution and modi- use, sell, offer for sale, import and otherwise run, modify and propagate
Conveying Modified Source Versions. those permissions, but the entire Program remains governed by this
fication follow. TERMS AND CONDITIONS 0. Definitions. the contents of its contributor version.
License without regard to the additional permissions.
You may convey a work based on the Program, or the modifications
“This License” refers to version 3 of the GNU General Public License. In the following three paragraphs, a “patent license” is any express
to produce it from the Program, in the form of source code under the When you convey a copy of a covered work, you may at your option
terms of section 4, provided that you also meet all of these conditions: agreement or commitment, however denominated, not to enforce a
remove any additional permissions from that copy, or from any part
“Copyright” also means copyright-like laws that apply to other kinds patent (such as an express permission to practice a patent or covenant
of it. (Additional permissions may be written to require their own
of works, such as semiconductor masks. not to sue for patent infringement). To “grant” such a patent license
* a) The work must carry prominent notices stating that you modified removal in certain cases when you modify the work.) You may place
to a party means to make such an agreement or commitment not to
it, and giving a relevant date. * b) The work must carry prominent additional permissions on material, added by you to a covered work,
“The Program” refers to any copyrightable work licensed under this Li- enforce a patent against the party.
notices stating that it is released under this License and any conditions for which you have or can give appropriate copyright permission.
cense. Each licensee is addressed as “you”. “Licensees” and “recipients” added under section 7. This requirement modifies the requirement in
may be individuals or organizations. section 4 to “keep intact all notices”. * c) You must license the entire If you convey a covered work, knowingly relying on a patent license,
Notwithstanding any other provision of this License, for material you and the Corresponding Source of the work is not available for anyone
work, as a whole, under this License to anyone who comes into pos-
add to a covered work, you may (if authorized by the copyright holders to copy, free of charge and under the terms of this License, through
To “modify” a work means to copy from or adapt all or part of the work session of a copy. This License will therefore apply, along with any
of that material) supplement the terms of this License with terms: a publicly available network server or other readily accessible means,
in a fashion requiring copyright permission, other than the making of applicable section 7 additional terms, to the whole of the work, and
an exact copy. The resulting work is called a “modified version” of the all its parts, regardless of how they are packaged. This License gives then you must either (1) cause the Corresponding Source to be so
earlier work or a work “based on” the earlier work. no permission to license the work in any other way, but it does not * a) Disclaiming warranty or limiting liability differently from the available, or (2) arrange to deprive yourself of the benefit of the patent
invalidate such permission if you have separately received it. * d) If terms of sections 15 and 16 of this License; or * b) Requiring preser- license for this particular work, or (3) arrange, in a manner consistent
the work has interactive user interfaces, each must display Appropriate vation of specified reasonable legal notices or author attributions in with the requirements of this License, to extend the patent license to
A “covered work” means either the unmodified Program or a work
Legal Notices; however, if the Program has interactive interfaces that that material or in the Appropriate Legal Notices displayed by works downstream recipients. “Knowingly relying” means you have actual
based on the Program.
do not display Appropriate Legal Notices, your work need not make containing it; or * c) Prohibiting misrepresentation of the origin of knowledge that, but for the patent license, your conveying the cov-
them do so. that material, or requiring that modified versions of such material be ered work in a country, or your recipient’s use of the covered work
To “propagate” a work means to do anything with it that, without per- marked in reasonable ways as different from the original version; or * in a country, would infringe one or more identifiable patents in that
mission, would make you directly or secondarily liable for infringement d) Limiting the use for publicity purposes of names of licensors or au- country that you have reason to believe are valid.
under applicable copyright law, except executing it on a computer or A compilation of a covered work with other separate and independent
thors of the material; or * e) Declining to grant rights under trademark
modifying a private copy. Propagation includes copying, distribution works, which are not by their nature extensions of the covered work,
law for use of some trade names, trademarks, or service marks; or * If, pursuant to or in connection with a single transaction or arrange-
(with or without modification), making available to the public, and in and which are not combined with it such as to form a larger program,
f) Requiring indemnification of licensors and authors of that material ment, you convey, or propagate by procuring conveyance of, a covered
some countries other activities as well. in or on a volume of a storage or distribution medium, is called an
by anyone who conveys the material (or modified versions of it) with work, and grant a patent license to some of the parties receiving the
“aggregate” if the compilation and its resulting copyright are not used
contractual assumptions of liability to the recipient, for any liability covered work authorizing them to use, propagate, modify or convey a
to limit the access or legal rights of the compilation’s users beyond
To “convey” a work means any kind of propagation that enables other that these contractual assumptions directly impose on those licensors specific copy of the covered work, then the patent license you grant is
what the individual works permit. Inclusion of a covered work in an
parties to make or receive copies. Mere interaction with a user through and authors. automatically extended to all recipients of the covered work and works
aggregate does not cause this License to apply to the other parts of
a computer network, with no transfer of a copy, is not conveying. based on it.
the aggregate. 6. Conveying Non-Source Forms.
All other non-permissive additional terms are considered “further re-
An interactive user interface displays “Appropriate Legal Notices” to strictions” within the meaning of section 10. If the Program as you A patent license is “discriminatory” if it does not include within the
You may convey a covered work in object code form under the terms of
the extent that it includes a convenient and prominently visible fea- received it, or any part of it, contains a notice stating that it is gov- scope of its coverage, prohibits the exercise of, or is conditioned on the
sections 4 and 5, provided that you also convey the machine-readable
ture that (1) displays an appropriate copyright notice, and (2) tells the erned by this License along with a term that is a further restriction, non-exercise of one or more of the rights that are specifically granted
Corresponding Source under the terms of this License, in one of these
user that there is no warranty for the work (except to the extent that you may remove that term. If a license document contains a further under this License. You may not convey a covered work if you are
ways:
warranties are provided), that licensees may convey the work under restriction but permits relicensing or conveying under this License, you a party to an arrangement with a third party that is in the business
this License, and how to view a copy of this License. If the inter- may add to a covered work material governed by the terms of that li- of distributing software, under which you make payment to the third
face presents a list of user commands or options, such as a menu, a * a) Convey the object code in, or embodied in, a physical product (in- cense document, provided that the further restriction does not survive party based on the extent of your activity of conveying the work, and
prominent item in the list meets this criterion. 1. Source Code. cluding a physical distribution medium), accompanied by the Corre- such relicensing or conveying. under which the third party grants, to any of the parties who would
sponding Source fixed on a durable physical medium customarily used
receive the covered work from you, a discriminatory patent license (a)
for software interchange. * b) Convey the object code in, or embodied
The “source code” for a work means the preferred form of the work for If you add terms to a covered work in accord with this section, you in connection with copies of the covered work conveyed by you (or
in, a physical product (including a physical distribution medium), ac-
making modifications to it. “Object code” means any non-source form must place, in the relevant source files, a statement of the additional copies made from those copies), or (b) primarily for and in connection
companied by a written offer, valid for at least three years and valid
of a work. terms that apply to those files, or a notice indicating where to find the with specific products or compilations that contain the covered work,
for as long as you offer spare parts or customer support for that prod-
applicable terms. unless you entered into that arrangement, or that patent license was
uct model, to give anyone who possesses the object code either (1) a
A “Standard Interface” means an interface that either is an official granted, prior to 28 March 2007.
copy of the Corresponding Source for all the software in the product
standard defined by a recognized standards body, or, in the case of that is covered by this License, on a durable physical medium cus- Additional terms, permissive or non-permissive, may be stated in the
interfaces specified for a particular programming language, one that is tomarily used for software interchange, for a price no more than your Nothing in this License shall be construed as excluding or limiting any
form of a separately written license, or stated as exceptions; the above
widely used among developers working in that language. reasonable cost of physically performing this conveying of source, or implied license or other defenses to infringement that may otherwise
requirements apply either way. 8. Termination.
(2) access to copy the Corresponding Source from a network server at be available to you under applicable patent law. 12. No Surrender of
The “System Libraries” of an executable work include anything, other no charge. * c) Convey individual copies of the object code with a Others’ Freedom.
than the work as a whole, that (a) is included in the normal form of copy of the written offer to provide the Corresponding Source. This You may not propagate or modify a covered work except as expressly
packaging a Major Component, but which is not part of that Major alternative is allowed only occasionally and noncommercially, and only provided under this License. Any attempt otherwise to propagate or If conditions are imposed on you (whether by court order, agreement
Component, and (b) serves only to enable use of the work with that if you received the object code with such an offer, in accord with sub- modify it is void, and will automatically terminate your rights under or otherwise) that contradict the conditions of this License, they do not
Major Component, or to implement a Standard Interface for which an section 6b. * d) Convey the object code by offering access from a this License (including any patent licenses granted under the third excuse you from the conditions of this License. If you cannot convey
implementation is available to the public in source code form. A “Ma- designated place (gratis or for a charge), and offer equivalent access to paragraph of section 11). a covered work so as to satisfy simultaneously your obligations under
jor Component”, in this context, means a major essential component the Corresponding Source in the same way through the same place at this License and any other pertinent obligations, then as a consequence
(kernel, window system, and so on) of the specific operating system (if no further charge. You need not require recipients to copy the Corre- However, if you cease all violation of this License, then your license you may not convey it at all. For example, if you agree to terms that
any) on which the executable work runs, or a compiler used to produce sponding Source along with the object code. If the place to copy the from a particular copyright holder is reinstated (a) provisionally, un- obligate you to collect a royalty for further conveying from those to
the work, or an object code interpreter used to run it. object code is a network server, the Corresponding Source may be on a less and until the copyright holder explicitly and finally terminates whom you convey the Program, the only way you could satisfy both
those terms and this License would be to refrain entirely from con- Later license versions may give you additional or different permissions. If the disclaimer of warranty and limitation of liability provided above You should have received a copy of the GNU General Public License
veying the Program. 13. Use with the GNU Affero General Public However, no additional obligations are imposed on any author or copy- cannot be given local legal effect according to their terms, reviewing along with this program. If not, see <[Link]
License. right holder as a result of your choosing to follow a later version. 15. courts shall apply local law that most closely approximates an abso-
Disclaimer of Warranty. lute waiver of all civil liability in connection with the Program, unless a
warranty or assumption of liability accompanies a copy of the Program Also add information on how to contact you by electronic and paper
Notwithstanding any other provision of this License, you have permis- in return for a fee. mail.
sion to link or combine any covered work with a work licensed under
version 3 of the GNU Affero General Public License into a single com- THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EX-
TENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN END OF TERMS AND CONDITIONS How to Apply These Terms If the program does terminal interaction, make it output a short notice
bined work, and to convey the resulting work. The terms of this Li-
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLD- to Your New Programs like this when it starts in an interactive mode:
cense will continue to apply to the part which is the covered work, but
the special requirements of the GNU Affero General Public License, ERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM
section 13, concerning interaction through a network will apply to the “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EX- If you develop a new program, and you want it to be of the greatest <program> Copyright (C) <year> <name of author> This program
combination as such. 14. Revised Versions of this License. PRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, possible use to the public, the best way to achieve this is to make it comes with ABSOLUTELY NO WARRANTY; for details type ‘show
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND free software which everyone can redistribute and change under these w’. This is free software, and you are welcome to redistribute it under
FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK terms. certain conditions; type ‘show c’ for details.
The Free Software Foundation may publish revised and/or new ver- AS TO THE QUALITY AND PERFORMANCE OF THE PRO-
sions of the GNU General Public License from time to time. Such new GRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DE-
To do so, attach the following notices to the program. It is safest to
versions will be similar in spirit to the present version, but may differ FECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SER- The hypothetical commands ‘show w’ and ‘show c’ should show the
attach them to the start of each source file to most effectively state the
in detail to address new problems or concerns. VICING, REPAIR OR CORRECTION. 16. Limitation of Liability. appropriate parts of the General Public License. Of course, your pro-
exclusion of warranty; and each file should have at least the “copyright”
line and a pointer to where the full notice is found. gram’s commands might be different; for a GUI interface, you would
use an “about box”.
Each version is given a distinguishing version number. If the Program
specifies that a certain numbered version of the GNU General Pub- IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR
<one line to give the program’s name and a brief idea of what it does.>
lic License “or any later version” applies to it, you have the option of AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, You should also get your employer (if you work as a programmer) or
Copyright (C) <year> <name of author>
following the terms and conditions either of that numbered version or OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS school, if any, to sign a “copyright disclaimer” for the program, if nec-
of any later version published by the Free Software Foundation. If THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU essary. For more information on this, and how to apply and follow the
FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCI- This program is free software: you can redistribute it and/or modify
the Program does not specify a version number of the GNU General it under the terms of the GNU General Public License as published by GNU GPL, see <[Link]
Public License, you may choose any version ever published by the Free DENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF
THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING the Free Software Foundation, either version 3 of the License, or (at
Software Foundation. your option) any later version.
BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING REN- The GNU General Public License does not permit incorporating your
DERED INACCURATE OR LOSSES SUSTAINED BY YOU OR program into proprietary programs. If your program is a subroutine
If the Program specifies that a proxy can decide which future versions THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPER- This program is distributed in the hope that it will be useful, but library, you may consider it more useful to permit linking proprietary
of the GNU General Public License can be used, that proxy’s public ATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER WITHOUT ANY WARRANTY; without even the implied warranty applications with the library. If this is what you want to do, use the
statement of acceptance of a version permanently authorizes you to OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY of MERCHANTABILITY or FITNESS FOR A PARTICULAR PUR- GNU Lesser General Public License instead of this License. But first,
choose that version for the Program. OF SUCH DAMAGES. 17. Interpretation of Sections 15 and 16. POSE. See the GNU General Public License for more details. please read <[Link]

44.2 GNU Free Documentation License


Version 1.3, 3 November 2008 following text that translates XYZ in another language. (Here XYZ in their titles. Section numbers or the equivalent are not considered (section 1) will typically require changing the actual title. 9. TERMI-
stands for a specific section name mentioned below, such as ”Acknowl- part of the section titles. * M. Delete any section Entitled ”Endorse- NATION
Copyright © 2000, 2001, 2002, 2007, 2008 Free Software Foundation, edgements”, ”Dedications”, ”Endorsements”, or ”History”.) To ”Preserve ments”. Such a section may not be included in the Modified Version.
Inc. <[Link] the Title” of such a section when you modify the Document means that * N. Do not retitle any existing section to be Entitled ”Endorsements” You may not copy, modify, sublicense, or distribute the Document ex-
it remains a section ”Entitled XYZ” according to this definition. or to conflict in title with any Invariant Section. * O. Preserve any cept as expressly provided under this License. Any attempt otherwise
Warranty Disclaimers. to copy, modify, sublicense, or distribute it is void, and will automati-
Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed. 0. PREAMBLE The Document may include Warranty Disclaimers next to the notice cally terminate your rights under this License.
which states that this License applies to the Document. These War- If the Modified Version includes new front-matter sections or appen-
ranty Disclaimers are considered to be included by reference in this dices that qualify as Secondary Sections and contain no material copied However, if you cease all violation of this License, then your license
The purpose of this License is to make a manual, textbook, or other License, but only as regards disclaiming warranties: any other impli- from the Document, you may at your option designate some or all of from a particular copyright holder is reinstated (a) provisionally, un-
functional and useful document ”free” in the sense of freedom: to as- cation that these Warranty Disclaimers may have is void and has no these sections as invariant. To do this, add their titles to the list of less and until the copyright holder explicitly and finally terminates
sure everyone the effective freedom to copy and redistribute it, with effect on the meaning of this License. 2. VERBATIM COPYING Invariant Sections in the Modified Version’s license notice. These titles your license, and (b) permanently, if the copyright holder fails to no-
or without modifying it, either commercially or noncommercially. Sec-
must be distinct from any other section titles. tify you of the violation by some reasonable means prior to 60 days
ondarily, this License preserves for the author and publisher a way to
get credit for their work, while not being considered responsible for You may copy and distribute the Document in any medium, either after the cessation.
modifications made by others. commercially or noncommercially, provided that this License, the copy- You may add a section Entitled ”Endorsements”, provided it contains
right notices, and the license notice saying this License applies to the nothing but endorsements of your Modified Version by various par- Moreover, your license from a particular copyright holder is reinstated
Document are reproduced in all copies, and that you add no other con- ties—for example, statements of peer review or that the text has been
This License is a kind of ”copyleft”, which means that derivative works permanently if the copyright holder notifies you of the violation by
ditions whatsoever to those of this License. You may not use technical approved by an organization as the authoritative definition of a stan-
of the document must themselves be free in the same sense. It com- some reasonable means, this is the first time you have received notice
measures to obstruct or control the reading or further copying of the dard.
plements the GNU General Public License, which is a copyleft license of violation of this License (for any work) from that copyright holder,
copies you make or distribute. However, you may accept compensa-
designed for free software. and you cure the violation prior to 30 days after your receipt of the
tion in exchange for copies. If you distribute a large enough number
You may add a passage of up to five words as a Front-Cover Text, notice.
of copies you must also follow the conditions in section 3.
and a passage of up to 25 words as a Back-Cover Text, to the end
We have designed this License in order to use it for manuals for free
of the list of Cover Texts in the Modified Version. Only one passage Termination of your rights under this section does not terminate the
software, because free software needs free documentation: a free pro- You may also lend copies, under the same conditions stated above, and of Front-Cover Text and one of Back-Cover Text may be added by licenses of parties who have received copies or rights from you under
gram should come with manuals providing the same freedoms that the you may publicly display copies. 3. COPYING IN QUANTITY (or through arrangements made by) any one entity. If the Document this License. If your rights have been terminated and not permanently
software does. But this License is not limited to software manuals;
already includes a cover text for the same cover, previously added by reinstated, receipt of a copy of some or all of the same material does
it can be used for any textual work, regardless of subject matter or
If you publish printed copies (or copies in media that commonly have you or by arrangement made by the same entity you are acting on not give you any rights to use it. 10. FUTURE REVISIONS OF THIS
whether it is published as a printed book. We recommend this Li-
printed covers) of the Document, numbering more than 100, and the behalf of, you may not add another; but you may replace the old one, LICENSE
cense principally for works whose purpose is instruction or reference.
Document’s license notice requires Cover Texts, you must enclose the on explicit permission from the previous publisher that added the old
1. APPLICABILITY AND DEFINITIONS
copies in covers that carry, clearly and legibly, all these Cover Texts: one.
The Free Software Foundation may publish new, revised versions
Front-Cover Texts on the front cover, and Back-Cover Texts on the
This License applies to any manual or other work, in any medium, of the GNU Free Documentation License from time to time. Such
back cover. Both covers must also clearly and legibly identify you as The author(s) and publisher(s) of the Document do not by this Li-
that contains a notice placed by the copyright holder saying it can new versions will be similar in spirit to the present version, but
the publisher of these copies. The front cover must present the full title cense give permission to use their names for publicity for or to as-
be distributed under the terms of this License. Such a notice grants a may differ in detail to address new problems or concerns. See
with all words of the title equally prominent and visible. You may add sert or imply endorsement of any Modified Version. 5. COMBINING
world-wide, royalty-free license, unlimited in duration, to use that work [Link]
other material on the covers in addition. Copying with changes limited DOCUMENTS
under the conditions stated herein. The ”Document”, below, refers to to the covers, as long as they preserve the title of the Document and
any such manual or work. Any member of the public is a licensee, and satisfy these conditions, can be treated as verbatim copying in other Each version of the License is given a distinguishing version number.
is addressed as ”you”. You accept the license if you copy, modify or respects. You may combine the Document with other documents released under If the Document specifies that a particular numbered version of this
distribute the work in a way requiring permission under copyright law. this License, under the terms defined in section 4 above for modified License ”or any later version” applies to it, you have the option of
versions, provided that you include in the combination all of the In- following the terms and conditions either of that specified version or
If the required texts for either cover are too voluminous to fit legibly, variant Sections of all of the original documents, unmodified, and list
A ”Modified Version” of the Document means any work containing the of any later version that has been published (not as a draft) by the
you should put the first ones listed (as many as fit reasonably) on the them all as Invariant Sections of your combined work in its license
Document or a portion of it, either copied verbatim, or with modifica- Free Software Foundation. If the Document does not specify a version
actual cover, and continue the rest onto adjacent pages. notice, and that you preserve all their Warranty Disclaimers.
tions and/or translated into another language. number of this License, you may choose any version ever published
(not as a draft) by the Free Software Foundation. If the Document
If you publish or distribute Opaque copies of the Document numbering specifies that a proxy can decide which future versions of this License
A ”Secondary Section” is a named appendix or a front-matter sec- The combined work need only contain one copy of this License, and
more than 100, you must either include a machine-readable Transpar- can be used, that proxy’s public statement of acceptance of a version
tion of the Document that deals exclusively with the relationship of multiple identical Invariant Sections may be replaced with a single
ent copy along with each Opaque copy, or state in or with each Opaque permanently authorizes you to choose that version for the Document.
the publishers or authors of the Document to the Document’s overall copy. If there are multiple Invariant Sections with the same name
copy a computer-network location from which the general network- 11. RELICENSING
subject (or to related matters) and contains nothing that could fall but different contents, make the title of each such section unique by
using public has access to download using public-standard network
directly within that overall subject. (Thus, if the Document is in part adding at the end of it, in parentheses, the name of the original au-
protocols a complete Transparent copy of the Document, free of added
a textbook of mathematics, a Secondary Section may not explain any thor or publisher of that section if known, or else a unique number. ”Massive Multiauthor Collaboration Site” (or ”MMC Site”) means any
material. If you use the latter option, you must take reasonably pru-
mathematics.) The relationship could be a matter of historical connec- Make the same adjustment to the section titles in the list of Invariant World Wide Web server that publishes copyrightable works and also
dent steps, when you begin distribution of Opaque copies in quantity,
tion with the subject or with related matters, or of legal, commercial, Sections in the license notice of the combined work. provides prominent facilities for anybody to edit those works. A public
to ensure that this Transparent copy will remain thus accessible at the
philosophical, ethical or political position regarding them. stated location until at least one year after the last time you distribute wiki that anybody can edit is an example of such a server. A ”Massive
an Opaque copy (directly or through your agents or retailers) of that In the combination, you must combine any sections Entitled ”History” Multiauthor Collaboration” (or ”MMC”) contained in the site means
edition to the public. in the various original documents, forming one section Entitled ”His- any set of copyrightable works thus published on the MMC site.
The ”Invariant Sections” are certain Secondary Sections whose titles
are designated, as being those of Invariant Sections, in the notice that tory”; likewise combine any sections Entitled ”Acknowledgements”, and
says that the Document is released under this License. If a section any sections Entitled ”Dedications”. You must delete all sections En- ”CC-BY-SA” means the Creative Commons Attribution-Share Alike
It is requested, but not required, that you contact the authors of the
does not fit the above definition of Secondary then it is not allowed to titled ”Endorsements”. 6. COLLECTIONS OF DOCUMENTS 3.0 license published by Creative Commons Corporation, a not-for-
Document well before redistributing any large number of copies, to
be designated as Invariant. The Document may contain zero Invariant give them a chance to provide you with an updated version of the profit corporation with a principal place of business in San Francisco,
Sections. If the Document does not identify any Invariant Sections Document. 4. MODIFICATIONS You may make a collection consisting of the Document and other doc- California, as well as future copyleft versions of that license published
then there are none. uments released under this License, and replace the individual copies by that same organization.
of this License in the various documents with a single copy that is
You may copy and distribute a Modified Version of the Document un-
The ”Cover Texts” are certain short passages of text that are listed, as included in the collection, provided that you follow the rules of this ”Incorporate” means to publish or republish a Document, in whole or
der the conditions of sections 2 and 3 above, provided that you release
Front-Cover Texts or Back-Cover Texts, in the notice that says that License for verbatim copying of each of the documents in all other in part, as part of another Document.
the Modified Version under precisely this License, with the Modified
the Document is released under this License. A Front-Cover Text may respects.
Version filling the role of the Document, thus licensing distribution
be at most 5 words, and a Back-Cover Text may be at most 25 words. and modification of the Modified Version to whoever possesses a copy An MMC is ”eligible for relicensing” if it is licensed under this License,
of it. In addition, you must do these things in the Modified Version: You may extract a single document from such a collection, and dis- and if all works that were first published under this License somewhere
A ”Transparent” copy of the Document means a machine-readable copy, tribute it individually under this License, provided you insert a copy other than this MMC, and subsequently incorporated in whole or in
represented in a format whose specification is available to the general * A. Use in the Title Page (and on the covers, if any) a title dis- of this License into the extracted document, and follow this License part into the MMC, (1) had no cover texts or invariant sections, and
public, that is suitable for revising the document straightforwardly tinct from that of the Document, and from those of previous versions in all other respects regarding verbatim copying of that document. 7. (2) were thus incorporated prior to November 1, 2008.
with generic text editors or (for images composed of pixels) generic (which should, if there were any, be listed in the History section of the AGGREGATION WITH INDEPENDENT WORKS
paint programs or (for drawings) some widely available drawing edi- Document). You may use the same title as a previous version if the The operator of an MMC Site may republish an MMC contained in
tor, and that is suitable for input to text formatters or for automatic original publisher of that version gives permission. * B. List on the A compilation of the Document or its derivatives with other separate the site under CC-BY-SA on the same site at any time before August
translation to a variety of formats suitable for input to text formatters. Title Page, as authors, one or more persons or entities responsible for and independent documents or works, in or on a volume of a storage or 1, 2009, provided the MMC is eligible for relicensing. ADDENDUM:
A copy made in an otherwise Transparent file format whose markup, authorship of the modifications in the Modified Version, together with distribution medium, is called an ”aggregate” if the copyright resulting How to use this License for your documents
or absence of markup, has been arranged to thwart or discourage sub- at least five of the principal authors of the Document (all of its prin- from the compilation is not used to limit the legal rights of the com-
sequent modification by readers is not Transparent. An image format cipal authors, if it has fewer than five), unless they release you from pilation’s users beyond what the individual works permit. When the
is not Transparent if used for any substantial amount of text. A copy To use this License in a document you have written, include a copy
this requirement. * C. State on the Title page the name of the pub- Document is included in an aggregate, this License does not apply to
that is not ”Transparent” is called ”Opaque”. of the License in the document and put the following copyright and
lisher of the Modified Version, as the publisher. * D. Preserve all the the other works in the aggregate which are not themselves derivative
license notices just after the title page:
copyright notices of the Document. * E. Add an appropriate copyright works of the Document.
Examples of suitable formats for Transparent copies include plain notice for your modifications adjacent to the other copyright notices.
ASCII without markup, Texinfo input format, LaTeX input for- * F. Include, immediately after the copyright notices, a license notice Copyright (C) YEAR YOUR NAME. Permission is granted to copy,
If the Cover Text requirement of section 3 is applicable to these copies distribute and/or modify this document under the terms of the GNU
mat, SGML or XML using a publicly available DTD, and standard- giving the public permission to use the Modified Version under the of the Document, then if the Document is less than one half of the
conforming simple HTML, PostScript or PDF designed for human terms of this License, in the form shown in the Addendum below. * Free Documentation License, Version 1.3 or any later version pub-
entire aggregate, the Document’s Cover Texts may be placed on cov- lished by the Free Software Foundation; with no Invariant Sections,
modification. Examples of transparent image formats include PNG, G. Preserve in that license notice the full lists of Invariant Sections ers that bracket the Document within the aggregate, or the electronic
XCF and JPG. Opaque formats include proprietary formats that can and required Cover Texts given in the Document’s license notice. * no Front-Cover Texts, and no Back-Cover Texts. A copy of the license
equivalent of covers if the Document is in electronic form. Otherwise is included in the section entitled ”GNU Free Documentation License”.
be read and edited only by proprietary word processors, SGML or H. Include an unaltered copy of this License. * I. Preserve the section they must appear on printed covers that bracket the whole aggregate.
XML for which the DTD and/or processing tools are not generally Entitled ”History”, Preserve its Title, and add to it an item stating at 8. TRANSLATION
available, and the machine-generated HTML, PostScript or PDF pro- least the title, year, new authors, and publisher of the Modified Ver- If you have Invariant Sections, Front-Cover Texts and Back-Cover
duced by some word processors for output purposes only. sion as given on the Title Page. If there is no section Entitled ”History” Texts, replace the ”with … Texts.” line with this:
in the Document, create one stating the title, year, authors, and pub- Translation is considered a kind of modification, so you may distribute
lisher of the Document as given on its Title Page, then add an item translations of the Document under the terms of section 4. Replacing
The ”Title Page” means, for a printed book, the title page itself, plus Invariant Sections with translations requires special permission from with the Invariant Sections being LIST THEIR TITLES, with the
such following pages as are needed to hold, legibly, the material this describing the Modified Version as stated in the previous sentence. * Front-Cover Texts being LIST, and with the Back-Cover Texts being
J. Preserve the network location, if any, given in the Document for their copyright holders, but you may include translations of some or all
License requires to appear in the title page. For works in formats Invariant Sections in addition to the original versions of these Invari- LIST.
which do not have any title page as such, ”Title Page” means the text public access to a Transparent copy of the Document, and likewise the
network locations given in the Document for previous versions it was ant Sections. You may include a translation of this License, and all the
near the most prominent appearance of the work’s title, preceding the license notices in the Document, and any Warranty Disclaimers, pro- If you have Invariant Sections without Cover Texts, or some other
beginning of the body of the text. based on. These may be placed in the ”History” section. You may omit
a network location for a work that was published at least four years vided that you also include the original English version of this License combination of the three, merge those two alternatives to suit the sit-
before the Document itself, or if the original publisher of the version it and the original versions of those notices and disclaimers. In case of a uation.
The ”publisher” means any person or entity that distributes copies of refers to gives permission. * K. For any section Entitled ”Acknowledge- disagreement between the translation and the original version of this
the Document to the public. ments” or ”Dedications”, Preserve the Title of the section, and preserve License or a notice or disclaimer, the original version will prevail. If your document contains nontrivial examples of program code, we
in the section all the substance and tone of each of the contributor ac- recommend releasing these examples in parallel under your choice of
A section ”Entitled XYZ” means a named subunit of the Document knowledgements and/or dedications given therein. * L. Preserve all If a section in the Document is Entitled ”Acknowledgements”, ”Dedi- free software license, such as the GNU General Public License, to per-
whose title either is precisely XYZ or contains XYZ in parentheses the Invariant Sections of the Document, unaltered in their text and cations”, or ”History”, the requirement (section 4) to Preserve its Title mit their use in free software.
44.3 GNU Lesser General Public License
GNU LESSER GENERAL PUBLIC LICENSE The “Corresponding Application Code” for a Combined Work means You may convey a Combined Work under terms of your choice that, You may place library facilities that are a work based on the Library
the object code and/or source code for the Application, including any taken together, effectively do not restrict modification of the portions side by side in a single library together with other library facilities that
Version 3, 29 June 2007 data and utility programs needed for reproducing the Combined Work of the Library contained in the Combined Work and reverse engineering are not Applications and are not covered by this License, and convey
from the Application, but excluding the System Libraries of the Com- for debugging such modifications, if you also do each of the following: such a combined library under terms of your choice, if you do both of
bined Work. 1. Exception to Section 3 of the GNU GPL. the following:
Copyright © 2007 Free Software Foundation, Inc. <[Link]
You may convey a covered work under sections 3 and 4 of this License * a) Give prominent notice with each copy of the Combined Work * a) Accompany the combined library with a copy of the same work
Everyone is permitted to copy and distribute verbatim copies of this without being bound by section 3 of the GNU GPL. 2. Conveying that the Library is used in it and that the Library and its use are based on the Library, uncombined with any other library facilities,
license document, but changing it is not allowed. Modified Versions. covered by this License. * b) Accompany the Combined Work with a conveyed under the terms of this License. * b) Give prominent no-
copy of the GNU GPL and this license document. * c) For a Com- tice with the combined library that part of it is a work based on the
This version of the GNU Lesser General Public License incorporates If you modify a copy of the Library, and, in your modifications, a facil- bined Work that displays copyright notices during execution, include Library, and explaining where to find the accompanying uncombined
the terms and conditions of version 3 of the GNU General Public Li- ity refers to a function or data to be supplied by an Application that the copyright notice for the Library among these notices, as well as a form of the same work.
cense, supplemented by the additional permissions listed below. 0. uses the facility (other than as an argument passed when the facility reference directing the user to the copies of the GNU GPL and this
Additional Definitions. is invoked), then you may convey a copy of the modified version: license document. * d) Do one of the following: o 0) Convey the
Minimal Corresponding Source under the terms of this License, and 6. Revised Versions of the GNU Lesser General Public License.
As used herein, “this License” refers to version 3 of the GNU Lesser * a) under this License, provided that you make a good faith effort to the Corresponding Application Code in a form suitable for, and un-
General Public License, and the “GNU GPL” refers to version 3 of the ensure that, in the event an Application does not supply the function der terms that permit, the user to recombine or relink the Application The Free Software Foundation may publish revised and/or new ver-
GNU General Public License. or data, the facility still operates, and performs whatever part of its with a modified version of the Linked Version to produce a modified sions of the GNU Lesser General Public License from time to time.
purpose remains meaningful, or * b) under the GNU GPL, with none Combined Work, in the manner specified by section 6 of the GNU Such new versions will be similar in spirit to the present version, but
of the additional permissions of this License applicable to that copy. GPL for conveying Corresponding Source. o 1) Use a suitable shared may differ in detail to address new problems or concerns.
“The Library” refers to a covered work governed by this License, other
library mechanism for linking with the Library. A suitable mechanism
than an Application or a Combined Work as defined below.
is one that (a) uses at run time a copy of the Library already present
3. Object Code Incorporating Material from Library Header Files. on the user’s computer system, and (b) will operate properly with a Each version is given a distinguishing version number. If the Library
An “Application” is any work that makes use of an interface provided modified version of the Library that is interface-compatible with the as you received it specifies that a certain numbered version of the GNU
by the Library, but which is not otherwise based on the Library. Defin- The object code form of an Application may incorporate material from Linked Version. * e) Provide Installation Information, but only if you Lesser General Public License “or any later version” applies to it, you
ing a subclass of a class defined by the Library is deemed a mode of a header file that is part of the Library. You may convey such object would otherwise be required to provide such information under section have the option of following the terms and conditions either of that
using an interface provided by the Library. code under terms of your choice, provided that, if the incorporated ma- 6 of the GNU GPL, and only to the extent that such information is published version or of any later version published by the Free Software
terial is not limited to numerical parameters, data structure layouts necessary to install and execute a modified version of the Combined Foundation. If the Library as you received it does not specify a version
A “Combined Work” is a work produced by combining or linking an and accessors, or small macros, inline functions and templates (ten or Work produced by recombining or relinking the Application with a number of the GNU Lesser General Public License, you may choose
Application with the Library. The particular version of the Library fewer lines in length), you do both of the following: modified version of the Linked Version. (If you use option 4d0, the any version of the GNU Lesser General Public License ever published
with which the Combined Work was made is also called the “Linked Installation Information must accompany the Minimal Corresponding by the Free Software Foundation.
Version”. * a) Give prominent notice with each copy of the object code that the Source and Corresponding Application Code. If you use option 4d1,
Library is used in it and that the Library and its use are covered by you must provide the Installation Information in the manner specified
If the Library as you received it specifies that a proxy can decide
this License. * b) Accompany the object code with a copy of the GNU by section 6 of the GNU GPL for conveying Corresponding Source.)
The “Minimal Corresponding Source” for a Combined Work means the whether future versions of the GNU Lesser General Public License
Corresponding Source for the Combined Work, excluding any source GPL and this license document. shall apply, that proxy’s public statement of acceptance of any ver-
code for portions of the Combined Work that, considered in isolation, sion is permanent authorization for you to choose that version for the
are based on the Application, and not on the Linked Version. 4. Combined Works. 5. Combined Libraries. Library.

You might also like