Methods Flashcards

1
Q

What about methods with same name, but they have wrapping class and primitive as only parameters?

A

It is allowed for them to exist in same class:

public void method(int a) {}
public void method(Integer a) {}

public void method(Integer[] a) {}
public void method(int[] a) {}

Same goes for arrays, but not varargs and arrays

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

What happens when we set a value to autobox and numeric promote?

A

Java wont do both at the same time, it can ether autobox or numeric promote smaller value to bigger. Eg.
Long l = 9 wont compile.
long l = 9 will.

Same applies for methods:
method(Long a) <- method(9) wont compile

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

What is alternative to autoboxing and unboxing?

A

Depending on value type:

Integer i1 = Integer.valueOf(int) is autoboxing
int i2 = i1.intValue() is unboxing

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

How can we get NullpointerException while unboxing?

A

When we try to unbox a wrapper class that is null to primitive

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

Are varargs and arrays interchangeable?

A

Yes.
new boolean[2] -> boolean…

method(new int[] { 1, 2, 3 }) is also valid.

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

What is position where varargs need to be when used as parameters?

A

At the end

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

Can varargs have … before name of the varargs?

A

No, they must be after the name.

args… O
…args X

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

What is rule for initializing static final class member?

A

It can be initialized only once, second time will throw compile error. So they can be inited directly inline or with static blocks, but only once.

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

What are rules for package access methods?

A

If class is public, then it can be initialized from anywhere, but if it contains package access protected methods, they can be only ran from inside the same package.

This access type is more restrictive than protected.

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

What modifiers can instance variable have?

A

static
transient
volatile
final

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

What access modifiers are there?

A

public
protected
package protected
private

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

What optional modifiers can methods have?

A

synchronized
final
static
abstract
default

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

What do we need to watch out when using final with instance variables?

A

We must initialize its value before it is used, in instance variable case, before constructor call ends. In static class members, before usage or end of the class.

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

What do we need to watch out regarding modifiers?

A

Optional modifiers can come before other modifiers

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

Can “final” and “var” be used together?

A

Yes, but only in method bodies.

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

What can you tell about static members of a class and their initialization?

A

They auto. initialize. eg. int to 0 and String to null

17
Q

What happens when you null a reference that contains static member?

A

You can still access static member trough that nulled reference.

18
Q

How does “package access” modifier work?

A

If defined on class, only classes that are instantiated in the same package (not even subpackage) can instantiate it. If extended, class members are only available to child methods.
If defined on class members, only classes that are in the same package can use those members, but class can be instantiated if public.

19
Q

How does “protected” modifier work?

A

Cannot be defined on a class.
When defined on class members, only classes that are in the same package can use those members. If extended, class members are only available to child methods or classes that are instantiated in the same package.

20
Q

Is there numeric promotion between wrapping classes?

A

No, those classes need to be unboxed and then numberic promotion can happen

21
Q

What is set when you instantiate “new boolean[2]”

A

Values of the array are auto. set to false