1.1. Comments
Three permissible styles of comment in a Java technology program are:
// comment on one line
/* comment on one
or more lines */
/** documenting comment */ use the javadoc tool to create documentations
1.2. Identifiers
Identifiers are used to identify variables, classes, or methods. Simply speaking, identifiers are names given to variables, classes, or methods.
A. Identifiers are composed with letters (characters), numbers, underscores (_), or dollar signs ($), but cannot start with number. Java language uses Unicode characters. So letters are not only Latin letters but also characters in other languages, such as Chinese characters.
B. Other signs are not accepted.
C. Identifiers are case sensitive. Upper case and lower case are not the same.
D. Identifiers have no maximum length. We can create an identifier as long as we want without limitation.
1.3. Keywords and reserved words
Keywords are some words which are for special use in Java languages. These words cannot be used as identifiers or for others purposes. Keywords are all lower cases. There are 53 keywords and reserved words in Java language:
abstract: an abstract class or method
assert: used to locate internal program errors
boolean: the Boolean type
break: breaks out of a switch or loop
byte: the 8-bit integer type
case: a case of a switch
catch: the clause of a try block catching an exception
char: the Unicode character type
class: defines a class type
const: not used
continue: continues at the end of a loop
default: the default clause of a switch
do: the top of a do/while loop
double: the double-precision floating-number type
else: the else clause of an if statement
extends: defines the parent class of a class
final: a constant, or a class or method that cannot be overridden
finally: the part of a try block that is always executed
float: the single-precision floating-point type
for: a loop type
goto: not used
if: a conditional statement
implements: defines the interface(s) that a class implements
import: imports a package
instanceof: tests if an object is an instance of a class
int: the 32-bit integer type
interface: an abstract type with methods that a class can implement
long: the 64-bit long integer type
native: a method implemented by the host system
new: allocates a new object or array
null: a null reference
package: a package of classes
private: a feature that is accessible only by methods of this class
protected: a feature that is accessible only by methods of this class, its children, and other classes in the same package
public: a feature that is accessible by methods of all classes
return: returns from a method
short: the 16-bit integer type
static: a feature that is unique to its class, not to objects of its class
strictfp: Use strict rules for floating-point computations
super: the superclass object or constructor
switch: a selection statement
synchronized: a method or code block that is atomic to a thread
this: the implicit argument of a method, or a constructor of this class
throw: throws an exception
throws: the exceptions that a method can throw
transient: marks data that should not be persistent
try: a block of code that traps exceptions
void: denotes a method that returns no value
volatile: ensures that a field is coherently accessed by multiple threads
while: a loop
1.4. Types
Types are primitive types, and classes and objects. We are only discussing primitive types.
1.4.1. Logical Types
The boolean data type has two literals, true and false. Integers cannot sign a boolean variable.
1.4.2. Numeric Types
1.4.3.
char
The char data type is a special numeric type. It can be considered as textual because it represents a 16-bit Unicode character. Its literal can be an unsigned short integer (16 bits) or a character enclosed in single quotes (’ ’).
Integral – byte, short, int, and long
A. The default is int. If an integer does not define a type, it must be int type.
B. Uses three forms – Decimal, octal, or hexadecimal
C. Defines long by using the letter L or l: long num = 1000L
Floating Point – float and double
A. The default is double
B. Defines float by using the letter F or f:
1.4.4. Declare and assign variables
int a; /*declare an int variable*/ a = 10; /*assign value to the variable*/
int b = 5; // declare and assign int
In a method, variables of primitive types are allocated memory spaces while declaring. So, variables of primitive types must be assigned in a method.
1.4.5. Convert types
Automatic
byte --> short(char) --> int --> long --> float --> double
forcible
byte <-- short(char) <-- int <-- long <-- float <-- double
1.4.6. String
String is a class, not a primitive type. But it can be used like a primitive type.
String str = “Hello”; // declare like a primitive type
String str = new String(“Hello”); // declare like an object.
But the two way are not the completely same. The first way will allocate memory space in a String pool, and the other way will allocate memory space directly.
String is an class. So however to declare the variable, the variable is always an object. Two Sting variables cannot use “==” to compare, because it will compare the memory space addresses, not values.
The differences between primitive types and classes while declaring and assigning:
Declaration of primitive types allocates memory space
Declaration of nonprimitive types (classes) does not allocate memory space
Declared object variables are not the data itself, but references (or pointers) to the data
An object declaration allocates storage only for a reference and use the new operator to allocate and initialize storage