ch3 Flashcards
Can a numeric literal contain a comma?
no
Which object does a variable with the value null
refer to?
To no object at all. There is no null singleton or sth like that in Java.
What is the scope of static variables?
Created: when the class is loaded. Stay: as long as the class stays in the JVM.
What are the possible scopes of a variable?
static, instance / member, local / method / stack (temporary, automatic??), block (Remember: no life outside a class)
What about the variables of the method which has invoked the current method?
They are alive, but out of scope. (They are on the stack, but not in the top étage.)
When do instance variables get a value?
When the object gets created. If no explicit value is defined, they get initialized with a default value.
Is an array an object?
Yes.
int [] numbers = new int[10];
What does this array contain?
10 int elements with the value 0.
Do local variables have a default value?
No.
What happens, if you use a local variable which has not been initialized?
compiler error: might not have been initialized
Reference variables referring to such objects behave a bit different.
String
What does variable shadowing mean?
Reusing a variable name which has already been declared somewhere else.
Which part of the memory is involved in the garbage collection process?
heap
What does the garbage collection do?
- search for objects eligible for garbage collection: No live thread can reach it. 2. evtl delete them
Who starts the garbage collection?
The JVM. The program code can request a run, but there is no guarantee, it will run indeed.
Is the following statement true: If there is a reference to an object, it is not eligible for garbage collection ?
No. If the object belongs to an ‘island of isolation’, it is eligible for garbage collection.
How can you request a garbage collection?
System.gc()
Runtime.getRuntime().gc()
What is Runtime?
a class. It has a single instance (a singleton) for each main program. It provides a mechanism to communicate directly with the JVM.
How can you communicate directly with the JVM in your code?
via the singleton instance of the Runtime
class: Runtime.getRuntime()
Can you get an OutOfMemoryException, if the garbage collector has never run?
No.
What are the drawbacks of the finalize
method?
It is not deterministic when the garbage collection runs, thus it is not deterministic when finalize runs. If the code in finalize saves the object from deletion, but it gets eligible for garbage collection again later, the finalize
method will not run before the actual deletion.