Constructor
INSTANCE INITIALIZER
Instance initializer are code blocks appear outside a method.
ex:
1: public class Bird {
2: public static void main(String[] args) {
3: { System.out.println("Feathers"); }
4: }
5: { System.out.println("Snowy"); } //instance initializer
6: }ORDER OF INITIALIZATION
The Primitive Types
SIGNED AND UNSIGNED: SHORT AND CHAR
short bird = 'd'; char mammal = (short)83; System.out.println(bird); // Prints 100 System.out.println(mammal); // Prints S short reptile = 65535; // DOES NOT COMPILE char fish = (short)-1; // DOES NOT COMPILE
FLOATING-POINT NUMBERS AND SCIENTIFIC NOTATION
for the exam you are not required to know scientific notation or how floating-point values are stored.
Writing Literals
By default, Java assumes you are defining an int value with a numeric literal.
long max = 3123456789; // DOES NOT COMPILE, out of range for int long max = 3123456789L; // postfix L, now Java knows it is a long
Java allows you to specify digits in several other formats:
Literals and the Underscore Character
Ex:
double notAtStart = _1000.00; // DOES NOT COMPILE double notAtEnd = 1000.00_; // DOES NOT COMPILE double notByDecimal = 1000_.00; // DOES NOT COMPILE double annoyingButLegal = 1_00_0.0_0; // Ugly, but compiles double reallyUgly = 1\_\_\_\_\_\_\_\_\_\_2; // Also compiles
Reference Type
DISTINGUISHING BETWEEN PRIMITIVES AND REFERENCE TYPES
4 Legal Identifiers Rules:
Legal?
long okidentifier; float $OK2Identifier; boolean _alsoOK1d3ntifi3r; char \_\_SStillOkbutKnotsonice$;
Legal?
int 3DPointClass; byte hollywood@vine;_ String *$coffee; double public; short _;
int 3DPointClass; // identifiers cannot begin with a number byte hollywood@vine; // @ is not a letter, digit, $ or _ String *$coffee; // * is not a letter, digit, $ or _ double public; // public is a reserved word short _; // a single underscore is not allowed
IDENTIFIERS IN THE REAL WORLD
THIS_IS_A_CONSTANTDECLARING MULTIPLE VARIABLES
6: double d1, double d2; //ilegalLOCAL VARIABLES
public void findAnswer(boolean check) {
int answer;
int otherAnswer;
int onlyOneBranch;
if (check) {
onlyOneBranch = 1;
answer = 1;
} else {
answer = 2;
}
System.out.println(answer);
System.out.println(onlyOneBranch); // DOES NOT COMPILE
}The answer variable is initialized in both branches of the if statement, regardless of whether check is true or false, the value answer will be set to something before it is used.
The otherAnswer variable is not initialized but never used, and the compiler is equally as happy. Remember, the compiler is only concerned if you try to use uninitialized local variables; it doesn’t mind the ones you never use.
public void checkAnswer() {
boolean value;
findAnswer(value); // DOES NOT COMPILE
}The call to findAnswer() does not compile because it tries to use a variable that is not initialized.
DEFINING INSTANCE AND CLASS VARIABLES
An instance variable, often called a field, is a value defined within a specific instance of an object.
a class variable is one that is defined on the class level and shared among all instances of the class.
Instance and class variables do not require you to initialize them. As soon as you declare these variables, they are given a default value.
Default initialization values by type
VAR
Type Inference of var
When you type var, you are instructing the compiler to determine the type for you. The compiler looks at the code on the line of the declaration and uses it to infer the type.
In Java, var is still a specific type defined at compile time. It does not change type at runtime.
7: public void reassignment() {
8: var number = 7;
9: number = 4;
10: number = "five"; // DOES NOT COMPILE
11: }var apples = (short)10; apples = (byte)5; apples = 1_000_000; // DOES NOT COMPILE