Programmer I Chapter 2: Java building blocks Flashcards
What this prints out?
public class Egg { public Egg() { number = 5; } public static void main(String[] args) { Egg egg = new Egg(); System.out.println(egg.number); } private int number = 3; { number = 4; } }
5
ch2 creating objects
how short and char are similar and different?
both are 16 bits,
short is signed and char is unsigned
what will this print?
short bird = ‘d’;
char mammal = (short)83;
System.out.println(bird);
System.out.println(mammal);
100 (code for ‘d’)
S (letter with code 83)
what will this print?
short reptile = 65535;
System.out.println(reptile);
does not compile - max value of short is 32767
what will this print?
char fish = (short)-1;
System.out.println(fish);
does not compile - char is signed
how to write binary, octal and hexademical literals?
binary - 0b10 (or 0B10)
octal - 017
hexademical - 0xff (or 0XFF)
what lines will compile?
- double a = _1000.00;
- double b = 1000.00_;
- double c = 1000_.00;
- double d = 1_00_0.0_0;
- double e = 1__________2;
- no
- no
- no
- yes
- yes
what lines will compile?
int value = null;
String s = null;
- no
2. yes
rules for legal identifier names?
- starts with a letter, _ or $
- can include numbers but not start with them
- a single underscore “_” is not allowed, since Java 9
- can’t be a reserved word
what variables are initialized?
void paintFence() { int i1, i2, i3 = 0; }
only i3 is initialized
what lines are legal?
1: boolean b1, b2;
2: String s1 = “1”, s2;
3: int num, String value;
4: double d1, double d2;
5: int i1; int i2;
6: int i3; i4;
1: yes
2: yes
3: no - different types can’t be declared in the same statement
4: no - even if both say double, still counts as different types
5: yes
6: no
what will this print out?
public void findAnswer(boolean check) { int answer; int otherAnswer; int value; if (check) { value= 1; answer = 1; } else { answer = 2; } System.out.println(answer); System.out.println(value); }
will not compile - value is not initialized in all if branches
what will this print out?
public void printAnswer(boolean value) { System.out.println(value); } public void checkAnswer() { boolean value; findAnswer(value); }
does not compile - value in checkAnswer is not initialized
what are default values for field values of these types?
boolean, byte, short, int, long, float, double, char, object reference
boolean - false byte, short, int, long - 0 float, double - 0.0 char - '\u0000' (NUL) object references - null
what is var? can it be used for field variables?
var - local variable type inference
can only be used for local variables (inside methods), not field variables
what is the type of apples?
var apples = (short)10; apples = (byte)5;
short, on line 2 the byte value is promoted to short
will this compile?
public void breakingDeclaration() { var silly = 1; }
yes, line breaks are allowed
will this compile?
public void doesThisCompile(boolean check) { var question; question = 1; var answer; if (check) { answer = 2; } else { answer = 3; } System.out.println(answer); }
no, can’t infer types for question and answer
what lines compile?
int a, var b = 3; var a = 2, b = 3;
none - can’t use var in multiple variable declarations
why will this not compile?
public int addition(var a, var b) {
return a + b;
}
a, b are not local variables. local variable type inference can only be used for local variables
is this code legal?
package var;
public class Var { public void var() { var var = "var"; } public void Var() { Var var = new Var(); } }
the code compiles.
var is a reserved type name (not a reserved keyword) and can be used as variable identifier and package name.
is this code legal?
public class var { public var() { } }
class declaration is illegal.
var is a reserved type name - it can’t be used to define a type: class, interface, enum.
but var can be used as variable identifier and package name.
what happens after running this method?
public static void main(String[] args) {
System.gc();
}
gc() requests the JVM to perform garbage collection. The execution is not guaranteed and JVM may ignore the request.
System.gc() is not guaranteed to do anything.
What are the conditions for an object to become eligible for garbage collection?
- The object no longer has any references pointing to it
2. All references to the object have gone out of scope