• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/49

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

49 Cards in this Set

  • Front
  • Back
What is a static factory and when should you use it over a constructor?
Used to create an object from a static function. Advantages: 1. Unlike constructors, they have names so they can describe the obj being returned. 2. Not required to create an obj so they can utilize caches. 3. Can return obj of any subtype of the return type. Disadvantages: 1. Classes without public or protected constructors cannot be subclassed. 2. Not readily distinguishable from other factory methods.
What is a singleton and how is it invoked?
An obj that is envoked exactly once.1. Could be instantiated using a a factory (getInstance) and a private constructor. 2. Best way is to use an Enum since it is the only way to gurantee the singleton contract through deserialization.
What is a finalizer and why should you avoid using them?
It is the opposite of a constructor and performs destruction tasks.There is no guranteed contract that an objects finalizer function will get called as it varies from JVM to JVM. You should especially never use a finalizer to update persistant data because of this.
What is the correct way to override the equals method?
1. Use == first to improve performance if function is expensive. 2. Use instanceof to check type equality. 3. Cast the argument to the correct type. 4. Ensure all significat fields are equal. 5. Equals should be symmetric, transitive, & consistent. 6. Override hashcode.
Why should you minimize the accessibility of classes?
1. To enforce encapsulation and information hiding which makes a class easier to test, refactor, develop in parllel, and reuse code.
Why should you minimize mutability?
1. Immutable classes are always in a single state so they are easier to use. 2. Inherently thread safe, require no synchronization. 3. Because multiple threads cannot access concurrently, they can be shared freely. 4. You can share final internals. 5. They make great building blocks (e.g. they can be used as keys to maps because there state wont change).
Why should you favor composition over inheritance?
1. Inheritance breaks encapsulation (a subclass is dependent on the implementations of its parents class). 2. You will expose functions and variables that you otherwise shouldn't have, just to make them viewable to subclasses.
Why should you prefer interfaces to abstract classes?
1. Existing classes can easily be retrofitted to implement a new interface. To make classes decendents of an abstract class, you must find a common parent class that can be made abstract. 2. You can only extend one class. 3. You can use interfaces as a mixin.
What is autoboxing and what are its disadvantages?
Autoboxing is the action of the JVM converting int to Integer, double to Double, etc. The conversion does not come free and can be quite expensive and so should be avoided as much as possible.
When is an appropriate time to use a finalizer and what precautions should be taken?
You can use a finalizer as a safety net in case the client does not properly call your termination method. All non-final classes that use a finalizer, should use a finalizer guardian.
What is a finalizer guardian?
A single instance of the anonymous class, called a finalizer guardian, is created for each instance of the enclosing class. The enclosing instance stores the sole reference to its finalizer guardian in a private instance field so the finalizer guardian becomes eligible for finalization at the same time as the enclosing instance. When the guardian is finalized, it performs the finalization activity desired for the enclosing instance, just as if its finalizer were a method on the enclosing class:

Why should you override hashcode when overriding the equls method?

Failure to do so will result in a violation of the general contract for Obejct.Hashcode, which will prevent your class from functioning properly in conjunction with all hash based collections including HashMap, HashSet, and HashTable.



Eg, if hashcode isn't correct, two objects that are equal could be mapped to different buckets in a hashmsp.

