Chapter1 Flashcards
Name primitive types in relative order:
Boolean, byte(8bit), short, Int, long, float(32bit), double(64bit), char(16bit)
long max = 312456789;
Does not compile. Why?
Needs ‘L’ on literal.
long max = 312456789L;
int i1, i2, i3 = 0;
How many declared?
How many initialized?
3 declared.
1 initialized.
Three rules for legal identifiers:
Must begin with letter, $ or _.
Subsequent characters may be numbers.
No reserved words (although different case technically allowed)
Local, instance and class variables? Difference between?
Local is in a method.
Instance is a field in a class.
Class variable shared across multiple objects using static.
Defaults for Boolean, byte, Int, float, double, char, objects
False, byte:0, Int:0, float:0.0, double:0.0, char:’\u0000’, object:null
Order of initialisation?
Fields and instance blocks run in order in which they appear. Then constructor runs.
A byte can God a value from …?
-128 to 127. A byte is 8 bits. A bit has two possible values. So 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 = 256. 0 needs to be included.
Name 3 other base numbers Java recognizes:
Octal 017
Hexadecimal 0xFf
Binary 0b10
Which are valid? boolean b1, b2; String s1 = "1", s2; double d1, double d2; Int i1; int i2; int i3; i4;
Legal Legal Not legal Legal Not legal
Order of elements in a class… ie methods, import statements etc
Package declaration, import statements, class declaration, field and methods. Only class declaration is required
How many classes can there be in a file? Other rules?
Multiple. But only one must be public. Both can be not public. Also one must be named after file.
Rules about garbage collection?
Calling System.gc doesn’t guarantee garbage collection. An object remains on heap until it is no longer reachable: no references or all references gone out of scope.
finalize()?
Gets called if the garbage collector tries to collect object. Can run zero or one time.
Java is an interpreted language, it gets compiled to…
bytecode (a .class file). The same class files run everywhere.