Ch-10 Packages in Java
Ch-10 Packages in Java
Packages in Java
A package is a group of logically related classes and interfaces of Java that are included in an
application program. Packages are of two types:
i) User defined package
ii) Pre defined packages (Java API) like java.io, java.util, java.lang, java.applet, etc.
The package statement in a program groups all the following classes into a user defined
package. For e.g.
package MyPackage ;
The import statement makes a class or classes of a package available to the current program.
For e.g. import java. util. Scanner; import java . io. * ;
Streams: Java performs I/O through streams. Commonly used I/O streams present in java.io
are:
i) System.in – object of InputStream for input purpose.
ii) System.out – object of OutputStream for printing purpose.
iii) System.err – object of OutputStream for error messages.
Pre – Defined Packages:
1) java. io package: It contains a large number of stream classes used to read and write
data to a file or I/O devices.
Stream classes are of two types:
i) Byte Streams: They are used for byte-oriented I/O. For e.g. DataInputStream,
DataOutputStream. They can be used as:
DataInputStream dis = new DataInputStream (System.in);
ii) Character Streams: They are used for character-oriented I/O For e.g. BufferedReader,
BufferedWriter. They can be used as:
BufferedReader br = new BufferedReader(new InputStreamReader (System.in));
Where, br is any character stream object linked to the keyboard and InputStreamReader class
converts bytes to characters.
CN XI – SR Page 1
2) java.lang package: It is the default package which gets included at the start of any Java
program. It stores the basic functions of the language. Commonly used classes in java.lang are:
a) Wrapper Classes:
i) They are a collection of classes that provide a mechanism to "wrap" primitive types into an
object. Java has a wrapper class for each of the eight primitive data types:
The wrapper object can be created by instantiating the wrapper class with the new operator.
For example,
Character c = new Character ('T'); // creates a Character object with primitive value 'T'
Integer y = new Integer (33); // creates a Integer object with primitive value 33
Common Wrapper Methods:
i) Wrapper.valueOf ( ) – takes a value (or string) as an argument and returns a newly created
wrapped object of the class that invoked the method. For example:
Character c = Character.valueOf („a‟); // assigns 'a' to the Character object a
Float f = Float.valueOf (“3.14f”); // assigns 3.14 to the Float object f
ii) typeValue ( ) – such as intValue( ), doubleValue( ), charValue( ), etc. Each one of them
returns a primitive type equal to value of the wrapper object. For example:
int x = 25 ;
Integer oby = new Integer (42);
System.out.println(x + oby.intValue ( )); // 67
System.out.println(oby.doubleValue ( )); // 42.0
CN XI – SR Page 2
iii) parseType (String str) – such as parseInt( ), parseDouble( ), parseFloat( ), etc. Each of them
returns a primitive type equivalent of the number contained in the String argument. For
example,
int x = Integer.parseInt ("1234") ; // converts a string integer into an int
System.out.println(Integer.parseInt (“234”) + „A‟) ; // 299
iv) Wrapper.toString( ) – such as Integer.toString( ), Double.toString( ), etc. Each of them
accepts a primitive type argument and returns its equivalent String representation. For e.g.
String s = Integer.toString (1234); // converts an int into a String object "1234"
System.out.println(“d = “ + Double.toString (3.14)) ; // result is “d = 3.14”
System.out.println(Integer.toString(234) + „0'); // 2340
Boxing: is the process of converting a primitive data type into its corresponding wrapper class
object. Boxing takes place when a primitive value is:
(i) Passed as a parameter to a method that uses a corresponding wrapper class object as
a formal function argument.
(ii) Assigned to a variable of the corresponding wrapper class (autoboxing).
For example:
Integer obj1 = Integer.valueOf (10); // Explicit boxing
Character obj2 = new Character („T‟); // Boxing – creates a Character object
char ch = „T‟;
Character obj3 = new Character (ch); // Explicit boxing
Autoboxing:
The automatic conversion of primitive data type into an object of its corresponding wrapper
class by the Java compiler is called autoboxing. For example:
char ch = „T‟;
Character obj1 = ch; // Autoboxing of character
Character ob2 = „a‟; // Character autoboxing
Integer obj3 = 10; // Autoboxing of integer
CN XI – SR Page 3
Unboxing: is the process of converting a wrapper class object into its corresponding primitive
data type. Unboxing takes place when a wrapper class object is:
(i) Passed as a parameter to a method that uses a value of the corresponding primitive
type as a formal function argument.
(ii) Assigned to a variable of the corresponding primitive type (autounboxing).
For example:
Integer obj = new Integer (10); // Boxing
int ival = obj.intValue( ); // Explicit unboxing
Autounboxing:
The automatic conversion of an object of a wrapper class into its corresponding primitive data
type by the Java compiler is called autounboxing. For example:
Integer obj = Integer.valueOf (10); // Boxing
int ival = obj; // Autounboxing
Method Description
Returns true, if ch is a letter, otherwise it
boolean isLetter(char ch)
returns false.
Returns true, if ch is a digit, otherwise it
boolean isDigit(char ch)
returns false.
Returns true, if ch is a letter or a digit,
boolean isLetterOrDigit(char ch)
otherwise it returns false.
Returns true, if ch is a lowercase letter,
boolean isLowerCase(char ch)
otherwise it returns false.
Returns true, if ch is a uppercase letter,
boolean isUpperCase(char ch)
otherwise it returns false.
Returns true, if ch is whitespace, other wise
boolean isWhitespace(char ch)
it returns false.
char toLowerCase(char ch) Returns lowercase equivalent of ch
char toUpperCase(char ch) Returns uppercase equivalent of ch
CN XI – SR Page 4
class abc {
public static void main( ) {
char ch[ ] = {„R‟, „0‟, '\t'} ;
System.out.println (Character.isLetter(ch[0]);
System.out.println (Character.isDigit(ch[1]);
System.out.println (Character.isUpperCase(ch[0]);
System.out.println (Character.toLowerCase(ch[0]);
System.out.println (Character.isLetterOrDigit(„?‟));
System.out.println (Character.isWhitespace(ch[2]));
}
}
c) The Math class: contains a library of mathematical methods or functions.
CN XI – SR Page 5
d) String and StringBuffer classes: used to represent Strings in Java.
3) java.util package: It is a collection of utility classes.
a. Date – To create and manipulate calendar dates in system independent fashion.
b. StringTokenizer – Converts a string of text into tokens or words.
c. Scanner – It is used for text input processing from a keyboard, a file or a String. For e.g.
Scanner sc = new Scanner (System.in); // sc is a Scanner class object
d. Random – Implements a random number generator.
CN XI – SR Page 6