Chapter 2 Java Building Blocks Notes Flashcards
Constructor
-
constructor is a special type of method that creates a new object
- name of the constructor matches the name of the class
- no return type
- For most classes, you don’t have to code a constructor—the compiler will supply a “do nothing” default constructor for you.
- For the exam, remember that anytime a constructor is used, the new keyword is required.
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
- Fields and instance initializer blocks are run in the order in which they appear in the file.
- The constructor runs after all fields and instance initializer blocks have run.
The Primitive Types
- boolean, true or false, default: false
- byte, 8-bit integral value, default: 0, (-128 to 127)
- short, 16-bit integral value, default: 0, ( -32,768 to 32,767)
- int, 32-bit integral value, default: 0,
- long, 64-bit integral value, default: 0L
-
float, 32-bit floating-point value, default: 0.0f
The float data type is a single-precision 32-bit IEEE 754 floating point -
double, 64-bit floating-point value, default: 0.0d
The double data type is a double-precision 64-bit IEEE 754 floating point. - char, 16-bit Unicode value, default: ‘\u0000’, (‘\u0000’ (or 0) to ‘\uffff’)
SIGNED AND UNSIGNED: SHORT AND CHAR
- both are stored as integral types with the same 16-bit length
- short is signed
- char is unsigned. Therefore, char can hold a higher positive numeric value than short, but cannot hold any negative numbers.
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:
- Octal (digits 0–7), which uses the number 0 as a prefix—for example, 017
- Hexadecimal (digits 0–9 and letters A–F/a–f), which uses 0x or 0X as a prefix—for example, 0xFF, 0xff, 0XFf. Hexadecimal is case insensitive so all of these examples mean the same value.
- Binary (digits 0–1), which uses the number 0 followed by b or B as a prefix—for example, 0b10, 0B10
Literals and the Underscore Character
- You can add underscores anywhere except at the beginning of a literal, the end of a literal, right before a decimal point, or right after a decimal point.
- You can even place multiple underscore characters next to each other, although we don’t recommend it.
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
- Reference types can be assigned null
- Primitives do not have methods.
- primitive types have lowercase type names.
- All classes that come with Java begin with uppercase.
4 Legal Identifiers Rules:
- Identifiers must begin with a letter, a $ symbol, or a _ symbol.
- Identifiers can include numbers but not start with them.
- Since Java 9, a single underscore _ is not allowed as an identifier.
- You cannot use the same name as a Java reserved word. A reserved word is special word that Java has held aside so that you are not allowed to use it. Remember that Java is case sensitive, so you can use versions of the keywords that only differ in case. Please don’t, though.
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
- Method and variable names are written in camelCase with the first letter being lowercase.
- Class and interface names are written in camelCase with the first letter being uppercase. Also, don’t start any class name with $, as the compiler uses this symbol for some files.
- Constants, uppercase snake_case, ex:
THIS_IS_A_CONSTANT
DECLARING MULTIPLE VARIABLES
- You can declare many variables in the same declaration as long as they are all of the same type.
Java does not allow you to declare two different types in the same statement. Even if they are the same type:6: double d1, double d2; //ilegal
- You can also initialize any or all of those values inline.
LOCAL VARIABLES
- A local variable is a variable defined within a constructor, method, or initializer block.
- Local variables do not have a default value and must be initialized before use.
- you are not required to initialize the variable on the same line it is defined, but be sure to check to make sure it’s initialized before it’s used on the exam.
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
- boolean, false
- (byte, short, int, long), 0
- (float, double), 0.0
- char, ‘\u0000’ (NUL)
- All object references (everything else), null
VAR
- Introduce in Java 10
- The formal name of this feature is local variable type inference.
- You can only use this feature for local variables
- var can be used in for loops
- with some lambdas
- var can be used with try-with-resources.
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: }
- On line 8, the compiler determines that we want an int variable.
- On line 9, we have no trouble assigning a different int to it.
- On line 10, Java has a problem. We’ve asked it to assign a String to an int variable. This is not allowed.
var apples = (short)10; apples = (byte)5; apples = 1_000_000; // DOES NOT COMPILE
- The first line creates a var named apples with a type of short.
- It then assigns a byte of 5 to it, but did that change the data type of apples to byte
- The last line does not compile, as one million is well beyond the limits of short. The compiler treats the value as an int and reports an error indicating it cannot be assigned to apples.