0% found this document useful (0 votes)
2 views8 pages

Chapter 2 Library Class

Chapter 2 discusses Library Classes in Java, focusing on wrapper classes that allow primitive data types to be treated as objects. It explains the conversion methods between strings and primitive types, as well as character-oriented functions, autoboxing, and unboxing. The chapter also includes example programs and important questions related to the concepts covered.

Uploaded by

merajnadaf442
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)
2 views8 pages

Chapter 2 Library Class

Chapter 2 discusses Library Classes in Java, focusing on wrapper classes that allow primitive data types to be treated as objects. It explains the conversion methods between strings and primitive types, as well as character-oriented functions, autoboxing, and unboxing. The chapter also includes example programs and important questions related to the concepts covered.

Uploaded by

merajnadaf442
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

Chapter 2

Library Classes
Package: It is a collection of class which are included in the program as per the user choice.

Wrapper Class
A wrapper class is a class that allows primitive(predefined/Built-in) data types to be used
as objects.
Wrapper classes that are a part of Java library package. i.e [Link]
When an object of a wrapper class is created, a field in the memory is allocated to
contain(Store) a primitive data.
Integer is a wrapper class that contains primitive data type ‘int’ & it has following functions:
• [Link]( ): To convert a String into Integer data type
• [Link]( ): To convert an Integer data type into String.

Need of using Wrapper classes


• To store Primitive values in objects.
• To convert a String into Primitive types & vice-versa.

There are 8 wrapper classes in java.


Wrapper Class Primitive data type
Character char
Byte byte
Short short
Integer int
Long long
Float float
Double double
Boolean boolean

Methods of Wrapper Class


1) Conversion from String to Primitive data type
i. String type to Integer type data
Syntax: int <variable> = [Link](<String argument>); or
int <variable> = [Link](<String argument>);
Ex: String s=”25”;
int n=[Link](s); or int n=[Link](s);
[Link](“String into Integer type:”+n);

ii. String to Long type data


Syntax: long <variable> = [Link](<String argument>); or
long <variable> = [Link](<String argument>);
Ex: String s=”25”;
long n=[Link] (s); or long n=[Link](s);
[Link](“String into Long type:”+n);

iii. String to Float type data


Syntax: float <variable> = [Link](<String argument>); or
float <variable> = [Link](<String argument>);
ICSE CLASS X COMPUTER APPLICATIONS LIBRARY CLASS Page no 1
Ex: String s=”25”;
float n= [Link](s); or float n= [Link] (s);
[Link](“String into Float type:”+n);

iv. String to Double type data


Syntax: double <variable> = [Link](<String argument>); or
double <variable> = [Link] (<String argument>);
Ex: String s=”25”;
double n= [Link](s); or double n= [Link] (s);
[Link](“String into Double type:”+n);

2) Conversion from Primitive data type to String


i. Integer type data to String type
Syntax: String <variable> = [Link](<int argument>);
Ex: int n=25;
String s=[Link](n);
[Link](“Integer into String type:”+n);

ii. Long type data to String type


Syntax: String <variable> = [Link](<long argument>);
Ex: long n=25;
String s=[Link](n);
[Link](“Long into String type:”+n);

iii. Float type data to String type


Syntax: String <variable> = [Link](<float argument>);
Ex: float n=25.0F;
String s=[Link](n);
[Link](“Float into String type:”+n);

iv. Double type data to String type


Syntax: String <variable> = [Link](<double argument>);
Ex: double n=25.00D;
String s=[Link](n);
[Link](“Double into String type:”+n);

Program to convert String to Primitive type data.


class StringtoPDT
{
public static void main(String args[])
{
String s="25";
byte b=[Link](s); //Conversion from String to Byte DT
[Link](b);
String s1="26";
short sh=[Link](s1); //Conversion from String to Short DT
[Link](sh);
String s2="27";

ICSE CLASS X COMPUTER APPLICATIONS LIBRARY CLASS Page no 2


int i=[Link](s2); //Conversion from String to Integer DT
[Link](i);
String s3="28";
long l=[Link](s3); //Conversion from String to Long DT
[Link](l);
String s4="29";
float f=[Link](s4); //Conversion from String to Float DT
[Link](f);
String s5="30";
double d=[Link](s5); //Conversion from String to Doubel DT
[Link](d);
}
}
Output:

Program to convert Primitive data type to String


