Java - Intermediate Flashcards
(31 cards)
JDK
JDK (Java Development Kit) is a software development environment that provides tools and libraries necessary for developing Java applications.
It includes the:
1. compiler,
2. debugger,
4. 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.
JVM
- JVM (Java Virtual Machine)
- an abstract computing machine that executes Java bytecode.
- provides a runtime environment for Java applications (to run independently of the underlying hardware and operating system.)
Explain the concept of polymorphism in Java
- it allows a subclass to override a method of its superclass, providing its own implementation.
- Polymorphism enables:
- code flexibility
- and reusability.
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
- Ordered
- Allows duplicates
- Example:
- ArrayList,
- LinkedList
Set
- Unordered
- Does not allow duplicates
- Example:- HashSet
- TreeSet
Map
- An interface for a collection that stores data in key value pairs
- The way it orders depends on the implementation type:
- HashMap doesn’t guarantee order
- TreeMap sorts based on natural order of the keys
- Example:
- HashMap,
- TreeMap,
- LinkedHashMap,
- ConcurrentHashMap
Queue
Follows the FIFO (First-In-First-Out) principle.
Explain the concept of generics in Java
- Generics allow the use of type parameters to create generic classes and methods.
- provide compile-time type checking, ensuring that the correct types are used at compile time and preventing type-related errors at runtime
What are the benefits of Generics?
- compile-time type safety,
- and thus preventing type-related errors at runtime
ArrayList
ArrayList is the List implemented for a resizable array.
- Dynamic Size
- Can access elements by index
- Ordered
- Duplicates allowed
- Null elements allowed
- Resizes by a factor of 1.5 or 2
LinkedList
- double linked - knows prev and next node
- dynamically sized
- ordered
- duplicates allowed
- nulls allowed
Sequential access (walking the list) is better than random access.
When should you use an ArrayList?
- When you need fast access to elements by index
- and when the list size doesn’t change frequently.
When should you use LinkedLists?
- When you need to add or remove an element from the beginning or end,
- When you don’t know the size of the list you’re going to be working with.
- When you will be frequently adding or removing elements from the list. (ArrayLists have to shift each element forward or backward in the list whereas a linked list only has to update it’s prev/next pointers).
Explain the concept of threads in Java
- A thread represents an independent path of execution within a program.
- It allows multiple tasks to be executed concurrently.
What is synchronization in Java?
Ensures that only one thread can access a shared resource at a time.
Serialization
- object -> byte stream
- which can be stored in a:
- file,
- sent over a network,
- or persisted in a database.
Deserialization
byte code -> object
How do you serialize and desserialize a class?
- The Serializable interface is used to mark a class as serializable
- and the ObjectOutputStream is used to serialize an object
- and ObjectInputStream is used to deserialize an object.
What are annotations in Java?
Annotations provide metadata about program elements, such as:
1. classes,
2. methods,
3. or fields.
From Java 8 How do you handle dates and times in Java?
- 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 are the advantadges of the Date and Time API in Java 8?
- improved functionality,
- immutability - once an object is created it’s state cant be changed
- thread safety,
- and better support for operations like
- parsing,
- formatting,
- and calculations.
What are lambda expressions in Java?
- anonymous functions
- uses a more concise arrow syntax.
- helpful for things like implementing Functional Interfaces (an interface with only one method) instead of using anonymous inner classes.