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:
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).