Chapter 2 Java Building Blocks Notes Flashcards
1
Q
Constructor
A
-
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.
2
Q
INSTANCE INITIALIZER
A
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: }
3
Q
ORDER OF INITIALIZATION
A
- 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.
4
Q
The Primitive Types
A
- 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’)
5
Q
SIGNED AND UNSIGNED: SHORT AND CHAR
A
- 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
6
Q
FLOATING-POINT NUMBERS AND SCIENTIFIC NOTATION
A
for the exam you are not required to know scientific notation or how floating-point values are stored.
7
Q
Writing Literals
A
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
8
Q
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
A
9
Q
Literals and the Underscore Character
A
- 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
10
Q
Reference Type
A
11
Q
DISTINGUISHING BETWEEN PRIMITIVES AND REFERENCE TYPES
A
- 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.
12
Q
4 Legal Identifiers Rules:
A
- 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.
13
Q
Legal?
long okidentifier; float $OK2Identifier; boolean _alsoOK1d3ntifi3r; char \_\_SStillOkbutKnotsonice$;
A
14
Q
Legal?
int 3DPointClass; byte hollywood@vine;_ String *$coffee; double public; short _;
A
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
15
Q
IDENTIFIERS IN THE REAL WORLD
A
- 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
16
Q
DECLARING MULTIPLE VARIABLES
A
- 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.