Why should you always override toString()?
It makes your class easier to use and debug by providing a human readable representation of the object.
Why shouldn't you store a mutable object in a public final field?
While you cannot change the reference to the mutable object, the object itself can still be modified which has the same negative effects as making the field non-final.
Why should you not place an array with length greater than 0 in a final field?
All arrays of length greater than 0 are mutable.
When is the only time it is acceptable to make accessor variables public?
When the encapsulating class is a nested private or package-private class. You sitll consider encapsulation in this scenario if functionality might change.
What's the best way to make a class immutable?
1. Don't proved mutators (setters). 2. Ensure class can't be extended. 3. Make all fields final. 4. Make all fields private (not necessary for immutability but good for encapsulation). 5. Ensure exclusive access to mutable fields.
What are 2 ways to make sure a class cannot be extended and which one is better?
1. Make the class final. 2. Make the constructor private (or package private) and provide a static factory. I prefer option 2 because you get all the benefits of a static factory and you can still mock classes for testing.
What is a Builder Pattern and when should you use it?
Use a public static inner class to build the object and make constructors private. Better than telescoping for readability and better than chaining because of thread safety.
What is a Decorator Pattern?
Wrap a class using composistion and then make calls to the composite class through the wrapper. Allows you to control your own encapsulation and add functionality (decorate) the composite classes functions.
What are the different types of inner classes?
1. Static 2. Non Static 3. Local 4 Anonymous
What is a static and nonstatic inner class and when should you use one instead of the other?
A static class that is inside of another class. Should use static over nonstatic if the inner class does no need access to its enclosing instance. Nonstatic instances can only be created from an instance of its enclosing class so each nonstatic inner class will have a refrence to its enclosing class.A static class is commonly used for helper methods like the Builder Pattern.
What is a non static inner class and how should and shouldn't it be used?
A class within a class that is non static. Commonly used for an Adaptor Pattern to allow instance of enclosing class to be viewed as some other class. Each inner instance must be instantiated through an outer instance. The inner instance has access to the enclosing instance member variables. Use static if this access is not necessary.
Why is it better to use generics rather than type casting?
Type casting can cause run time exceptions where generics will not compile with the wrong type. It is always best to fail as soon as possible because it makes debugging easier.
How should you use generics to set up these three different tyeps of collections: Producer, Consumer, and Producer & Consumer?
PECS - Producer extends, Consumer superProducer - Collection <? extends E>Consumer - Collection <? super E>Both - Collection "Get and Put Principle"
How could you represent the base functionality of a jave enum class before java 1.5?
By using 'public static final' versions of the containing class. You should also keep the class immutable as enums should not be mutable.
What is a defensive copy?
Example: When a client accessess mutable obj from an internal class and you have an invariant that some value must not change. instead of returning the actual object, you can return a clone. The clone being the defensive copy.
In computer science, what is an invariant?
Something that is held to be true through a certain block of code in an application.
Instead of passing boolean parameter into a function, what else could you use and why might it be beneficial?
You could create an enum which allows you to add functionality if needed and easily add enums later if needed. Example: Fahrenheit, Celsius. Later you can add in Kelvins. Couldn't have done this if you had an 'isCelsius' boolean param.
When does a java program decide which overloaded method to run?
At compile time.
Why should you use the @override annotaion?
If you make a mistake, you will fail at compile time instead of trying to debug a possibly tricky runtime bug.
When does a java program decide which overriden method to run?
At run time.
How many digits can an int and long hold? What can you do if you need more digits?
An int can have 9 digits (2^31 or 32 bits) and a long can have 18 digits (2^63 or 64 bits). If you need more you can use a BigDecimal but this will be a slower solution.
What are the 3 kinds of throwables that java gives you?
1. Runtime Exceptions - use to indicate program errors. 2. checked exceptions - use in situations when the user can expect to recover. 3. Errors - conventionally, these should only be thrown by the JVM.
Why should you reference an object by its interface when possible?
It allows your application to be refactored easier and makes your code more extensible. If you use an interface as a function paramater instead of the base class, you can create a better implementation of that interface later and seamlessly pass in the new one.
What does it mean for an object to be failure atomic?
An object that is reverted to its original state when an exception occurs. This is especially important for checked exceptions as the user is expected to have the ability to recover.
What are the best ways to achieve failure atomicity?
1. Use immutable objects. If your object is immutable, you have nothing to revert and probably are only stopping the creation of a new instance. 2. Check the validity of parameters before updating a mutable object. 3. Create a defensive copy of the object and only save the changes after completeing without errors.
What are two reasons you need to use synchronization to keep a mutable object thread safe?
1. You need to ensure threads have mutual exclusivity to mutator methods. I.e. only one thread at a time. 2. It also gurantees that updated fields will be visible between threads.
What is the wait loop idiom when using the .wait() concurrent function()?
It is important to use this idiom because it tests your invariant before AND after the wait.synchronized (obj) { while (condition is true) { obj.wait() } doSomeWork();}
Which primitive values are not atomic and why?
long and double are the only primitives that are not atomic. This is because they are both 64 bits in length and are subsequently read in 2, 32 bit operations. Beacuse it involves two operations, atomicity cannot be guranteed without proper locking.
What does it mean for a value to be atomic?
Operations can be performed on it without locking.
Why does synchronization have no effect unless both read and write function of a mutable value are synchronized?
1. Synchronization not only gurantees mutual exclusivity, but it also ensures that mutated values will be shared across threads. 2. One thread could be writing during an update and might miss the update because that actions weren't locked.
How can you achieve both effects of synchronization on an atomic value without using the synchronized keyword?
By declaring the varible to be volatile. A volatile varible ensures that the variable will be shared between threads when the value is updated while an atomic variable ensures mutual exclusivity.
How will a static synchronized method obtain an objects lock?
Since a static method is associated with a class and not an object instance, it will obtain the class lock.
According to Effective Java, what are the different levels of thread safety?
In order of saftey: 1. Immutable 2. Unconditionally safe. 3. Conditionally safe. 4. Not thread safe. 5. Thread hostile.Java Concurrency in Practice has 3 levels: 1. Immutable. 2.Thread safe. 3. Not thread safe.
Why is using the synchronized keyword declaration as the only documentation for thread safety a bad idea?
A single, or even multiple synchronized keywords do not explain the level of thread safety for a class or object.
What is the private lock idiom and why might you use it instead of synchronization?
private final Object lock = new object();public void foo() {synchronized (lock) {}}Can only be used in an unconditionally thread safe class and is beneficial because it thwarts denail of service attacks by not allowing any other classes to obtain the lock. All thread safety is contained in this class

What is a the thread scheduler and why shouldn't you depend on it details?

Thread scheduler in java is the part of the JVM that decides which thread should run.There is no guarantee that which runnable thread will be chosen to run by the thread scheduler.Only one thread at a time can run in a single process.



Applications that use this are not portable because the details are possibly different between JVM implementations.

What is serialization?
Encoding an object to a byte stream.