10 05 Libraryclasses
10 05 Libraryclasses
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.
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.