class PDTtoString
{
public static void main(String args[])
{
byte b=2;
String s=[Link](b); //Conversion from Byte DT to String
[Link]("Byte into String type:"+s);
short a=23;
String s1=[Link](a); //Conversion from Short DT to String
[Link]("Short into String type:"+s1);
int n=25;
String s2=[Link](n); //Conversion from Integer DT to String
[Link]("Integer into String type:"+s2);
long l=25;
String s3=[Link](l); //Conversion from Long DT to String
[Link]("Long into String type:"+s3);
float f=25.00F;
String s4=[Link](f); //Conversion from Float DT to String
[Link]("Float into String type:"+s4);
double d=25.000D;
String s5=[Link](d); //Conversion from Double DT to String
[Link]("Double into String type:"+s5);
char c='2';
String s6=[Link](c); //Conversion from Character DT to String
[Link]("Character into String type:"+s6);

ICSE CLASS X COMPUTER APPLICATIONS LIBRARY CLASS Page no 3


boolean b1=true;
String s7=[Link](b1); //Conversion from Boolean DT to String
[Link]("Boolean into String type:"+s7);
}
}
Output:

Character Type data


A single letter or digit or any special symbol enclosed within single quotes.
Size of char data type is 2 bytes(16 bits).
Ex: ‘a’ ‘7’ ‘@’ ‘*’
Assigning character literals to a variable
Char c=’7’; char c1=’a’; char c2=’#’;

Input a character
1) By using Scanner class
<char> <variable> = <Scanner Object>.,next().charAt(0)>;
Ex: char ch = [Link]( ).charAt(0);

2) By using InputStreamReader class


<char> <variable> = (char)(BufferedReader [Link]());
Ex: char ch = (char)([Link]( ));

Character Oriented Functions


i. [Link]( )
This function is used to check whether given argument is a letter or not. It returns a
boolean type value either true or false.
Syntax: boolean <variable> = [Link](<character>);
Ex: boolean p = [Link](‘N’);
[Link](“Entered character is a letter=”+p); //Output is True

ii. [Link]( )
This function is used to check whether given argument is a Digit or not. It returns a
boolean type value either true or false.
Syntax: boolean <variable> = [Link](<character>);
Ex: boolean p = [Link](‘7’);
[Link](“Entered Character is a Digit=”+p); //Output is True

ICSE CLASS X COMPUTER APPLICATIONS LIBRARY CLASS Page no 4


iii. Character .isLetterOrDigit( )
This function is used to check whether given argument is a letter or a digit. It returns
a boolean type value either true or false.
Syntax: boolean <variable> = Character .isLetterOrDigit (<character>);
Ex: boolean p = [Link] (‘N’);
[Link](“Entered character is a letter or Digit=”+p); //Output is True

Ex: boolean p = [Link] (‘7’);


[Link](“Entered Character is a letter or Digit=”+p); //Output is True

Ex: boolean p = Character .isLetterOrDigit (‘*’);


[Link](“Entered Character is a letter or Digit=”+p); //Output is False

[Link] .isWhitespace( )
This function is used to check for an existing blank or gap in a string. It returns a
boolean type value either true or false.
Syntax: boolean <variable> = Character .isWhitespace (<character>);
Ex: boolean p = Character .isWhitespace (‘ ’);
[Link](“Existing Blank or Gap=”+p); //Output is True

v. Character .isUpperCase( )
This function is used to check whether given argument is a Uppercase or not. It
returns a boolean type value either true or false.
Syntax: boolean <variable> = Character .isUpperCase (<character>);
Ex: boolean p = Character .isUpperCase (‘ N’);
[Link](“Entered character is a Uppercase:”+p); //Output is True

[Link] .isLowerCase( )
This function is used to check whether given argument is a Lowercase or not. It
returns a boolean type value either true or false.
Syntax: boolean <variable> = Character .isLowerCase (<character>);
Ex: boolean p = Character .isLowerCase (‘ n’);
[Link](“Entered character is a Lowercase:”+p); //Output is True

vii. Character .toUpperCase( )


This function returns the given argument in Upper case letter. The given letter
remains same if it is already in uppercase. The given Character remains same if it is
not a letter. It returns a character type value.
Syntax: char <variable> = Character .toUpperCase (<character>);
Ex: char p = Character .toUpperCase (‘n’);
[Link](“Converting character to Uppercase:”+p); //Output is N
viii. Character .toLowerCase( )
This function returns the given argument in Lower case letter. The given letter
remains same if it is already in lowercase. The given Character remains same if it is
not a letter. It returns a character type value.
Syntax: char <variable> = Character .toLowerCase (<character>);
Ex: char p = Character .toLowerCase (‘N’);
ICSE CLASS X COMPUTER APPLICATIONS LIBRARY CLASS Page no 5
[Link](“Converting character to Lowercase:”+p); //Output is n

