The art and science of java

The Art and Science of Java
Preliminary Draft
Eric S. Roberts
Stanford University
Stanford, California
January 2006Preface
This text is an early draft for a general introductory textbook in computer science – a
Java-based version of my 1995 textbook The Art and Science of C. My hope is that I can
Use much of the existing material in writing the new book, although quite a bit of the
Material and overall organization have to change. At this point, the material is still in a
Preliminary form, and the feedback I get from those of you who are taking this course
Will almost certainly lead to some changes before the book is published.
One of the central features of the text is that it incorporates the work of the Association of
Computing Machinery’s Java Task Force, which was convened in 2004 with the
Following charter:
To review the Java language, APIs, and tools from the perspective of introductory

/> Computing education and to develop a stable collection of pedagogical resources that
Will make it easier to teach Java to first-year computing students without having
Those students overwhelmed by its complexity.
I am grateful to my colleagues on the Task Force – Kim Bruce, Robb Cutler, James H.
Cross II, Scott Grissom, Karl Klee, Susan Rodger, Fran Trees, Ian Utting, and Frank
Yellin – for all their hard work over the past year, as well as to the National Science
Foundation, the ACM Education Board, the SIGCSE Special Projects Fund for their
Financial support.
I also want to thank the participants in last year’s CS 298 seminar – Andrew Adams,
Andy Aymeloglu, Kurt Berglund, Seyed Dorminani-Tabatabaei, Erik Forslin, Alex
Himel, Tom Hurlbutt, Dave Myszewski, Ann Pan, Vishakha Parvate, Cynthia Wang, Paul
Wilkins, and Julie Zhuo for helping me work through these ideas. In addition, I would
Like to thank my CS 106A TA Brandon Burr and all the hardworking section-leaders for
Taking on the challenge of helping to teach a course with a just-in-time approach to the
Particularly because my wife Lauren Rusk (who has edited all of my books) has not yet
Had her chance to work her wonderful magic on the language, you may still find some
Rough edges, awkward constructions, and places where real improvement is needed.
Writing is, after all, at least as difficult as programming and requires just as much testing
To get everything right. If you let me know when things are wrong, I think we’ll end up
With a textbook and a course that are exciting, thorough, and practical.
Thanks in advance for all your help.
Eric Roberts
Professor of Computer Science
Stanford University
September 2005Table of Contents
1. Introduction 1
1.1 A brief history of computing 2
1.2 What is computer science? 4
1.3 An overview of computer hardware 5
1.4 Algorithms 7
1.5 Stages in the programming process 8
1.6 Java and the object-oriented paradigm 13
1.7 Java and the World Wide Web 17
2. Programming by Example 21
2.1 The “hello world” program 22
2.2 Perspectives on the programming process 26
2.3 A program to add two numbers 26
2.4 Classes and objects 31
3. Expressions 39
3.1 Primitive data types 41
3.2 Constants and variables 42
3.3 Operators and operands 46
3.4 Assignment statements 53
3.5 Programming idioms and patterns 56
4. Statement Forms 63
4.1 Simple statements 64
4.2 Control statements 66
4.3 Boolean data 67
4.4 The if statement 73
4.5 The switch statement 78
4.6 The concept of iteration 79
4.7 The while statement 85
4.8 The for statement 90