0% found this document useful (0 votes)
20 views6 pages

Java Wrapper Classes Overview

The document discusses Java wrapper classes and their functions. It provides examples of wrapper classes like Integer, Character, etc. and functions like parseInt(), toString() that convert between primitive types and wrapper objects. Multiple choice questions are given to test understanding of wrapper classes, autoboxing, parsing functions etc. True/false and coding questions are also provided to further assess the concepts.

Uploaded by

Sukhpreet Kaur
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views6 pages

Java Wrapper Classes Overview

The document discusses Java wrapper classes and their functions. It provides examples of wrapper classes like Integer, Character, etc. and functions like parseInt(), toString() that convert between primitive types and wrapper objects. Multiple choice questions are given to test understanding of wrapper classes, autoboxing, parsing functions etc. True/false and coding questions are also provided to further assess the concepts.

Uploaded by

Sukhpreet Kaur
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Library Classes

Note:
Package is a collection of classes.
By default, [Link] package is imported in a Java program.
The user can create a data type by using a class.
The data type int is included in Integer wrapper class.
Wrapper class uses first letter in upper case.
String data is converted to float type by using [Link](String) function.
Conversion from primitive type to wrapper object is called as autoboxing.
Each character oriented function uses a wrapper tag Character.

State whether the following statements are True/False


Java class library includes all the packages True
Wrapper classes belong to [Link] package. False
Array is a non-primitive data type. True
Class is a composite data type. True
[Link]() converts integer data to String. True

Choose appropriate option for the following statements


Question 1
A package contains:
1. tags
2. classes ✓
3. data
4. arrays
Question 2
Each primitive data type belongs to a specific:
1. block
2. object
3. wrapper class ✓
4. none
Question 3
Automatic conversion of primitive data into an object of wrapper class is called:
1. autoboxing ✓
2. explicit conversion
3. shifting
4. none
Question 4
The parseInt() function is a member of:
1. integer wrapper class ✓
2. character wrapper class
3. boolean wrapper class
4. none
Question 5
valueOf() function converts:
1. Primitive type to String
2. String to primitive type ✓
3. character to String
4. None
Answer the following questions
Question 1
What is a package?
In Java, a package is used to group related classes. Packages are of 2 types:
1. Built-In packages — These are provided by Java API
2. User-Defined packages — These are created by the programmers to efficiently
structure their code.
[Link], [Link] are a couple of examples of built-in packages.

Question 2
What is the significance of '*' while importing a package?
The asterisk(*) sign indicates that all the classes in the imported package can be used in
the program.

Question 3
Define a wrapper class.
Wrapper classes wrap the value of a primitive type in an object. Wrapper classes are
present in [Link] package. The different wrapper classes provided by Java are Boolean,
Byte, Integer, Float, Character, Short, Long and Double.

Question 4
Differentiate between:
(a) isUpperCase() and toUpperCase()
isUpperCase() toUpperCase()
It is used to check if the character given as its It is used to convert the character given as its
argument is in upper case or not. argument to upper case
Its return type is boolean Its return type is char
(b) parseInt() and toString() functions
parseInt() toString()
It converts a string to an integer It converts an integer to a string
Its return type is int Its return type is String
(c) primitive type and composite type data
Primitive Data Types Composite Data Types
Primitive Data Types are Java's fundamental data Composite Data Types are created by
types using Primitive Data Types
Primitive Data Types are built-in data types defined by Composite Data Types are defined by the
Java language specification programmer
Examples of Primitive Data Types are byte, short, int, Examples of Composite Data Types are
long, float, double, char, boolean Class and Array

Question 5(i)
int res = 'A';
What is the value of res?
Value of res is 65.

Question 5(ii)
Name the package that contains wrapper classes.
[Link]

Question 5(iii)
Write the prototype of a function check which takes an integer as an argument and returns a
character.
char check(int n)
Write the purpose of the following functions
Question 1
[Link]()
It is used to convert string data into float data.

