Java Lib Part 1

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Differentiate between primitive type and composite type data

Role of wrapper classes in Java and explore an example.


Wrapper Classes:

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

Commonly Used Wrapper Classes:

Here are some commonly used wrapper classes and their corresponding primitive types:

Integer: Wraps int

Double: Wraps double

Character: Wraps char

Boolean: Wraps boolean

…and others for different primitive types.

Role of Wrapper Classes:

Determining Data Nature:

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.

Converting Primitives to Objects:

You can create instances of wrapper classes from primitive values. For instance, Integer myInt =
Integer.valueOf(42);.

Converting Objects to Primitives:

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.

Assume the user inputs two integers: 5 and 7

import java.util.Scanner;

public class WrapperExample {


public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

// Read input from the user

System.out.print("Enter the first number: ");

int num1 = scanner.nextInt();

System.out.print("Enter the second number: ");

int num2 = scanner.nextInt();

// Create Integer objects from primitive values

Integer wrappedNum1 = Integer.valueOf(num1);

Integer wrappedNum2 = Integer.valueOf(num2);

// Perform addition using wrapper objects

int sum = wrappedNum1 + wrappedNum2;

System.out.println("Sum of " + num1 + " and " + num2 + " is: " + sum);

// Close the scanner

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.

We perform addition using the wrapper objects.

The result is printed as the sum of the two input numbers.

Remember that wrapper classes provide a bridge between primitive types and objects, making it
easier to work with both in Java.

Advantages of using wrapper classes in Java over primitive data types

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.

Nullability and Default Values:

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.

Generics and Collections:

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.

Autoboxing and Unboxing:

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

// Autoboxing: int to Integer

int primitiveInt = 42;

Integer wrappedInt = primitiveInt; // Automatically boxed

// Unboxing: Integer to int

Integer anotherWrappedInt = Integer.valueOf(100);

int extractedValue = anotherWrappedInt; // Automatically unboxed

Standard Library Methods:

Wrapper classes provide useful methods that are not available for primitive types.

For example:

Integer.parseInt(String) converts a string to an int.

Double.parseDouble(String) converts a string to a double.


1. Which of the following is a composite data type?

a. int b. String

c. char d. float

2. Which of the following is not a wrapper class?

a. Byte b. Int

c. Long d. Float

3. Which of the following is valid method of initialising?

a. Integer a=new (5); b. Integer a=Integer(5);

c. Integer a=new Integer(5); d. Integer a=new (“5”);

4. If s=“123”, which among the following will convert it to an integer?

a. int a=Integer(s); b. int a=(int)s;

c. int a=parseInt(s); d. int a=Integer.parseInt(a);

5. Which among the following is valid character initialisation?

a. Character c=new Character(‘c’); b. Character c=new Character(“c”);

c. Both a and b d. None of these

6. Which among the following methods return a true or a false?


a. toLowerCase() b. toUpperCase()

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

c. java.awt d. None of these

8. Which class is inherited by the Character and Boolean wrapper classes?

a. Object b. Number

c. Both a and b d. None of these

9. What is, converting a primitive value into an object of the corresponding wrapper class called?

a. Autoboxing b. Unboxing

c. Type Casting d. All of these

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

Output: Not an alphabet

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

Ouput: Uppercase Frequency: 6

Lowecase Frequency: 4

You might also like