java Flashcards
JDK vs JRE vs JVM
- Java Development Kit contains compiler and debugger tools to compile the Java source code, also includes Java Runtime Environment and Java Virtual Machine
- The JRE provides an environment and in that environment the JVM runs the compiled byte code. It contains both the library and the JVM.
- The JVM creates a layer of abstraction from the Java Code and the computer environment. It is used to convert the java bytecode into machine readable code.
Class vs Object
- An object is an instantiation of a class
- The class is the blueprint that provides the states and behaviors its instantiated objects will have
What are the class members? (JAVA)
- Instance variables
- Methods
- Constructors
- Inner class
- static/instance blocks
What are the types of variables? (JAVA)
- Instance variable
- Local variable
- Static variable
What are the scopes of the variables? (JAVA)
- Class - inside class
- Method - inside method
- Block - inside for or if statements
What are the Access Modifiers? (JAVA)
- Public - globally accessible
- Protected - accessible within the package and its subclasses
- Default - accessible only within the package
- Private - accessible only within that class
What are the Non-Access Modifiers? (JAVA)
- Static - variable or function is shared between all instances of that class as it belongs to the type, not the actual objects themselves
- Final - define an entity that can only be assigned once. Once a final variable has been assigned, it always contains the same value.
- Abstract - purpose of extending
- Synchronized - used for threads; accessed by only one thread at a time
- Volatile - used for threads; lets JVM know that a thread accessing a variable must merge its own private copy with the master copy in memory
- Transient - used in serialization; field that is transient will not be sent at all and will appear as null on the other end
What is a Constructor? (JAVA)
A method that creates an instance of a class based on the parameters of the constructor
What are the types of Constructors? (JAVA)
- Default (created by the JVM for you if no constructor is specified)
- No args
- Other
What are the 4 pillars of Object Oriented Programming?
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
. What is Abstraction? How to achieve it? (JAVA)
- Abstraction is focused on the concept of reusability which is achieved using generalization or specialization
ACHIEVE WITH:
- Abstract class
- Using a framework, such as Spring
Abstract Class vs Interface (JAVA)
- Can only extend one abstract class
- Can implement as many interfaces as you would like
- If a programmer wants to have method definitions or contain variables that are not static and final, they would use an abstract class. The methods in an abstract class can also be overridden, while in an interface, all the methods must be implemented by the class that implements the interface
What is Polymorphism? How to achieve it?
- Polymorphism is the concept that in Java, objects can have the same states and behaviors, so they are interchangeable
ACHIEVE WITH:
- Method overriding
- Method overloading
What is Inheritance? How to achieve?
- Inheritance is the concept that classes can share states and behaviors through extension.
- Achieve by extending a class
What is Encapsulation? How to achieve?
- Encapsulation is a technique to control an object’s accessibility to its states. You can achieve this control using access modifiers. The access modifiers are public, protected, default, and private.
What is a hashcode? (JAVA)
- A hashcode is an identifier for an object that has been converted to an integer.
Final vs Finally vs Finalize (JAVA)
- Final is a non-access modifier that marks your variable as the value that it is initialized
- Finally refers to the finally clause in a try, catch, finally block. Finally will always
execute, even if there is an exception. - Finalize - a method that the garbage collector invokes when the JVM figures out
that a particular instance should be garbage collected.
What does the keyword “Static” mean? (JAVA)
- Static is a non-access modifier that means you can use that method or variable from a class without instantiating that class.
What is an Exception? What class does it extend?
- Exception extends Throwable, which has Exception and Errors as subclasses and RuntimeException, which is a subclass of Exception.
String vs StringBuilder vs StringBuffer
- String - Immutable (cannot be changed or extended); stored in string pool; thread
safe - StringBuilder - Mutable; for strings changing rapidly; do not extend string because string is final
- StringBuffer - Mutable; synchronized, much slower; do not extend string because string is final; thread safe, useful for threads/strings being manipulated by multiple
threads
What is Serialization?
- Serialization is the process where java objects are converted to byte streams
- Must use the serializable interface to be marked for serialization.
What are the Data Structures in Java?
Data Structures are a way to store and organize data so that it can be used efficiently
Types of Data Structure
- Array - one block of memory; constant access time
- ArrayList - dynamically resizable; object
- Vector - dynamically resizable; object
- LinkedList - uses nodes
- Set - unique values; sortable
- Queue - FIFO
- Tree - binary search tree
- Map - key value; not sortable
- Deque - accessible from both ends
- Stack - LIFO
What is a Generic?
- A Generic is a template which enables programmers to specify generic methods and classes with a single method declaration, a set of related methods, or with a single class declaration
- An example of a Generic is using the diamond syntax, and an example of using the diamond syntax is when you create a reference to a List of some object type and
initialize an ArrayList.
What is a Wrapper Class?
- A Wrapper Class is meant to encapsulate primitive data types to turn them into
objects using an operation of the JVM called autoboxing. You can also unbox an
object to a primitive.
EX: Character, Byte, Integer, Float, …
What is a Collection? What data structures does it provide?
A Collection - a group of individual objects represented as a single unit; an interface with static methods.
- Iterable -> Collection -> Set, List, Queue
What is a Set? What are some types of sets?
An interface which extends collection. A set is an unordered collection of objects
which can only contain unique values.
- HashSet
- LinkedSet
- TreeSet
What is a List? What are some types of lists?
- An interface that implements Collection
- An ordered collection of objects in which duplicate values can be stored
TYPES:
- ArrayList
- LinkedList
- Vector
- Stack
What is a Thread?
- A thread is a part of a process that runs a path of the program in execution. It runs asynchronous to the main method thread.
What is Singleton? How to make one?
- A Singleton is a class that can only have one instance of that class at a time. Its purpose is to limit the number of objects to only one
- To design a Singleton class, you make the constructor
private and write a static accessor method that gets your Singleton.
What are Reflections?
- Reflections refers to the accessing and manipulation of a class at runtime.
- Reflection is generally used in frameworks to operate on objects without having those objects be of any particular class.
What is the difference between ‘Collection’ vs ‘Collections’
- Collection is the interface that implements Iterable. It marks a class as a Collection and therefore also Iterable.
- Collections is the java util package that provides static methods available to
Collection classes.
What are the data types in Java? (8)
- Boolean - true or false
Default false
1 bit - Char - character type whose values are 16-bit Unicode characters
Default \u0000
16 bits - Byte
Default 0
8 bits - Short
Default 0
16 bits - Int
Default 0
32 bits - Long
- Default 0
- 64 bits
- Float
Default 0.0
32 bits - Double
Default 0.0
64 bits
How do you invoke a static method?
- A static method can be invoked within the class that it is declared in without needing to create an instance of that class to invoke the method.
- Outside the defining class, a static method is called by prefixing it with its class name.
What is an Interface?
- An interface is a class that other classes can implement.
. Can I force garbage collection?
- No, you cannot force the garbage collector to scan the heap.
What is the finally block?
- The finally block is the block that executes after a try-catch block.
A finally block will
always execute, even if an exception is thrown.
Comparable vs Comparator
- Comparable is an interface that compares an object based on natural ordering
(numbers and letters). It uses a convention of positive number for greater than the
compared element, negative number for less than the compared element, and 0 for
equal. - Comparator is an interface that implements Comparable and implements its
compareTo() method for custom ordering. Comparator allows you to implement your
own rules.
How do I start a thread implementing Runnable?
- Invoke the run method to start a thread implementing Runnable.
- The Runnable object needs to be passed through the thread constructor and have
invoke start.
Thread methods. (run, start, sleep, wait, notify, notifyAll, join).
Run() - Executes starts the thread of execution.
Start() - Creates a new thread and calls run().
Sleep() - static method of a thread, does not release resource locks.
Wait() - method of an object, releases resource locks.
Notify() - wakes up only a single thread from a wait state to use a resource.
NotifyAll() - wakes up all threads waiting for a resource.
Join()- causes the current thread to wait for another thread to finish.
Difference between the run and start method?
- When you call run() it executes the run() method.
- When you call start() it instantiates a new thread and calls run.
What is a deadlock?
- Deadlock is when two or more threads are waiting for resources that the other(s)
have.
What is shadowing?
- Shadowing is when two variables or objects with the same name and type have overlapping scopes.
- When this happens the field with the larger scope is hidden.
How do you insert elements in a Map?
- Invoke the maps put() method.
What is synchronization?
- Synchronization is the process of making resources thread-safe
- Done using the synchronize keyword
- Makes object only accessible by one thread
When is an object ready for garbage collection?
- An object is ready for garbage collection when there is no longer any reference to it
in memory. - This typically happens when the object goes out of scope
What is Starvation?
- Occurs when two threads are sharing resources: producer is creating resources and
the consumer is using them - When the producer creates so fast that the consumer cannot keep up, it reaches a
point where it can no longer produce, causing it to starve on runtime
InputStream vs Reader
- InputStream is a byte stream (used for serialized data).
- Reader is a character stream (used for reading character data (files)).
What is the parent of all exceptions?
The hierarchy is:
- Throwable
- Exception
- RuntimeException
- Error
How do I make an object Serializable?
- Implement the Serializable Interface
- serializable is a marker interface (has no methods)
Annotations. (JUNIT)
- @Test - Denotes a method as a test.
- @Before - Runs before every test.
- @BeforeClass - Runs once before the class is run.
- @After - Runs after each test.
- @AfterClass - Runs once after the tests are done.
- @Ignore - ignores a test temporarily
How do you test your application automatically?
- Certain processes and applications can use hooks to run tests when a commit or push is
made as well (Jenkins, husky).