Java Basics Long Answers
Java Basics Long Answers
Definition of Array:
An array is a collection of elements stored in contiguous memory locations. All the elements
in an array are of the same data type, and they are accessed using a common name with an
index.
Features:
- Fixed size
- Same data type
- Indexed access (starting from 0)
- Efficient memory management
Types:
- Single Dimensional Array: int[] arr = {1, 2, 3};
- Multi-Dimensional Array: int[][] matrix = {{1,2},{3,4}};
Advantages:
- Easy access
- Memory efficient
Disadvantages:
- Fixed size
- Costly insert/delete
Definition of OOPs:
Object-Oriented Programming (OOP) is a paradigm based on the concept of "objects", which
can contain data and methods.
Major Concepts:
- Class: Blueprint of objects.
- Object: Instance of a class.
- Inheritance: Reuse properties.
- Polymorphism: One interface, many forms.
- Encapsulation: Binding data and code.
- Abstraction: Hiding complexity.
2. Different Types of Constructors
Definition:
Constructor is a special method used to initialize objects in Java. It has the same name as the
class and no return type.
Types:
1. Default Constructor:
- No parameters.
- Java provides it automatically.
2. Parameterized Constructor:
- Takes parameters to assign values.
3. Copy Constructor:
- Takes another object of same class as argument.
- Java doesn’t provide it by default.
Constructors can be overloaded and can call each other using this().
Definition:
Operators are symbols used to perform operations on variables and values.
How it Works:
- JVM monitors objects.
- If no references, object is marked for GC.
- Memory is reclaimed for reuse.
Phases:
- Marking
- Deletion
- Compaction
Types of GC:
- Serial, Parallel, CMS, G1
Advantages:
- Automatic memory management
- Avoids memory leaks
Limitations:
- No control over GC timing
- May cause pause in execution