0% found this document useful (0 votes)
11 views

10 05 Libraryclasses

Uploaded by

takshanamirtha29
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)
11 views

10 05 Libraryclasses

Uploaded by

takshanamirtha29
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
You are on page 1/ 6

Class X_Chapter 5

Library Classes
Definition / Description
The Pre-defined classes are known as library classes. It is used to reduce programming
effort of programmers. These are used as data types. E.g.: String, Scanner, Math etc.
Two Kinds of Data Types
The two kinds of data types in Java are Primitive and Reference (Non-primitive).
The primitive data types are: byte, short, int, long, float, double, boolean and char. A
primitive variable is single memory cell and it can contain only one value at a time.
The reference data types are: classes, arrays and interfaces. A reference data type variable
(object or array) is composed of multiple memory cells and it can contain multiple values. So
it’s a composite data type.
Two Kinds of Classes
Two kinds of classes are: Class as data types and Class as application.
Class as Data Types
All pre-defined classes are used as data types in user-defined classes.
E.g.: String, Scanner etc.
The user-defined classes that are as data types are user-defined data types. These
classes are composite data types, it means we can include many data type of variables and
methods in these classes.
E.g.:
class Student
{
int roll; String name; double mark;
public void compute()
{ ; ; ;}
}
Class as Application
Class with main() method is an application class. It is the class that we execute. This class
applies (executes) the data type classes by creating objects of the data type classes and
calling methods of them.
E.g.:
public class School
{
public static void main()
{
Student ob=new Student();
ob.compute();
}
}
Note: Data type classes have no independent use. These are used in application classes.
ICSE X 2 Libray Classes
Wrapper Classes
Definition
Wrapper classes are predefined classes that consist methods to convert reference
values like string values to primitive values (e.g.: int, double etc.) and vice versa.
E.g.: Integer, Double, Character etc.

Wrapper classes are packed (included) in java.lang package.

List of Primitives, Wrapper Classes and Parse Methods


Primitive Classes Methods
byte Byte parseByte(), valueOf(), toString()
short Short parseShort(), valueOf(), toString()
int Integer parseInt(), valueOf(), toString()
long Long parseLong(), valueOf(), toString()
float Float parseFloat(), valueOf(), toString()
double Double parseDouble(), valueOf(), toString()
boolean Boolean parseBoolean(), valueOf(), toString()
char Character (Covered in next section)
Note: There is no parse method and toString method in Character class
Wrapper Class Methods
Wrapper class methods are used to convert the reference value (e.g.: String) to primitive
values (e.g.: int, double) and vice versa.
E.g.: Integer.parseInt(), Double.parseDouble(), Integer.toString(), Double.toString(),
Integer.valueOf(), Double.valueOf() etc.
An Example Program: To read and display a name, standard, division and mark.
Note: To input values to a program using wrapper class methods it requires
BufferedReader, InputStreamReader, IOException classes, and its methods
readLine() and read(). These classes are reside in java.io package.
import java.io.*;
public class Example1
{
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter name: ");
String name=br.readLine();
System.out.print("Enter standard: ");
int std=Integer.parseInt(br.readLine()); “125”
System.out.print("Enter division: ");
char div=(char)br.read();
System.out.print("Enter mark: ");
double mark=Double.parseDouble(br.readLine()); “95.5”
System.out.println("Your Details:");
System.out.println("Name : "+name);
System.out.println("Class : "+std+“ ”+div);
System.out.println("Mark : "+mark);
}
}
ICSE X 3 Libray Classes
Description
import java.io.*;
The java.io is a package which contains the pre-defined classes BufferedReader,
InputStreamReader and IOException. The * symbol represents all classes in a package.
The import keyword connects these classes to our program. Package is a group of related
pre-defined classes.
throws IOException
The throws is a keyword. The IOException is a pre-defined class. If an in input error happen
the throws keyword inform the compiler using IOException class (exception means input error).
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
This statement creates an object named br. The br is a user-defined object of
BufferedReader class.
br.readLine();
The br is a user-defined object of BufferedReader class. The readLine() is a pre-defined
method of BufferedReader class. The . operator is used to link the br and the readLine().
Integer.parseInt(br.readLine());
This statement is used to read an int type value from the keyboard during program run. The
Integer.parseInt() is full name of a wrapper class method. The Integer is a wrapper class and
parseInt() is its individual method name. Explanation is given as Notes.
Double.parseDouble(br.readLine());
This statement is used to read a double type value from the keyboard during program run.
The Double.parseDouble() is a full name of a wrapper class method. The Double is a
wrapper class and parseDouble() is its individual method name.
(char)br.read();
This statement is used to read a char type value from the keyboard during program run.
The br.read() reads the ASCII value of a character from the keyboard. When we type A, it is
read as 65. The (char) converted the 65 into A which is a char type value.
Notes:
When we input an integer 15 to the statement Integer.parseInt(br.readLine()) the
br.readLine() reads it as string 15. The Integer.parseInt() converts the string 15 into int 15.
Write Output:
System.out.println("15"+2);
System.out.println(Integer.parseInt("15")+2);
Answer:
152
17
Write Output:
System.out.println("1.5"+2);
System.out.println(Double.parseDouble("1.5")+2);
Answer:
1.52
3.5
ICSE X 4 Libray Classes
The valueOf() Methods of Wrapper Classes
Integer.valueOf(): It converts string representation of an int value to int data type.
E.g.: int n=Integer.valueOf("25")+4 is 29 where "25"+4 is 254
It is equivalent to Integer.parseInt().
Double.valueOf(): It converts string representation of a double value to double data type.
E.g.: double n=Double.valueOf("20.5")+4 is 24.5 where "20.5"+4 is 20.54
It is equivalent to Double.parseDouble()