Question 2
[Link]()
It is used to convert double data to a string.

Question 3
[Link]()
It is used to convert string data into the Integer wrapper object.

Question 4
[Link]()
It is used to check if the character given as its argument is a digit.

Question 5
[Link]()
It is used to check if the character given as its argument is a whitespace.
Find the output of the following program snippets

Question 1
char ch = '*';
boolean b = [Link](ch);
[Link](b);
Output
false
Explanation
As Asterisk (*) is not a letter so [Link]() method returns false.

Question 2
char c = 'A';
int n = (int) c + 32;
[Link]((char)n);
Output
a
Explanation
int n = (int) c + 32 ⇒ 65 + 32 ⇒ 97
So, variable n get the value of 97. 97 is the ASCII code of small a so casting n to char, prints a to
the console.

Question 3
String s= "7";
int t =[Link](s);
t=t+1000;
[Link](t);
Output
1007
Explanation
[Link]() converts "7" into an int value i.e. the decimal number 7. t+1000 adds the number
7 to 1000 giving 1007 as the output.
Question 4
char c = 'B';
int i = 4;
[Link](c+i);
[Link]((int)c+i);
Output
70
70
Explanation
In the expression c + i, c is of type char and i is of type int. As int is the higher type so char gets
promoted to int. Thus, ASCII code of 'B' which is 66 is added to 4 giving the output as 70. This is
an example of implicit type conversion.
In the next expression (int)c + i, c which is of char type is explicitly casted to int. Again, ASCII code
of 'B' which is 66 is added to 4 giving the output as 70. This is an example of explicit type
conversion.

Question 5
char ch = 'y';
char chr = [Link](ch);
int p = (int) chr;
[Link](chr + "\t" + p);
Output
Y 89
Explanation
[Link]()method converts small y to capital Y so chr gets the value of 'Y'. Casting
chr to int gives the ASCII code of 'Y' which is 89.

Question 6
int n = 97;
char ch = [Link]((char)n);
[Link](ch + " Great Victory");
Output
A Great Victory
Explanation
97 is the ASCII code of small a so [Link]((char)n) returns capital A which is
stored in ch.

Question 7
char ch = 'x'; int n = 5;
n = n + (int)ch;
char c = (char)n;
[Link]((char)((int)c-26));
Output
c
Explanation
As ASCII code of 'x' is 120, so the expession n + (int)ch ⇒ 5 + 120 ⇒ 125. After that, the
expression (char)((int)c-26) ⇒ (char)(125 - 26) ⇒ (char)99 ⇒ 'c' as ASCII code of 'c' is 99. So, c is
the final output.

Question 8
char ch = 'A';
char chr = [Link](ch);
int n = (int)chr-32;
[Link]((char)n + "\t" + chr);
Output
A a
Explanation
[Link]() converts 'A' to 'a'. ASCII code of 'a' is 97. n becomes 65 — (int)chr-32 ⇒
97 - 32 ⇒ 65. 65 is the ASCII code of 'A'.

Java Programs
Question 1
Write a program in Java to input a character. Find and display the next 10 th character in the ASCII
table.
import [Link];

public class TenthChar


{
public static void main(String args[]) {

Scanner in = new Scanner([Link]);


[Link]("Enter a character: ");
char ch = [Link]().charAt(0);
char nextCh = (char)(ch + 10);
[Link]("Tenth character from "
+ ch + " is " + nextCh);
}
}

Question 2
Write a program in Java to input a character. Display next 5 characters.
import [Link];

public class FiveChars


{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter a character: ");
char ch = [Link]().charAt(0);

[Link]("Next 5 characters from "


+ ch + " are:");

for (int i = 1; i <= 5; i++) {


[Link](++ch);
}
}
}

