Post on 10-Apr-2018
8/8/2019 8 Java Lang Package
http://slidepdf.com/reader/full/8-java-lang-package 1/22
By Waqas 1
8/8/2019 8 Java Lang Package
http://slidepdf.com/reader/full/8-java-lang-package 2/22
By Waqas 2
8/8/2019 8 Java Lang Package
http://slidepdf.com/reader/full/8-java-lang-package 3/22
By Waqas 3
Language Package (lang)
The java.lang package contains classes that are
central to the operation of the java language
and environment. For example: very little can
be done without String class and the Objectclass is completely indispensable.
Due to this importance, java compiler
automatically import all the classes in the package into every source file.
8/8/2019 8 Java Lang Package
http://slidepdf.com/reader/full/8-java-lang-package 4/22
By Waqas 4
Object Class
Object class is the super class of all javaclasses. If a class does not extends any class,
the compiler build a class that directly inherit
from Object.All the methods of Object class inherited by
all the classes. Two important methods are:
public boolean equals(Object o)
public String toString( )
8/8/2019 8 Java Lang Package
http://slidepdf.com/reader/full/8-java-lang-package 5/22
By Waqas 5
equals(Object o) compares the reference
of the calling object with the o object. If they refer to the same object the method
return true otherwise returns false.
This method is overridden by String,Date and Wrapper Classes. In those
classes it compare the contents of the
objects rather than their memoryreference.
8/8/2019 8 Java Lang Package
http://slidepdf.com/reader/full/8-java-lang-package 6/22
By Waqas 6
toString( ) method provides a string
representation of an object¶s state. The
method provided by Object class returnsclass name followed by hash code. The
classes should override this method to
display class specific information.When we print any object the compiler
calls toString( ) method to display the
string representation of the object.
8/8/2019 8 Java Lang Package
http://slidepdf.com/reader/full/8-java-lang-package 7/22
By Waqas 7
Math Class
Math class is final class so you can notextends it. Constructors are private so you can
not create object of it.
All the methods in this class are static and can be accessed with class name.
The methods in Math class cover a broad
range of mathematical functions including
trigonometry, logarithms and exponentiationand rounding.
8/8/2019 8 Java Lang Package
http://slidepdf.com/reader/full/8-java-lang-package 8/22
By Waqas 8
Wrapper Classes
Each java primitive data type has acorresponding wrapper class. A wrapper class
is simply a class that contain a single,
immutable value.All the wrapper classes can be constructed by
passing the value to be wrapped into the
constructor.
The wrapper classes are useful whenever itwould be convenient to treat a piece of
primitive data type as if it were an object.
8/8/2019 8 Java Lang Package
http://slidepdf.com/reader/full/8-java-lang-package 9/22
By Waqas 9
Wrapped values inside wrapper classes cannot
be modified.
Following is the list of wrapper classes:
Byte byte
Character char
Shortshort
Boolean boolean
Integer int
Wrapper ClassPrimitive Data Type
8/8/2019 8 Java Lang Package
http://slidepdf.com/reader/full/8-java-lang-package 10/22
By Waqas 10
Longlong
Floatfloat
Doubledouble
The values wrapped inside two
wrappers of the same type can be
checked for equality by using equals( )
method, which are overridden by all
wrapper classes.
8/8/2019 8 Java Lang Package
http://slidepdf.com/reader/full/8-java-lang-package 11/22
By Waqas 11
Some examples of wrapper classes:
int a = 5;
Integer a1 = new Integer(a);
short s = 43;
Short s1 = new Short(s);
char c = µA¶;Character c1 = new Character(c);
8/8/2019 8 Java Lang Package
http://slidepdf.com/reader/full/8-java-lang-package 12/22
By Waqas 12
boolean b = true;
Boolean b = new Boolean(b);
double d = 54.43;
Double d1 = new Double(d);
Integer i = new Integer(4);
Double d = new Double(33.33);
Character c = new Character(µA¶);
8/8/2019 8 Java Lang Package
http://slidepdf.com/reader/full/8-java-lang-package 13/22
By Waqas 13
String Class
The String class contain an immutable string.Once an instance of string class is created, the
string it contains cannot be changed.
There are ten constructors in the string class
which allow you to create string out of many
forms like character arrays or another string
etc.
Note that java support 16 bit Unicodecharacter set.
8/8/2019 8 Java Lang Package
http://slidepdf.com/reader/full/8-java-lang-package 14/22
By Waqas 14
The most common constructor is:
String s = new String(³Hello´);
can be abbreviated as:
String s = ³Hello´;
Both create a object of String class
8/8/2019 8 Java Lang Package
http://slidepdf.com/reader/full/8-java-lang-package 15/22
By Waqas 15
We can compare two string object contents
using equals( ) method.
String s1 = ³Hello´;
String s2 = ³Hell0´;
boolean b = s1.equals(s2);
System.out.println(b);
In this case true is printed on the screen.
8/8/2019 8 Java Lang Package
http://slidepdf.com/reader/full/8-java-lang-package 16/22
By Waqas 16
String class contain several methods to
perform transformations on Strings.
Some methods are:
toUpperCase( );
toLowerCase( );length( );
substring( );
indexOf( );
charAt( );
etc.
8/8/2019 8 Java Lang Package
http://slidepdf.com/reader/full/8-java-lang-package 17/22
By Waqas 17
String class overload + operator to perform
concatenation of two strings and other objects.
String s1 = ³Hello´;
String s2 = ³World´;
Int i = 43;
String s = s1 + s2 + i;
Now s contains : HelloWorld43;
8/8/2019 8 Java Lang Package
http://slidepdf.com/reader/full/8-java-lang-package 18/22
By Waqas 18
StringBuffer Class
The instance of StringBuffer class represents astring that can be dynamically modified.
The most commonly used constructor takes a
String instance.
StringBuffer sb = new StringBuffer(³Hello´);
StringBuffer has several methods which areused to dynamically change string.
8/8/2019 8 Java Lang Package
http://slidepdf.com/reader/full/8-java-lang-package 19/22
By Waqas 19
append( String s) method append the s to the
end of the calling StringBuffer object.
StringBuffer sb = new StringBuffer(³Hello´);
String s = ³World´;
sb.append(s);
Now sb represents:
HelloWorld
8/8/2019 8 Java Lang Package
http://slidepdf.com/reader/full/8-java-lang-package 20/22
By Waqas 20
insert( int index, String s) method insert the s
at the position of index number specified of the
calling StringBuffer object.
StringBuffer sb = new StringBuffer(³Hello´);
String s = ³World´;
sb.insert(2, s);
Now sb represents:
HeWorldllo
8/8/2019 8 Java Lang Package
http://slidepdf.com/reader/full/8-java-lang-package 21/22
By Waqas 21
reverse( ) method return the string in reverse
order.
StringBuffer sb = new StringBuffer(³Hello´);
sb.reverse( );
Now sb represents:
olleH
8/8/2019 8 Java Lang Package
http://slidepdf.com/reader/full/8-java-lang-package 22/22
By Waqas 22