CHP3 - Making Decisions Flashcards

1
Q

Hierarchy of Number Class

A

java.lang.Object
↳ java.lang.Number
↳ java.lang.Integer
↳ java.lang.Double
↳ java.lang.Float
↳ java.lang.Long
↳ java.lang.Short
↳ java.lang.Byte
↳ java.util.concurrent.atomic.AtomicInteger
↳ java.util.concurrent.atomic.AtomicLong

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Pattern Matching

A

Technique of controlling program flow that only executes a section of code that meets certain criteria.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Supported Types of Pattern Matching

A

The type of the pattern variable must be a compatible type, which includes the same type, a subtype, or a supertype of the reference variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Shadowing

A

Shadowing occurs when a variable declared within an inner scope has the same name as a variable in an outer scope. The inner variable “shadows” or hides the outer variable within that block.

int value = 50;
{
int value = 100; // This ‘value’ shadows the outer ‘value’
System.out.println(value); // prints 100
}
System.out.println(value); // prints 50

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

switch variable types

A

int and Integer
byte and Byte
short and Short
char and Character
String
enum values
All object types (pattern matching)
var (if the type resolves to one of the preceding types)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

switch (month) {}

Is this valid?

A

A switch statement is not required to contain any case clauses. This is perfectly valid.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Ways to write exhaustive switch

A
  1. Add a default clause.
  2. If the switch takes an enum, add a case clause for every possible enum value.
  3. Cover all possible types of the switch variable with pattern matching
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is yield?

A

yield keyword is return statement for switch

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

switch and pattern matching

A

One of the simplest rules when working with switch and pattern matching is that the type cannot be unrelated.
It must be the same type as the switch variable or a subtype.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does it mean switch statement must be exhaustive?

A

✔ Exhaustiveness means handling all possible values in a switch.
✔ For enums and sealed types, if all cases are covered, default is not needed.
✔ For open types (like Object or Number), a default case is required.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

continue in switch?

A

It cannot be used inside of switch

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

private DayOfWeek getWeekDay(int day, final int thursday);

In this method signature, is thursday compile time constant?

A

No, it is not since any int value can be passed as parameter.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

case 2. 10: break;

Is it legal in Java?

A

✔ Valid switch syntax: case 2, 10: is legal in Java 21+ (but not in Java 20 or earlier).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Is it valid in case?
case !(Number n) -˃ 3 + n.intValue();

A

The code does not compile because case clause uses the logical complement operator (!), which is not permitted with pattern matching.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

do {
5: meters–;
6: if (meters==8) keepGoing = false;
7: result -= 2;
8: } while keepGoing;

Is it valid?

A

No it is not valid.
Because while condition must be inside of braces.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

yield attention

A

Be carefull with:
-semicolon after yield closing brace
-without yield

17
Q

default vs null in switch

A

default dominates the null

18
Q

Method vs field access

A

Method calls are resolved at runtime, while field access is resolved at compile time.