Ch1: Java Building Blocks Flashcards
What can identifier names start with?
A letter, an underscore, or a dollar sign
Identifier names can have which characters?
A letter, an underscore, or a dollar sign. Subsequent characters may also have numbers.
What is an object?
A runtime instance of a class in memory
What are class methods called in other languages?
Functions or Procedures
What is the Java term for a function or procedure in a class?
A method
What are we referring to when we talk about the members of a class?
Methods and fields
What is the general term for methods and fields in a class?
Members
What is another word for a field in a class?
Instance variable
What is another word for a class or instance level variable?
field
What is the method signature for a program entry point? (The “Main” method)
public static void main(String[] args)
For the program entry point, is the Main method uppercase or lowercase?
Lowercase:
public static void main(String[] args)
What is the command to compile a file called “zoo.java”?
javac zoo.java
What is the command to run the class file “zoo.class”?
java zoo
Which class is always imported by default?
java.lang.*
How do you specify other class paths when running a java program?
java -cp “.:/path1/foo.jar:/path2/*” myPackage.MyClass
What is an Instance Initializer?
A code block ( {..} ) outside of a method, but inside of a class.
When an object is created, what is the order of initialization?
- Static fields and static initializers, in order
- Instance variables and instance initializers, in order
- Constructors
What are the two types of data containers in Java?
Primitive types and reference types
List all primitive type keywords
boolean byte short int long float double char
What is byte?
8-bit integral value
What is short?
16-bit integral value
what is int?
32-bit integral value
what is long?
64-bit integral value
what is float?
32-bit floating-point value
what is double?
64-bit floating-point value
what is char?
16-bit Unicode value
What is boolean?
true or false
What is an example of a floating point literal?
123.45f
In order of size, what are the integral primitives?
byte > short > int > long
In order of size, what are the floating-point primitives?
float > double
What range of values can a byte hold?
-128 to 127
What data type is used for integral numeric literals?
int
How do you specify a numeric literal should be a long?
Add an L to the end (123L)
What number bases can we specify for numeric literals?
Decimal (default), Octal, Hexadecimal, Binary
How do you specify a numeric literal is octal?
Lead with a zero (eg, 0123)
How do you specify a numeric literal is hex?
Lead with zero and x/X (eg, 0xFF)
How do you specify a numeric literal is binary?
Lead with a zero and b/B (eg, 0b10)
What is the rule for underscores in numeric literals?
Underscores must have a number on both sides (eg, 1_000.75)
How do you initialize multiple variables on the same line?
String s1 = “foo”, s2 = “bar”;
What is a local variable?
A variable defined within a method
What is the result of trying to use a local variable before it is initialized?
An error is thrown
What is a reference type variable?
A pointer to an object in memory
What is the result of trying to use an instance variable before it is initialized?
The action will be taken on the default value
What is the result of trying to use a class variable before it is initialized?
The action will be taken on the default value
What is an instance variable?
A variable defined within a class, but outside of a method.
What is a class variable?
A variable defined within a class with the static keyword.
What’s another word for a field in a class?
instance variable
What is the default value for instance variables of type boolean?
false
What is the default value for instance variables of numeric types?
0 or 0.0
What is the default value for instance variables that are object references?
null
What is the default value for instance variables of type char?
NUL
What is the default value for class (static) variables of type boolean?
false
What is the default value for class (static) variables of numeric types?
0 or 0.0
What is the default value for class (static) variables of object references?
null
What is the default value for class (static) variables of type char?
NUL
What is the default value for local variables of type boolean?
Garbage
What is the default value for local variables of numeric types?
Garbage
What is the default value for local variables of object references?
Garbage
What is the default value for local variables of type char?
Garbage
What order should elements be within a class?
- Package declaration
- Import statements
- Class declaration
* Field declarations and Method declarations can be anywhere within the “class” code block
Where are java objects stored in memory?
On the heap
What is another term for heap memory?
Free store
What is another term form free store memory?
Heap memory
Which method triggers garbage collection?
System.gc()
What is the signature of the method that is ran when an object is garbage collected?
protected void finalize()
What is the finalize() method?
A method that is run when an object is garbage collected
How many times will finalize() run?
No more than once, potentially never. (0 or 1 times)
What are the benefits of Java?
- Object Oriented (uses classes)
- Encapsulated (uses access modifiers)
- Platform Independent (compile once, run anywhere)
- Robust (Managed memory - no pointers)
- Simple (Easier than C++ - no pointers, no operator overloading)
- Secure (Sandboxed inside JVM)
What data type is used for floating-point numeric literals?
double
What is an example of an int literal?
123
What is an example of a long literal?
123L
What is an example of a float literal?
123.45F
What is an example of a double literal?
123.45
What data type is used for integral literals?
int
What data type would the literal 123 be?
int
What data type would the literal 123.45 be?
double