Java Flashcards
Final
final class can't be inherited final method can't be overridden final variable value can't be changed
Finally
code will be executed whether exception is handled or not.
Finalize
used to perform clean up processing just before object is garbage collected
static
modifier associated with the class rather than with any object.
Generics
“a type or method to operate on objects of various types while providing compile-time type safety”
Java Generic methods and generic classes enable programmers to specify, with a single method declaration, a set of related methods, or with a single class declaration, a set of related types, respectively.
Generics also provide compile-time type safety that allows programmers to catch invalid types at compile time.
type erasure:
- all the extra information added using generics into source code will be removed from byte code generated from it
- inside byte code will be the old java syntax without generics. This helps in generating and executing code written prior to Java 5 (before generics)
StringBuilder vs. StringBuffer
StringBuffer is synchronized
StringBuilder is not synchronized (faster)
Both are mutable (String is immutable)
ArrayList vs. Vector
ArrayList is synchronized
Vector is not synchronized
ArrayList grows by 50%
Vector doubles in size
ArrayList can only use Iterator
Vector can use Iterator and Enumerator
Checked exceptions vs. Unchecked exceptions
Checked exceptions:
- checked at compile time
- can anticipate and try to recover from
- should try to catch
- if a method throws a checked exception, then the method must either handle the exception, or it must specify the exception using throws keyword.
- parent class is Exception
- IOException
- SQLException
- DataAccessException
- ClassNotFoundException
- InvocationTargetException
- MalformedURLException
Unchecked exceptions:
- handling is NOT verified during compile time.
- occur because of bad programming.
- parent class is RuntimeException.
- NullPointerException
- ArrayIndexOutOfBound
- IllegalArgumentException
- IllegalStateException
Error
a subclass of Throwable
- indicates serious problems that a reasonable application should not try to recover from.
- most are abnormal conditions.
- some of the common Errors are OutOfMemoryError and StackOverflowError.
static binding
a binding that occurs during compilation.
also called early binding because it happens before a program runs.
actual object is not used for binding.
Example: method overloading.
volatile
variables are stored in main memory (not CPU cache)
changes to them are visible across threads
synchronized
keyword used in method declarations and code blocks
instance methods are synchronized over the instance of the class owning the method. Only one thread per instance of the class can execute this method.
static methods are synchronized on the Class object associated with the class. Only one Class object exists per JVM per class, so only one thread can execute inside a static synchronized method per class, irrespective of the number of instances it has.
all synchronized blocks synchronized on the same object can only have one thread executing inside them at the same time. All other threads attempting to enter the synchronized block are blocked until the thread inside the synchronized block exits the block.
synchronized block within method: public void performSynchronizedTask() { synchronized (this) { setCount(getCount()+1); } }
synchronized block within static method:
public static void performStaticSyncTask(){
synchronized (SynchronizedBlocks.class) {
setStaticCount(getStaticCount() + 1);
}
}
atomic
operation can be safely performed in parallel on multiple threads without using the synchronized keyword or locks
thread contention
a condition where one thread is waiting for a lock/object that is currently being held by another thread. Therefore, this waiting thread cannot use that object until the other thread has unlocked that particular object.
race condition
occurs when two or more threads can access shared data, and they try to change it at the same time.
Because the thread scheduling algorithm can swap between threads at any time, you don’t know the order in which the threads will attempt to access the shared data. Therefore, the result of the change in data is dependent on the thread scheduling algorithm, i.e. both threads are “racing” to access/change the data.