Chapter 3: Assignments Flashcards
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?
- char d = -12;
- char d = (char) -98;
- char f = 70000;
- char c = ‘"’
- Char is a 16 bit unsigned integer, needs a cast because loss of percision; 2 is correct, with cast.
- Does not fit in a 16 bit integer.
What assignments are inccorect?
- byte a = 3;
- byte b = 8;
- byte c = a + b;
- expressions containing only variables that are ints or smaller are evaluated to an int. An int cannot be assigned to a byte without a cast.
What are the return type:
- byte a = 1; byte b = 2; a + b
- byte a = 1; long b = 2; a + b;
- short a = 1; byte b = 2; a + b;
- long a = 1; float f = 2F; a + b;
- int
- long
- int
- float
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.
- Block variables, as long as the code-block executes.
- Local variables, as long as the method executes.
- Instance variables, as long as the object lives.
- Static variables, as long as the class is loaded in the JVM.
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
- Nothing, this is a valid multi-dimensional array declaration.
- Length of array cannot be set in the declaration.
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
- Primitive arrays can only be assigned to the same type array reference.
- Object arrays can only be assignedn to the same type or a super type.
- You cannot assign an array with a different diemsnion.
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
- Super class static blocks
- Class static blocks
- Super class code blocks
- Constructor Super class
- Class code blocks.
- Constructor class.
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.,