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.