Java Basics Flashcards
What is the difference between an instance and a local variable? What about method parameters?
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.
== vs .equals
== 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)
what is the difference between a for loop, a while loop, and an enhanced for loop?
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 do you invoke a method on an array?
You don’t. Arrays are objects, but it has no methods and only 1 instance variable (length).
What is a parameterized type?
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.
What is the difference between &, | and &&, ||?
The single versions force the JVM to check both sides of the expression.
There is another difference that will come later.
What’s the difference between an array and an array list?`
ArrayList is flexible, can invoke methods on it. Array cannot change size, no methods except .length. (May be slightly faster with primitives)
When do you extend another class?
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.
When does a subclass inherit methods from the superclass, and when does it NOT inherit them?
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.
3 things that make a class un-extendable
1) superclass is marked non-public (can’t be private though)
2) keyword FINAL
3) class has private constructors
If you want to change the way a method works, but you don’t have access to the source code, what can you do?
Extend the original class, then override the method.
What is the contract for overriding methods?
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
Can you have an abstract method in a regular class?
No. Abstract methods (no body) must be in an abstract class.
Integer vs. int
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.
return entities.stream()
.map(entity -> convert(entity))
.collect(Collectors.toList());
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.