|
This chapter covers a part of the fourth SCJP objective, specifically sections 4.4, 4.6, 1.1 and part of 4.5. We learned about the Java keywords and identifiers. We also learned how to distinguish between a keyword and an identifier. We quickly overviewed the primitive types and their ranges. We then learned the various types of literals that can appear in Java code and their validity. Finally, we took a close look at the arrays, their construction, initialization and usage in Java programming. This is a brief summary of the important concepts in this chapter.
q Keywords and Identifiers
§ All Java keywords are always in a lower case.
§ Identifiers must start with either a letter, $ or _ (underscore) and can have additional letters, $, _ or digits in it.
§ An identifier should not be a Java keyword or a Java reserved word.
q Literals
§ Literals are valid values, which can be directly typed in the Java code.
§ Java has two types of literals, the primitive literals and string literals. The primitive literals may be of boolean, character, or integral literals.
q Boolean Literals
§ There are two boolean literals, true and false.
§ Boolean literals are case sensitive. (For example, tRue, True, FALSE are all invalid literals)
q Character Literals
§ Character literals are defined as a character in single quotes (For example ‘a’).
§ Character literals can be represented using hexadecimal values (For example ‘\u19ac’).
§ Character literals can be some special characters (For example ‘\n’).
q Integral Literals
By default, integral literals are of data type int and they represent a value in decimal number system. The integral literals can also be represented in octal or hexadecimal number systems.
q Octal Literal
§ An octal literal is identified when there is 0 (zero) at the start of the literal.
§ Octal literals cannot have digit 8 and 9 in it because there is no single digit above 7 in octal system.
q Hexadecimal Literal
§ Integral literals can also represent the hexadecimal value.
§ The hex literals are case insensitive. (For example 0xabcd and 0xAbcD are both same.)
q Floating Point Literals
§ By default, they are of type double. (For example, literal 12.09 in if(rate == 12.09) is of type double) (d or D is optional and redundant).
§ If you want to use a float literal, you must append an F or f to the value.
q String Literals
§ String literals are represented as a sequence of characters enclosed in double quotes.
q Primitive data types and their ranges
§ Primitive data types can be classified as signed, unsigned numeric types and boolean type.
§ The byte, short, int, long, float and double are signed numeric types.
The signed numbers are represented in 2's complement form.
You can use the formula –2(n-1) to 2(n-1) -1 to determine the
range of a signed integral types byte, short int and long.
§ The char represents unsigned integral.
§ Primitive data type boolean can take boolean literals, true or false, as values.
q Arrays
§ Array object is a fixed-sized ordered grouping of either primitives or object references or other arrays.
§ Array elements must be of the same type.
§ Arrays must be declared, created and initialized before their first use.
§ Size of array is not required and is not specified at the time of its declaration. It must be specified at the time of its construction.
§ Arrays are automatically initialized at the time of construction with new keyword. Each element is initialized to a default value.
§ Arrays can also be explicitly initialized at the time of their construction with curly brackets.
§ Array is a subclass of java.lang.Object and understands all the methods of that class.
§ The array elements are accessible by index, or position. Array index starts at 0.
§ Array’s length member variable tells the size of that array.
§ Multidimensional arrays are array of arrays. Each sub-array may be of different size.