INTRODUCTION: |
As with most high level languages, Java provides standard data types to store information. Java is a richly typed language which gives the programmer a wide variety of data types to use. In this lesson you will declare variables, store values in them, and print out their values using the System.out object. The key topics for this lesson are:
A. Identifiers in Java |
||||||||||||||||||||||||||||||||||||||||||||
VOCABULARY: |
IDENTIFIER |
KEYWORDS |
|||||||||||||||||||||||||||||||||||||||||||
DISCUSSION: |
A. Identifiers in Java 1. An identifier is a name that will be used to describe classes, methods, constants, variables, and other items. 2. The rules for writing identifiers in Java are: 3. Java is a case sensitive language. Java will distinguish between upper
and lower case letters in identifiers. Therefore: 4. A good identifier should also be a mnemonic device which helps describe
the nature or purpose of that function or variable. It is better to use Remember that our goal is to write code that is easy to read and professional in nature. 5. Programmers will adopt different styles of using upper and lower case letters in writing identifiers. The reserved keywords in Java must be typed in lower case text, but identifiers can be typed using any combination of upper and lower case letters. 6. The following conventions will be used throughout the curriculum guide: B. Basic Data Types in Java 1. Java provides eight primitive data types: byte, short, int, long, float, double, char and a boolean. a. The data types byte, short, int, and long are for integers. 2. Integer type - any positive or negative number without a decimal point. 3. Floating Point type - any signed or unsigned number with a decimal point.
A floating point value can be specified by A floating point value cannot contain a comma or $ symbol. e.g. 1,234.56 $66.95 4. The following table summarizes the bytes allocated and the resulting size.
5. Character type - letters, digits 0..9, and punctuation symbols.
6. Data types are provided by high level languages to minimize memory usage and processing time. Integers and characters require less memory and are easier to process. Floating point values require more memory and time to process. 7. The final primitive is the type boolean. It is used to represent a single
true/false value. A boolean value can have only one of two values: C. Declaring and Initializing Variables in Java 1. A variable must be declared before it can be initialized with a value. The general
syntax of variable declarations is: 2. Variables can be declared near the top of the method or in the midst of writing code. Variables can also be declared and assigned a value in one line. The following example program illustrates these aspects of variable declaration and assignments. Program 3-1 public class DeclareVar { public static void main(String[] args) { // first is declared and initialized // second is just intialized int first, second; double x = 2.5; char ch; bool done; first = 5; second = 7; x = 2.5; ch = 'T'; done = false; int sum = first + second; } } In Program 3-1, two variables, first and second, are declared on the same line, the variable x is declared and assigned the value 2.5, and the variable sum is declared and assigned the computed value first + second. 3. Where the variables are declared is a matter of programming style. Your instructor will probably have some preferences regarding this matter. D. Printing Variables Using the System.out Object 1. The System.out object is defined in each Java program. It has methods for displaying text strings and numbers in plain text format on the system display (console). For example: Program 3-2 public class PrintVar { public static void main(String[] args) { int number = 5; char letter = 'E'; double average = 3.95; boolean done = false; System.out.println("number = " + number); System.out.println("letter = " + letter); System.out.println("average = " + average); System.out.println("done = " + done); System.out.print("The "); System.out.println("End!"); } } Run output: number = 5 letter = E average = 3.95 done = false The End! 2. Method System.out.println displays (or prints) a line of text in the console window. When System.out.println completes its task, it automatically positions the output cursor (the location where the next character will be displayed) to the beginning of the next line in the console window (this is similar to pressing the Enter key when typing in a text editor—the cursor is repositioned at the beginning of the next line in your file). 3. The expression "number = " + number from the statement System.out.println("number = " + number); uses the + as an operator to “link” or concatenate two strings. The result of this operation is a new, and normally longer string. String concatenation is discussed in more detail in the next lesson. 4. The lines System.out.print("The "); System.out.println("End!"); of Program 3-2 display one line in the console window. The first statement uses System.out’s method print to display a string. Unlike println, print displays the argument and positions the cursor after the last displayed character in the console window. 5. Note the distinction between sending a text constant, "number = ", versus a variable, number, to the System.out object. Each variable will be printed out as represented by its declared data type, integer variables printed out like integers, boolean as true/false, and so forth. E. ASCII Code Values and Character Data 1. As mentioned earlier in section B.5, a character value can easily be converted to its corresponding ASCII integer value. 2. A character value is stored using two bytes of memory, which consists of 16 bits of binary (0 or 1) values. 3. The letter 'A' has the ASCII value of 65, which is stored as the binary value 0000000001000001. This is illustrated in the following program fragment: char letter = 'A'; System.out.println("letter = " + letter); System.out.print("its ASCII position = "); System.out.print((int)letter); Run output: letter = A its ASCII position = 65 The statement (int)letter is called a type conversion. The data type of the variable is converted to the prefix parenthesized type (int), if possible. 4. In Java, you can make a direct assignment of a character value to an integer variable, and vice versa. This is possible because both an integer and a character variable are ultimately stored in binary. However, it is better to be more explicit about such conversions by using type conversions. For example, the two lines of code below assign position the ASCII value of letter. char letter = 'C'; // ASCII value = 67 int position; position = letter; // This is legal, position now equals 67 vs. position = (int)letter; // This is easier to understand. F. Assignment Statements and Math Operators 1. An assignment statement has the following basic syntax:
a. The a = 5; also results in the value 5. This allows for chaining of assignment operators. a = b = 5; The assignment operator (=) is right-associative. This means that the above statement is really solved in this order: a = (b = 5);// solved from right to left. Since (b = 5) returns the integer 5, the value 5 is also assigned to variable a. 2. Java provides 5 math operators as listed below: 3. The numerical result and data type of the answer depends on the type of operands used in a problem. 4. For all the operators, if both operands are integers, the result is an integer. Examples: 2 + 3 = 5 (integer) 9 - 3 = 6 (integer) 4 * 8 = 32 (integer) 11/2 = 5 (integer) 5. If either of the operands is a float type, the result is a float type. Examples: 2 + 3.000 = 5.000 (float) 25 / 6.75 = 3.7037 (float)
a. When an integer and a float are used in a binary math expression, the
integer is promoted to a float value, then the math is executed. 6. The modulus operator (%) returns the remainder of dividing the first operand by the second. For example: 10 % 3 = 1 2 % 4 = 2 16 % 2 = 0 27.475 % 7.22 = 5.815 7. Changing the sign of a value can be accomplished with the negation operator (-), often called the unary (-) operator. A unary operator works with only one value. Applying the negation operator to an integer returns an integer, while applying it to a float returns a float value. For example: -(67) = -67 -(-2.345) = 2.345 8. To obtain the fractional answer to a question like 11/2 = 5.5, a type conversion must be applied to one of the operands. (double)11/2 results in 5.5 11.000/2 then we solve division 5.5 The type conversion operators are unary operators with the following syntax: (type) operand 9. There is more to cover regarding operators in Java. Topics such as math precedence and assignment operators will be covered later. | ||||||||||||||||||||||||||||||||||||||||||||
SUMMARY/ REVIEW: |
This lesson has covered a great amount of detail regarding the Java language. At first you will have to memorize the syntax of data types, but with time and practice fluency will come. |