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
materials.
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
5.