A literal is an explicit value that is used by a program. For example, your program may include a literal value of 3.1415 that is used whenever the value of pi is necessary, or it may include 65 as the mandatory retirement age. These values, 3.1415 and 65, are both literals.
Integer Literals
Integer literals can be specified in decimal, hexadecimal, or octal notation. To specify a decimal value, simply use the number as normal. To indicate that a literal value is a long, you can append either "L" or "l" to the end of the number. Hexadecimal values are given in base 16 and include the digits 0-9 and the letters A-F. To specify a hexadecimal value, use 0x followed by the digits and letters that comprise the value. Similarly, an octal value is identified by a leading 0 symbol.
For examples of specifying integer literals, see Table 3.7.
Floating-Point Literals
Similar to integer literals are Java's floating-point literals. Floating-point literals can be specified in either the familiar decimal notation (for example, 3.1415) or exponential notation (for example, 6.02e23). To indicate that a literal is to be treated as a single precision float, append either "f" or "F". To indicate that it is to be treated as a double precision value, append either "d" or "D".
Java includes predefined constants, POSITIVE_INFINITY, NEGATIVE_INFINITY, and NaN, to represent the infinity and not-a-number values.
The following list shows some valid floating-point literals:
43.3F3.1415d-12.123f6.02e+23f6.02e23d6.02e-23f6.02e23d
Boolean Literals
Java supports two Boolean literals-trueand false.
Character Literals
A character literal is a single character or an escape sequence enclosed in single quotes, for example, 'b'. Escape sequences are used to indicate special characters or actions, such as line feed, form feed, or carriage return. The available escape sequences are shown in Table 3.8. For examples of character literals, consider the following:
'b''\n'\u15e''\t'
String Literals
Although there is no string primitive type in Java, you can include string literals in your programs. Most applications and applets will make use of some form of string literal, probably at least for error messages. A string literal consists of zero or more characters (including the escape sequences shown in Table 3.8) enclosed in double quotes. As examples of string literals, consider the following:
"A String"
"Column 1\tColumn 2"
"First Line\r\nSecond Line"
"First Page\fSecond Page"
""
Because Java does not have a string primitive type, each use of a string literal causes an object of the Stringclass to be created behind the scenes. However, because of Java's automatic memory management, your program doesn't need to do anything special to free or release the memory used by the literal or string once you are finished with it.
Arrays
In Java you declare an array using enclosing square bracket symbols ([]). For example, consider the following array declarations:
int intArray[];float floatArray[];double [] doubleArray;char charArray[];
Notice that the brackets can be placed before or after the variable name. Placing the [] after the variable name follows the conventions of C, and if you are coming to Java from C or C++, you may want to continue that tradition. However, there is an advantage to placing the brackets before the variable name. By placing the brackets in front of the variable name, you can more easily declare multiple arrays. For example, consider the following declarations:
int [] firstArray, secondArray;int thirdArray[], justAnInt;
On the first line both firstArrayand secondArray are arrays. On the second line, thirdArrayis an array but justAnIntis, as its name implies, a lone integer. The ability to declare singleton variables and arrays in the same statement, as on the second line in the preceding example, is the source of many problems in other programming languages. Java helps prevent this type of problem by providing an easy, alternative syntax for declaring arrays.
Allocation
Once an array is declared, it must be allocated. You probably noticed that the size of the arrays has not been specified in the examples so far. This is because, in Java, all arrays must be allocated with new. Declaring the following array would have resulted in a compile-time error:
int intArray[10]; // this is an error
To allocate an array you use new, as shown in the following examples:
int intArray[] = new int[100];float floatArray[];floatArray = new float[100];long [] longArray = new long[100];double [][] doubleArray = new double[10][10];
Initialization
An alternative way of allocating a Java array is to specify a list of element initializers when the array is declared. This is done as follows:
int intArray[] = {1,2,3,4,5};char [] charArray = {'a', 'b', 'c'};String [] stringArray = {"A", "Four", "Element", "Array"};
In this case, intArray will be a five-element array holding the values 1 through 5. The three-element array charArray will hold the characters 'a', 'b', and 'c'. Finally, stringArraywill hold the strings shown.
Array Access
Items in a Java array are known as the components of the array. You can access a component at runtime by enclosing the component number you want to access with brackets as shown in the following:
int intArray[] = {100, 200, 300, 400, 500};int a = intArray[0]; // a will be equal to 100int b = intArray[1]; // b will be equal to 200int c = intArray[2]; // c will be equal to 300int d = intArray[3]; // d will be equal to 400int e = intArray[4]; // e will be equal to 500
No comments:
Post a Comment