The toString() Methods of Wrapper Classes


Integer.toString(): It converts an int value to its string representation.
E.g.: String n=Integer.toString(25)+4 is 254 where 25+4 is 29
Double.toString(): It converts a double value to its string representation.
E.g.: String n=Double.toString(20.5)+4 is 20.54 where 20.5+4 is 24.5

Methods of Character Class


Checking Methods
1. boolean isLetter(char ch)
It checks whether the argument character is a letter or not. It returns boolean value true or
false.
E.g.:
System.out.println(Character.isLetter(‘A’));
System.out.println(Character.isLetter(‘a’));
System.out.println(Character.isLetter(‘3’));
Output:
true
true
false
2. boolean isDigit(char ch)
It checks whether the argument character is a digit or not. It returns boolean value true or
false.
E.g.:
System.out.println(Character.isDigit(‘5’));
System.out.println(Character.isDigit(‘A’));
Output:
true
false
3. boolean isLetterOrDigit(char ch)
It checks whether the argument character is a letter or digit or not. It returns boolean value
true if the character is either letter or digit, for other characters it returns false.
E.g.:
System.out.println(Character.isLetterOrDigit(‘A’));
System.out.println(Character.isLetterOrDigit(‘3’));
System.out.println(Character.isLetterOrDigit(‘+’));
Output:
true
true
false
ICSE X 5 Libray Classes
4. boolean isLowerCase(char ch)
It checks whether the argument character is a small letter or not. It returns boolean value
true or false. E.g.:
System.out.println(Character.isLowerCase(‘a’));
System.out.println(Character.isLowerCase(‘A’));
Output:
true
false
5. boolean isUpperCase(char ch)
It checks whether the argument character is a capital letter or not. It returns boolean value
true or false. E.g.:
System.out.println(Character.isUpperCase(‘A’));
System.out.println(Character.isUpperCase(‘a’));
Output:
true
false
6. boolean isWhitespace(char ch)
It checks whether the argument character is a normal space, tab space or Enter space or
not. It returns boolean value true or false. E.g.:
System.out.println(Character.isWhitespace(‘ ’));
System.out.println(Character.isWhitespace(‘\t’));
System.out.println(Character.isWhitespace(‘\n’));
System.out.println(Character.isWhitespace(‘n’));
Output:
true
true
true
false

Conversion Methods
1. char toLowerCase(char ch)
It converts the argument character to lower case. It returns char type character. E.g.:
System.out.println(Character.toLowerCase(‘N’));
System.out.println(Character.toLowerCase(‘a’));
System.out.println(Character.toLowerCase(‘3’));
Output:
n
a
3
2. char toUpperCase(char ch)
It converts the argument character to upper case. It returns char type character. E.g.:
System.out.println(Character.toUpperCase(‘a’));
System.out.println(Character.toUpperCase(‘A’));
System.out.println(Character.toUpperCase(‘3’));
Output:
A
A
3
ICSE X 6 Libray Classes
Program Using Character Class Methods
1. Input a character. Check whether it is alphabet or digit or space or special character.
import java.io.*;
public class Charater
{
public static void main()throws IOException
{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a character");
char c=(char)br.read();
if(Character.isLetter(c))
System.out.println("Alphabet");
else if(Character.isDigit(c))
System.out.println("Digit");
else if(Character.isWhitespace(c))
System.out.println("Space");
else
System.out.println("Special Character");
}
}
2. Input an alphabet. If it is capital letter convert into small letter. If small letter convert
to capital. Print it.
import java.io.*;
public class Charater
{
public static void main()throws IOException
{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a character");
char c=(char)br.read();
if(Character.isUpperCase(c))
c=Character.toLowerCase(c);
else if(Character.isLowerCase(c))
c=Character.toUpperCase(c);
System.out.println("The converted character: "+c);
}
}
Autoboxing and Unboxing
Boxing
Converting a primitive value to reference value and store into an object of a wrapper class by
creating an object using new keyword is called boxing. For example, converting and storing
an int value to an Integer class object.
E.g.: Integer x = new Integer(5); The int type value 5 is stored into the object x by creating it
using new keyword.
Autoboxing
Converting a primitive value to reference value and store into an object of a wrapper class
directly is called autoboxing. For example, converting and storing an int value to an Integer
class object.
E.g.: Integer x = 5; The x is an object of Integer class. Into the x the int type value 5 is stored.
Unboxing
Converting a reference value in an object of a wrapper class to a primitive value and store
into a primitive variable is called unboxing. For example conversion and storing of reference
value in an Integer class object to an int variable.
E.g.: int b = x; The reference value in the object x is stored into the primitive variable b.

You might also like