The art and science of java

The Art and Science of JavaPreliminary DraftEric S. RobertsStanford UniversityStanford, CaliforniaJanuary 2006PrefaceThis text is an early draft for a general introductory textbook in computer science — aJava-based version of my 1995 textbook The Art and Science of C. My hope is that I canuse much of the existing material in writing the new book, although quite a bit of thematerial and overall organization have to change. At this point, the material is still in apreliminary form, and the feedback I get from those of you who are taking this coursewill 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 ofComputing Machinery’s Java Task Force, which was convened in 2004 with thefollowing charter:To review the Java language, APIs, and tools from the perspective of introductorycomputing education and to develop a stable collection of pedagogical resources thatwill make it easier to teach Java to first-year computing students without havingthose 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 FrankYellin — for all their hard work over the past year, as well as to the National ScienceFoundation, the ACM Education Board, the SIGCSE Special Projects Fund for theirfinancial 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, AlexHimel, Tom Hurlbutt, Dave Myszewski, Ann Pan, Vishakha Parvate, Cynthia Wang, PaulWilkins, and Julie Zhuo for helping me work through these ideas. In addition, I wouldlike to thank my CS 106A TA Brandon Burr and all the hardworking section-leaders fortaking on the challenge of helping to teach a course with a just-in-time approach to thematerials.Particularly because my wife Lauren Rusk (who has edited all of my books) has not yethad her chance to work her wonderful magic on the language, you may still find somerough 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 testingto get everything right. If you let me know when things are wrong, I think we’ll end upwith a textbook and a course that are exciting, thorough, and practical.Thanks in advance for all your help.Eric RobertsProfessor of Computer ScienceStanford UniversitySeptember 2005Table of Contents1. Introduction 11.1 A brief history of computing 21.2 What is computer science? 41.3 An overview of computer hardware 51.4 Algorithms 71.5 Stages in the programming process 81.6 Java and the object-oriented paradigm 131.7 Java and the World Wide Web 172. Programming by Example 212.1 The “hello world” program 222.2 Perspectives on the programming process 262.3 A program to add two numbers 262.4 Classes and objects 313. Expressions 393.1 Primitive data types 413.2 Constants and variables 423.3 Operators and operands 463.4 Assignment statements 533.5 Programming idioms and patterns 564. Statement Forms 634.1 Simple statements 644.2 Control statements 664.3 Boolean data 674.4 The if statement 734.5 The switch statement 784.6 The concept of iteration 794.7 The while statement 854.8 The for statement 905.