Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Ch-10 Packages in Java

The document discusses Java packages, which are groups of related classes and interfaces, and categorizes them into user-defined and pre-defined packages. It details the use of various Java I/O streams, wrapper classes, and methods for boxing and unboxing primitive types. Additionally, it covers commonly used classes in the java.lang and java.util packages, including the Math class and utilities for string manipulation.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Ch-10 Packages in Java

The document discusses Java packages, which are groups of related classes and interfaces, and categorizes them into user-defined and pre-defined packages. It details the use of various Java I/O streams, wrapper classes, and methods for boxing and unboxing primitive types. Additionally, it covers commonly used classes in the java.lang and java.util packages, including the Math class and utilities for string manipulation.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Using Library Classes (Chap – 10)

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:

Primitive type Wrapper class


boolean Boolean
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double

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

b) The Character class: provides methods to manipulate and process characters.

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.

Method Return type Example

Math.min(arg1,arg2)or int n = Math.min(4,6); // n = 4


int/long/float/double
Math.max (arg1,arg2 ) S.o.pln(Math.max(-4.5,2.5); // 2.5

Math.pow(num,base) double S.o.pln(Math.pow(16,0.5); // 4.0

Math.sqrt(arg ) double double n = Math.sqrt(4.0); // n =2.0

Math.abs(arg ) int/long/float/double S.o.pln(Math.abs(-29.6); // 29.6

Math.round(arg ) int int n = Math.round(6.25); // n = 6

Math.floor(arg ) double S.o.pln(Math.floor(-6.25); // -7.0

Math.ceil(arg ) double S.o.pln(Math.ceil(-6.25); // -6.0

Math.rint(arg ) double S.o.pln(Math.rint(29.6); // 30.0

returns a fractional value b/w 0 and 1


Math.random( ) double
double d = Math.random( );

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

You might also like