CH1 - Building Blocks Flashcards

(11 cards)

1
Q

What is Reference?

A

The reference is a variable that has a name and can be used to access the contents of an object.

All references are the same size, no matter what their type is.

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

Object vs Reference

A

An object sits on the heap and does not have name.
Therefore, there is no way to access an object except through reference.

An object cannot be assigned to another object, and an object cannot be passed to a method or returned from a method. It is object that gets garbage collected, not its reference.

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

Does System.gc() guarantee to run garbage collector?

A

No, calling System.gc() in Java does not guarantee that the garbage collector will run. It is merely a request for the JVM to perform garbage collection, but the JVM may ignore this request.

Why?
The Java Virtual Machine (JVM) has its own garbage collection (GC) strategy and decides when to run it.
Modern JVM implementations use sophisticated memory management techniques and do not necessarily run GC just because System.gc() was called.
The behavior of System.gc() can be overridden by JVM settings such as -XX:+DisableExplicitGC, which prevents explicit GC calls.

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

var with null

A

A var cannot be initialized with a null value without a type, but it can be assigned a null value later if the underlying type is not a primitive.

var cannot be used in a multiple‐variable assignment. (var fall=2, autumn=2)

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

Underscore in numeric values

A

An underscore (_) can be placed in any numeric literal, as long as it is not at the beginning, at the end, or next to a decimal point (.). Underscores can even be placed next to each other.

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

import

A

You cannot specify the same class name in two imports

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

Garbage Collection

A

In Java, there are no guarantees about when garbage collection will run. The JVM is free to ignore calls to System.gc().
An object may be eligible for garbage collection but never removed from the heap.

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

Text blocks in Java

A

-If a backslash is placed at the end of a line, it acts as a line continuation character, meaning the next line is treated as part of the same logical line.

-If you double the backslash (\) at the end of a line, it preserves one backslash in the output.

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

Default values

A

✅ Instance variables get default values when an object is created.
✅ Static variables also get default values, shared across all instances.
✅ Local variables (inside methods) don’t get default values—they must be initialized before use.

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

Java Encapsulation

A

Encapsulation works at the Class Level, not the Object Level.

private restricts access to outside classes, but not inside the same class.

Java assumes that all instances of the same class should trust each other because they are part of the same “trusted implementation.

Usefull cases:
- Implementing equals() method
-Copy constructor
-Cloning Objects

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