Where do instance variables, local variables and objects live?
Heap: instance variables and objects
Stack: local variables
What are the three integer literals?
Decimal literal - base10 - (0-9)
int length = 3; //3
Octal literal - base8 - (0-7) Prefixed with a 0 int length = 03; //3 int length = 010; //8 int length = 077; // 63 int length = 0100; // 64
Hexadecimal - base16 (0-F) Prefixed with 0x or 0X, not case sensitive int length = 0xF; // 15 int length = 0x01; //16 int length = 0xDEADCAFE; // Valid
How to assign the three integer literals to a long?
Postfix with a l or L.
long length = 1L;
long length = 0xFl;
long length = 0100L;
What is wrong?
float f = 10.50;
Floating-point literals are double by default. Prefix with F or f.
What is wrong?
double d = 2310293.231d;
Nothing, Floating-point literals are double by default, optionally they can be post-fixed with a d or D.
What assignments are inccorect?
What assignments are inccorect?
What are the return type:
Anything involing only smaller or int variables will result in int. (1 AND 3) Otherwise the result type will be the largest used holder (2 AND 4).
When do you need to explicitly cast for primitives?
When narrowing, putting something into a smaller holder.
short a = 1;
byte b = (byte) a;
long l = a; // No cast needed, larger container.
double d = l; // No cast needed, larger container.
What will be the result?
long l = 130L;
byte b = (byte) l;
130 does not fit in a byte. So the bits will be choped of from the left side. If the first bit (the sign bit) is now a 1 it will turn it into a negative number: -126.
Name the 4 variables scopes based on the from shortest life span to the longest life span.
What will be printed?
int[] numbers = new int[10};
System.out.println(numbers[0]);
0
The array element get their default value.
What is wrong?
public class Bar {
public static void main(String[] args) {
int days;
int year;
System.out.println("The year is: " + year);
}
}Local primitive is not initialized. Local variables do not get their default value if not assigned. Its fine to leave days unitialized since we do not use it.
Object also do not get a default value. So you need to explicitly set a null value if desired.
Object aObject = null;
What will be printed?
class Bar {
public int myIntValue = 4;
public void doStuff(int myIntValue) {
myIntValue++;
}
}
Bar b = new Bar(); b.doStuff(b.myIntValue); System.out.println(b.myIntValue);
The instance variable myIntValue is untouched since the method doStuff shadows the myIntValue variable reference.
What is wrong?
String [] arrayName []; // 1
String[5] strings; // 2
What is wrong?
int[] testScores;
testScores = {1,2,3}
Testscores is already initialized to a null value. You need to use the new keyword like:
testScores = new int[] {1,2,3};
What primiteves can a primitive array hold?
Any primitive that can be implicitly promoted to the decalered array type. For example: short, byte, char can all be assigned to an int array type.
What objects can a object array hold?
All objects that pass de IS-A test for the array type (inheritance: extends or implements).
What assignments will fail: int[] a; char[] c = new char[3]; a = c; // 1 Cars[] cars; Lexus[] lexuss = new Lexus[3]; Dog[] dogs = new Dog[3]; cars = dogs; // 2 cars = lexuss; //3 int[][] chart = new int[5][]; a = chart; // 4
1, 2, 4
What will be printend and in what order?
public class Blocks extends SuperBlocks{
static { System.out.println("Static Blocks"); }
{ System.out.println("Blocks1"); }
{ System.out.println("Blocks2"); }
Blocks() { System.out.println("Construct Blocks"); }
public static void main(String[] args) { new Blocks(); }
}
class SuperBlocks {
static { System.out.println("Static SuperBlocks"); }
{ System.out.println("SuperBlocks1"); }
SuperBlocks() { System.out.println("Construct SuperBlocks"); }
}Static SuperBlocks Static Blocks SuperBlocks1 Construct SuperBlocks Blocks1 Blocks2 Construct Blocks
What constructors does a Wrapper class have (Integer, Character etc.)
The primitve type or a String representation.
Except for Character which only has a constructor for the primitive and float wich can also take a double,
What is the result of the following:
Integer i = new Integer("GeenInteger"); // 1
Boolean b = new Boolean("GeenBoolean"); // 2// 1 Throws NumberFormatException // 2 Anything that does not equals to true will result in a false boolean.
What do the parseXxx() methods and valueOf() methods do?
Wrapper.parseXxx(): parse a String to a primitive. Optionaly take a radix argument, to define the base.
valueOf(): parse a String to a wrapper object.
Optionaly take a radix argument, to define the base.
Long.parseLong(“101010”, 2); // Results in 42.
What is the xxxValue method?
A method on the wrapper objects for converting the objects to a primitve value.,