ix. Conversion from Characters to ASCII code and Vice versa


i. Converting Characters to ASCII Code
Syntax: int <variable1> = (int)char <variable2>;
For Ex: char c = ‘B’;
int x = (int)c;
[Link](x); //Output is 66

ii. Converting ASCII Code to Characters


Syntax: char <variable1> = (char) int<variable2>;
For Ex: int n=97;
char c = (char)n;
[Link](c); //Output is a

Autoboxing
The automatic conversion of Primitive data type into an object of its equivalent wrapper
class.
Syntax: <wrapper class> <object> = new <wrapper class>(primitive value);
For ex: Integer val = new Integer(25);
Double dou = new Double(27.12345);

Unboxing
Unboxing is a opposite of autoboxing. It is a system of converting an object of wrapper
class into primitive data type.
Syntax: <Primitive data type> <variable> = <wrapper object>;
For ex: Integer val = new Integer(25);
int y= val;

ICSE CLASS X COMPUTER APPLICATIONS LIBRARY CLASS Page no 6


Program Based on Character Oriented Function.

class CharFunction
{
public static void main(String args[])
{
boolean p = [Link]('N'); //To check whether a given argument is letter or not
[Link]("Entered character is a letter ="+p);
boolean p1 = [Link]('7'); //To check whether a given argument is Digit or not
[Link]("Entered character is a Digit ="+p1);
boolean p2 = [Link] ('N'); //To check whether a given argument is letter or Digit
[Link]("Entered character is a letter or Digit ="+p2);
boolean p3 = [Link] ('7'); //To check whether a given argument is letter or Digit
[Link]("Entered Character is a letter or Digit ="+p3);
boolean p4 = Character .isLetterOrDigit ('*'); //To check whether a given argument is letter or Digit
[Link]("Entered Character is a letter or Digit ="+p4);
boolean p5 = [Link] (' '); //To check for an existing blank or gap in a string
[Link]("Existing Blank or Gap ="+p5);
boolean p6 = [Link] ('N'); //To check whether given argument is a Uppercase or not
[Link]("Entered character is a Uppercase:"+p6);
boolean p7 = Character .isLowerCase ('n'); //To check whether given argument is a lowercase or no t
[Link]("Entered character is a Lowercase:"+p7);
char p8 = [Link] ('n'); //to convert the given argument in Upper case letter
[Link]("Converting character to Uppercase:"+p8);
char p9 = [Link] ('N'); //to convert the given argument in Lower case letter
[Link]("Converting character to Lowercase:"+p9);
char c = 'B'; //Converting Characters to ASCII Code
int x = (int)c;
[Link](x);
int n=97; //Converting ASCII Code to Characters
char c1 = (char)n;
[Link](c1);

}
}
Output:

ICSE CLASS X COMPUTER APPLICATIONS LIBRARY CLASS Page no 7


Important Questions (from Previous year Board & Specimen copies)
1. Give the output of the following Character class methods: [2023 B] [2M]
(a) [Link] ('a')
(b) [Link]('#')

2. Define Boxing with an Example. [2023 SP] [2M]

3. Differentiate between boxing and unboxing. [2024 B] [2M]

4. Write the value of n after execution: [2023 B] [2M]


char ch ='d';
int n = ch + 5;

5. Predict the output of the following code snippet: [2025 SP] [2M]
char ch='B',
char chr=[Link](ch);
int n=(int)chr-10;
[Link]((char)n+"\t"+chr);

6. A student is trying to convert the string present in x to a numerical value, so that he can find the square root
of the converted value. However the code has an error. Name the error (syntax / logical / runtime). Correct
the code so that it compiles and runs correctly. [2025 SP] [2M]
String x= "25";
int y=[Link](x);
double r=[Link](y);
[Link](r);

7. Primitive data types are built in data types which are a part of the wrapper classes. These wrapper classes
are encapsulated in the [Link] package. Non primitive datatypes like Scanner class are a part of the utility
package for which an object needs to be created. [2024 BP] [2M]
(a) To which package the Character and Boolean classes belong?
(b) Write the statement to access the Scanner class in the program.

8. What is wrapper class?

9. [Link]('Z' + 32); will display: [2025 B] [1M]

10.

ICSE CLASS X COMPUTER APPLICATIONS LIBRARY CLASS Page no 8

You might also like