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.
How do you ensure thread safety?
The synchronized keyword can be used to create synchronized blocks or methods.
It ensures that only one thread can execute the synchronized code block or method at a time.
Synchronization can prevent race conditions and data corruption when multiple threads access and modify shared data simultaneously.
Serialization
the process of converting an object into a byte stream, which can be stored in a file, sent over a network, or persisted in a database.
Deserialization
reconstructing an object from the serialized byte stream.
How do you serialize and deserialize an object?
The Serializable interface is used to mark a class as serializable,
the ObjectOutputStream and ObjectInputStream classes are used for serialization and deserialization.
What are annotations in Java?
Annotations provide metadata about program elements, such as classes, methods, or fields.
How do you handle dates and times in Java in Java 8 and later versions?
In Java 8 and later versions, the Date and Time API (java.time package) was introduced.
It provides classes like LocalDate, LocalTime, LocalDateTime, and ZonedDateTime for handling dates, times, and time zones.
What advantadges does the Date and Time API bring in Java 8?
offers improved functionality, immutability, thread safety, and better support for operations like parsing, formatting, and calculations.
What are lambda expressions in Java?
Lambda expressions are anonymous functions that can be used to provide a concise and functional way of representing behavior in Java.
How do Lambda expressions simplify functional programming?
They simplify functional programming by allowing the representation of behavior as data and enabling the use of functional interfaces (interfaces with a single abstract method) like Predicate, Consumer, and Function.
Lambda expressions eliminate the need for creating anonymous inner classes for implementing functional interfaces and provide a more expressive and compact way of writing code.