Java Basics Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What is the difference between an instance and a local variable? What about method parameters?

A

Instance: variables declared inside a class, but outside a method. Get default values.
Local: variables declared within a method. Do NOT get a default value; must be initialized before use.
Method parameters: basically local variables, but they will never be initialized.

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

== vs .equals

A

== for primitives, or to see if reference variables refer to the same object on the heap.
.equals to see if OBJECTS are equal (i.e. if 2 different objects are equal)

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

what is the difference between a for loop, a while loop, and an enhanced for loop?

A

WHILE is for when you don’t know the number of iterations; it has only a boolean test.
A FOR loop is for when you do know the number of iterations, and it is cleaner.
An ENHANCED FOR loop makes it easier to iterate over all the elements in a collection, but there is little flexibility.

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

How do you invoke a method on an array?

A

You don’t. Arrays are objects, but it has no methods and only 1 instance variable (length).

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

What is a parameterized type?

A

The part that goes in angle brackets: ArrayList

It’s a way to force the compiler to allow only a specific type of object into the list.

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

What is the difference between &, | and &&, ||?

A

The single versions force the JVM to check both sides of the expression.
There is another difference that will come later.

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

What’s the difference between an array and an array list?`

A

ArrayList is flexible, can invoke methods on it. Array cannot change size, no methods except .length. (May be slightly faster with primitives)

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

When do you extend another class?

A

When the first class IS-A type of the other class. Don’t use it just to get the methods. It must be an IS-A inheritance relationship.

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

When does a subclass inherit methods from the superclass, and when does it NOT inherit them?

A
A subclass inherits all PUBLIC instance
variables and methods of the superclass, but
does not inherit the PRIVATE instance variables and methods of the superclass.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

3 things that make a class un-extendable

A

1) superclass is marked non-public (can’t be private though)
2) keyword FINAL
3) class has private constructors

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

If you want to change the way a method works, but you don’t have access to the source code, what can you do?

A

Extend the original class, then override the method.

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

What is the contract for overriding methods?

A

1) The overriding method must look exactly like the overridden method (same arguments and return types)
2) the access level must be the same or more open

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

Can you have an abstract method in a regular class?

A

No. Abstract methods (no body) must be in an abstract class.

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

Integer vs. int

A

Primitives can never be null, so a primitive type implicitly states that “this value is never null.” Null can be a real headache in Java, especially NullPointerExceptions, so that alone drives me to prefer primitives. If we receive a null from the data layer, a mapped primitive would be 0, which strikes me as no better or worse than null.

Also, there is an (admittedly slight) overhead associated with using a reference type over a primitive.

I think it’s safe to rely on this guarantee, but since we don’t technically own the library, I’m open to keeping reference types. I just know it’s common to take for granted that we should use reference types, when often a primitive makes more sense and is easier to work with, so I thought I’d throw this out there.

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

return entities.stream()
.map(entity -> convert(entity))
.collect(Collectors.toList());

A

Basically, map takes each element in the stream, and applies some operation that turns it into something new (in this case, a ProductEntity). Collect wraps it all together. There are a ton of useful Collectors, too, including some for Maps and Sets.

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

testHelper

A

I think it’s easier to justify “createProduct” if we make these static utility methods rather than instance methods, which I think makes sense. Then invocations look like TestHelper.createProduct(…), and since we are referencing the TestHelper class, the additional context is provided.

On static utility classes, it’s conventional to provide a private constructor to prevent instantiation. In effect, since this class is stateless, there’s no need to ever instantiate it; providing a private constructor makes that a guarantee.