Java Lib Part 1
Java Lib Part 1
Wrapper classes in Java provide a way to work with primitive data types as objects. They “wrap”
primitive values (such as int, double, char, etc.) into corresponding class objects.
These classes are part of the Java API and are essential for scenarios where you need to treat
primitive types as objects (e.g., when using collections, generics, or reflection).
Here are some commonly used wrapper classes and their corresponding primitive types:
Wrapper classes allow you to determine the nature of data associated with a primitive type. For
example, you can check if an Integer represents a positive or negative value.
You can create instances of wrapper classes from primitive values. For instance, Integer myInt =
Integer.valueOf(42);.
Wrapper classes provide methods to extract primitive values from objects. For example, int
extractedValue = myInt.intValue();.
Example:
Let’s consider an example where we want to calculate the sum of two numbers provided by the
user. We’ll use wrapper classes to handle input and perform the addition.
import java.util.Scanner;
System.out.println("Sum of " + num1 + " and " + num2 + " is: " + sum);
scanner.close();
In this example:
We read two integers from the user using Scanner.
We create Integer objects (wrappedNum1 and wrappedNum2) from the primitive values.
Remember that wrapper classes provide a bridge between primitive types and objects, making it
easier to work with both in Java.
Object-Oriented Approach:
Wrapper classes allow you to treat primitive data types as objects. This aligns with the object-
oriented paradigm, where everything is an object.
By using wrapper classes, you can seamlessly integrate primitive types into Java’s object hierarchy.
Primitive data types cannot represent null values. For example, an int cannot be null.
Wrapper classes, on the other hand, can hold null values. This is useful when dealing with scenarios
where an absence of data needs to be represented (e.g., database fields).
Wrapper classes provide default values (e.g., null for Integer, false for Boolean) when not explicitly
initialized.
Collections (such as List, Set, and Map) work with objects. You cannot directly use primitive types in
collections.
Wrapper classes allow you to store primitive values in collections. For instance, you can create a
List<Integer> to store a list of integers.
Method Overloading:
When defining methods, you can overload them based on parameter types.
Wrapper classes enable method overloading for both primitive and object types. For example, you
can have overloaded methods that accept int or Integer.
Java provides automatic conversion between primitive types and their corresponding wrapper
classes. This process is called autoboxing (primitive to wrapper) and unboxing (wrapper to primitive).
For example
Wrapper classes provide useful methods that are not available for primitive types.
For example:
a. int b. String
c. char d. float
a. Byte b. Int
c. Long d. Float
c. parseInt() d. isUpperCase()
7. What package is a part of the wrapper class which is imported by default into all Java programs?
a. java.util b. java.lang
a. Object b. Number
9. What is, converting a primitive value into an object of the corresponding wrapper class called?
a. Autoboxing b. Unboxing
10. Which among the following function is used to check whether a given character is a tab space
or not?
a. isTab() b. isTabSpace()
c. isSpace() d. isWhitespace()
Que.1 Input a character and check whether it is an alphabet or not, if it is check whether it is in
Uppercase or in lower case.
Example:
INPUT: A
Output: Uppercase
Input: 2
Que2 Input 10 characters and find the frequency of uppercase and lowercase characters separately.
For example:
Input: A, b, c, d, E, F, G, H, I, j
Lowecase Frequency: 4