ch0 - CHAPTER 0 INTRODUCTION TO JAVA This is a book about...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CHAPTER 0 INTRODUCTION TO JAVA This is a book about programming: specifically, about understanding and using data structures and algorithms. The Java Collections Framework has a considerable number of data structures and algorithms. Subsequent chapters will focus on what the framework is and how to use the framework in your programs. For this information to make sense to you, you will need to be familiar with certain aspects of Java that we present in this chapter. All of the material is needed, either for the framework itself or to enable you to use the framework in your programming projects. CHAPTER OBJECTIVES 1. Learn (or review) the fundamentals of Java, including classes, objects and messages. 2. Be able to use javadoc in writing method specifications. 3. Incorporate the Scanner class into your programming. 4. Understand the significance of the fact that a copy of the argument is stored in the corresponding parameter when a method is called. 5. Understand the details of arrays and output formatting. 0.1 JAVA FUNDAMENTALS Every Java program consists of a collection of classes. Basically, a class consists of variables, called fields , together with functions, called methods , that operate on those fields. A program is executed when a special method, the main method, is called by the run-time system (also known as the Java Virtual Machine). The heading of this method is fixed, as shown in the following program: public class HelloWorld { public static void main (String [ ] args) 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
{ System.out.println (“Hello, world!”); } // method main } // class HelloWorld The main method in this program calls another method, println , to produce the following output to the console window: Hello, world! Console output, that is, output to the console window on your monitor, is handled by the methods System.out.print , System.out.println , and System.out.printf (see Section 0.5). 0.1.1 PRIMITIVE TYPES A primitive type is a collection of values, together with operations that can be performed on those values. For example, the reserved word int denotes the primitive type whose values are integers in the range from about –2 billion to 2 billion, and whose operations are addition, subtraction, and so on. A variable – also called an instance – of type int is a location in computer memory that can hold one value of type int . The term “variable” is used because the value stored can change during the execution of an instruction. Instead of specifying the location’s address, we provide an identifier (that is, a name) and the Java compiler associates the identifier with a location. For example, here is a declaration for an int variable whose identifier is score : int score; By a standard abuse of language, we say that score is a variable instead of saying that score is an identifier for a variable. An assignment statement allows us to store a value in a variable. For example, score = 0; stores the value 0 in the variable score .
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This test prep was uploaded on 04/21/2008 for the course CS 103 taught by Professor Billcollins during the Spring '08 term at Lafayette.

Page1 / 32

ch0 - CHAPTER 0 INTRODUCTION TO JAVA This is a book about...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online