Java - Intermediate Flashcards
JDK
JDK (Java Development Kit) is a software development environment that provides tools and libraries necessary for developing Java applications.
It includes the compiler, debugger, and other development utilities.
JRE
JRE (Java Runtime Environment) is an implementation of the JVM (Java Virtual Machine) along with libraries and other files required to run Java applications.
JRE does not include development tools.
JVM
JVM (Java Virtual Machine) is an abstract computing machine that executes Java bytecode.
It provides a runtime environment for Java applications to run independently of the underlying hardware and operating system.
Explain the concept of polymorphism in Java
Polymorphism is the ability of an object to take on different forms.
In Java, it allows a subclass to override a method of its superclass, providing its own implementation.
Polymorphism enables code flexibility and reusability.
It allows you to write code that can work with objects of different classes, as long as they are related through inheritance or interface implementation.
For example:
class Shape {
void draw() {
System.out.println(“Drawing a shape”);
}
}
class Circle extends Shape {
@Override
void draw() {
System.out.println(“Drawing a circle”);
}
}
class Rectangle extends Shape {
@Override
void draw() {
System.out.println(“Drawing a rectangle”);
}
}
public class Main {
public static void main(String[] args) {
Shape circle = new Circle();
Shape rectangle = new Rectangle();
circle.draw(); // Output: Drawing a circle rectangle.draw(); // Output: Drawing a rectangle } }
What are the different types of collections available in Java?
List
Set
Map
Queue
List
An ordered collection that allows duplicate elements. Example: ArrayList, LinkedList.
Set
An unordered collection that does not allow duplicate elements. Example: HashSet, TreeSet.
Map
A collection that stores key-value pairs. Example: HashMap, TreeMap.
Queue
A collection that follows the FIFO (First-In-First-Out) principle. Example: LinkedList, PriorityQueue.
Explain the concept of generics in Java and their benefits.
Generics allow the use of type parameters to create generic classes and methods.
They enable the creation of reusable code that can work with different types.
Benefits of generics include compile-time type safety, eliminating the need for explicit type casting, and enabling code reuse.
ArrayList
a resizable array
LinkedList
a doubly-linked list
When would you list an ArrayList vs a LinkedList
ArrayList provides fast random access but slower insertions and deletions, while LinkedList provides fast insertions and deletions but slower random access.
Use ArrayList when you need fast access to elements by index and when the list size doesn’t change frequently.
Use LinkedList when you frequently add or remove elements from the list, particularly from the beginning or middle, and when you don’t require random access to elements.
Explain the concept of threads in Java
A thread in Java represents an independent path of execution within a program. It allows multiple tasks to be executed concurrently
What is synchronization in Java?
Synchronization is a mechanism in Java to control access to shared resources by multiple threads to ensure thread safety.