Chapter 2 Introduction to Java Applications 71 are (Hosting your own web site)

Chapter 2 Introduction to Java Applications 71 are declarations. The words firstNumber and secondNumber are the names of variables. A variable is a location in the computer s memory where a value can be stored for use by a program. All variables must be declared with a name and a data type before they can be used in a program. This declaration specifies that the variables firstNumber and secondNumber are data of type String (located in package java.lang), which means that the variables will hold strings. A variable name can be any valid identifier. Like statements, declarations end with a semicolon (;). Notice the single-line comments at the end of each line. This use and placement of the comments is a common practice used by programmers to indicate the purpose of each variable in the program. Good Programming Practice 2.12 Choosing meaningful variable names helps a program to be self-documenting (i.e., it becomes easier to understand the program simply by reading it rather than by reading manuals or viewing an excessive number of comments). Good Programming Practice 2.13 By convention, variable-name identifiers begin with a lowercase letter. As with class names, every word in the name after the first word should begin with a capital letter. For example, identifier firstNumber has a capital Nin its second word, Number. Good Programming Practice 2.14 Some programmers prefer to declare each variable on a separate line. This format allows for easy insertion of a descriptive comment next to each declaration. Software Engineering Observation 2.2 Java automatically imports classes from package java.lang, such as class String. Therefore, import statements are not required for classes in package java.lang. Declarations can be split over several lines, with each variable in the declaration separated by a comma (i.e., a comma-separated list of variable names). Several variables of the same type may be declared in one declaration or in multiple declarations. Lines 12 13 can also be written as follows: String firstNumber, // first string entered by user secondNumber; // second string entered by user Lines 14 16, int number1; // first number to add int number2; // second number to add int sum; // sum of number1 and number2 declare that variables number1, number2 and sum are data of type int, which means that these variables will hold integer values (whole numbers such as 7, 11, 0 and 31,914). We will soon discuss the data types float and double, for specifying real numbers (numbers with decimal points, such as 3.4, 0.0 and 11.19), and variables of type char, for specifying character data. A char variable may hold only a single lowercase letter, a single uppercase letter, a single digit or a single special character (such as x, $, 7 and *) and escape sequences (such as the newline character, n). Java is capable of representing characters from many other spoken languages. Types such as int, double and char are often called primitive data types, or built-in data types. Primitive-type names are keywords; Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

Leave a Reply