Assignment 3
1. Explain Wrapper Class
A wrapper class in Java provides a way to use primitive data types (int, char, float, etc.) as objects.
Java provides wrapper classes for each of the eight primitive types:
- Integer
- Byte
- Short
- Long
- Float
- Double
- Character
- Boolean
Wrapper classes are useful in situations where objects are required instead of primitives, such as in
collections like ArrayList.
2. Explain Auto Boxing & Unboxing
- Auto-boxing is the automatic conversion of primitive types into their corresponding wrapper class
objects.
- Unboxing is the automatic conversion of wrapper class objects back into primitive types.
Auto-boxing and unboxing help in reducing manual conversion, making code cleaner and more
readable.
3. What is an Array in Java and its Usage?
An array in Java is a data structure that allows storing multiple values of the same type in a single
variable. Arrays are used when we need to handle large amounts of data efficiently.
- Usage: Arrays are used in sorting, searching algorithms, and storing multiple elements efficiently.
4. What is a Vector in Java?
A Vector is a resizable array that grows dynamically as needed. It is synchronized, meaning it is
thread-safe.
- Usage: It is used when we require thread-safety while handling dynamic arrays.
5. How to Use String & StringBuffer in Java?
- String: Strings in Java are immutable, meaning their value cannot be changed once created.
- StringBuffer: It is a mutable sequence of characters, allowing modifications without creating new
objects.
- Usage: StringBuffer is used when frequent modifications to a string are required.
6. How to Use a Vector for Storing a List of Objects?
A Vector in Java can store a list of objects dynamically. Since it implements the List interface, it can
be used just like an ArrayList but with thread-safety.
7. Why is a Wrapper Class Useful?
- It allows primitives to be used in collections.
- Provides utility methods for data conversions.
- Supports auto-boxing and unboxing for easier type conversions.
Wrapper classes help bridge the gap between primitive types and objects in Java.