chapter 2 Flashcards
How do you create an instance of a class?
write "new" before the class name and parentheses after the class name. e.g. Foo f = new Foo();
What do constructor methods do?
Create new objects.
What are two characteristics of constructor methods?
Their name matches the name of the class, and they do not have return types.
What is the purpose of a constructor method?
To initialize fields in an instance of a class.
What keyword is required anytime a constructor is used?
new
What is a code block?
The lines of code between {}.
What are instance initializers?
Blocks of code that run outside of a method.
What order are fields and initializer blocks run?
The order in which they appear in the file.
When does the constructor run in relation to fields and initializer blocks?
After all fields and instance initializer blocks have run.
What are the two data types Java applications contain?
primitive and reference types.
How many and what are the built-in primitive data types Java has?
8 types.
boolean: true or false
byte: 8-bit integral value
short: 16-bit integral value
int: 32-bit integral value
long: 64-bit integral value
float: 32-bit floating-point value
double: 64-bit floating-point value
char: 16-bit Unicode value
What is and what is not a primitive data type in Java?
A single value in memory, not an object.
What does a float value require following the number in Java?
f
Are all numeric types signed in Java?
Yes.
What does it mean when a numeric type is signed?
The type reserves one bit to cover a negative range.
What are the ranges of values possible for each of the 6 primitive numeric types in Java?
byte: -128 to 127
short: -32,768 to 32,767
int: -2,147,483,648 to 2,147,483,647
long: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,80
float: 7 decimal digits
double: 16 decimal digits
What are the similarities and differences between short and char?
Both are stored as integral types with the same 16-bit length. Short is signed, char is not.
How are floating point numbers stored in most computer systems?
Scientific notation.
What is a number called when it is present in code?
A literal.
What does Java assume as the default type for a literal?
an int.
How can a numeric literal be made a long type?
Append an L (or an l) to the literal.
What can be added to numeric literals to allow easier reading?
underscores _
Where are underscores NOT allowed to be placed in a numeric literal?
The beginning or end of a literal.
Right before or after the decimal point.
What are the two ways a reference type can be assigned to a value?
To another object of the same or compatible type.
To a new object using the new keyword.