Question 4
Write a program to input a set of 20 letters. Convert each letter into upper case. Find and display
the number of vowels and number of consonants present in the set of given letters.
import [Link];
public class 20LetterSet {
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter any 20 letters");
int vc = 0, cc = 0;
for (int i = 0; i < 20; i++) {
char ch = [Link]().charAt(0);
ch = [Link](ch);
if (ch == 'A' ||
ch == 'E' ||
ch == 'I' ||
ch == 'O' ||
ch == 'U')
vc++;
else if (ch >= 'A' && ch <= 'Z')
cc++;
}
[Link]("Number of Vowels = " + vc);
[Link]("Number of Consonants = " + cc);
}
}

Question 6
Write a program to input two characters from the keyboard. Find the difference (d) between their
ASCII codes. Display the following messages:
If d=0 : both the characters are same.
If d<0 : first character is smaller.
If d>0 : second character is smaller.
Sample Input :
D
P
Sample Output :
d= (68-80) = -12
First character is smaller
import [Link];

public class ASCIIDiff


{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter first character: ");
char ch1 = [Link]().charAt(0);
[Link]("Enter second character: ");
char ch2 = [Link]().charAt(0);
int d = (int)ch1 - (int)ch2;
if (d > 0)
[Link]("Second character is smaller");
else if (d < 0)
[Link]("First character is smaller");
else
[Link]("Both the characters are same");
}}

Common questions

Powered by AI

Primitive data types are the fundamental built-in types defined by Java (such as int, byte, char), whereas composite data types are defined by the programmer using these primitive types, examples being classes and arrays. Primitive data types are the building blocks for data manipulation in Java, while composite data types combine them to form complex data structures .

The Java program for converting letters to uppercase and counting vowels and consonants illustrates principles of data manipulation by transforming input using Character.toUpperCase(), and leveraging loops and conditional statements to iterate and evaluate each letter. Control flow is demonstrated through iterative structures for processing input and logical structures that classify and count letters based on their ASCII ranges, showcasing comprehensive data handling .

The java.lang package plays a critical role in Java by providing classes that are fundamental to the design of the Java programming language, including classes that manage data and perform system operations. It contains wrapper classes such as Integer, Boolean, and Character, which allow primitive types to be used as objects .

The autoboxing feature influences coding style by providing a more seamless interaction between primitive types and objects, reducing the need for manual type conversion and thereby leading to cleaner, more readable code. However, excessive use of autoboxing can affect performance by introducing unnecessary object creation and garbage collection overhead, which developers must consider in efficiency-critical applications .

Java supports the conversion of primitive types into their corresponding object types through the process called autoboxing. Autoboxing is the automatic conversion that the Java compiler makes between the primitive types (int, double, etc.) and their corresponding object wrapper classes (Integer, Double, etc.).

The Character.toUpperCase() function in Java converts a given character to its uppercase equivalent. This affects ASCII values because it changes the representation of the character. For example, converting the lowercase 'y' changes its ASCII value from 121 to 89 for 'Y', as demonstrated in character conversion programs .

Functions like Float.parseFloat() and Double.toString() play crucial roles in Java's handling of data types by enabling conversions between strings and numeric formats. Float.parseFloat() converts strings into float data types for calculations, while Double.toString() enables the conversion of double values into strings for display or further string manipulation, thus facilitating seamless data type transitions within programs .

The asterisk '*' in the context of importing Java packages signifies that all classes within a package are available for use in the program. It simplifies code by reducing the need to import individual classes explicitly, although it can potentially lead to namespace conflicts if not managed carefully .

The parseInt() method demonstrates type conversion by converting a String to an integer, which is significant for data manipulation because it allows strings (often used in user input) to be transformed into numeric data types capable of arithmetic operations, enhancing program functionality and flexibility .

Implicit type conversion in Java is governed by automatic conversion from a lower to a higher data type, ensuring data compatibility without data loss—such as a char converted to int within expressions. Explicit type conversion, or casting, is used when converting higher data types back to lower, requiring manual intervention to confirm intended behavior and prevent data loss .

You might also like