You are on page 1of 28

Package

java.lang
Nguyen Viet Dung ITITIU14020
Tran Thach Tran ITITIU14119
Nguyen Minh Tri ITITIU14098
Pham Do Minh Quan
ITITIU14083
Pham Tuan Hai ITITIU13019

1. General

1.
General
The
Java package java.lang contains fundamental
classes and interfaces closely tied to the language and
runtime system.
This includes the root classes that form the class
system, types tied to the language definition, basic
exceptions, math functions, threading, security
functions, as well as some information on the underlying
native system.

2. The main classes and


interfaces in java.lang
2.1) Class Object
public class Object
ClassObjectis the root of the class hierarchy. Every class
hasObjectas a superclass. All objects, including arrays,
implement the methods of this class.
Since: JDK1.0

2.1) Class Object


METHOD DETAIL
*getClass
public finalClass<?>getClass()
Returns the runtime class of thisObject. The returnedClassobject is the object that is locked bystatic
synchronizedmethods of the represented class.
*clone
protectedObjectclone() throws CloneNotSupportedException
Creates and returns a copy of this object. The precise meaning of "copy" may depend on the class of
the object. The general intent is that, for any objectx, the expression:
x.clone() != x
will be true, and that the expression:
x.clone().getClass() == x.getClass()
will betrue, but these are not absolute requirements. While it is typically the case that:
x.clone().equals(x)
will betrue, this is not an absolute requirement.
*notify
Wakes up a single thread that is waiting on this object's monitor. If any threads are waiting on this
object, one of them is chosen to be awakened. The choice is arbitrary and occurs at the discretion of
the implementation. A thread waits on an object's monitor by calling one of thewaitmethods.

public boolean
equals(Object obj)
public String
toString()
public Object clone()
throws
CloneNotSuppotedExcep
protected
tion
void
finalize()
throws Throwable
public native int
hashCode() public Class
getClass()

Some more methods


involved with thread
activities collaboration
wait() and its overloaded
version
notify(), notifyAll()
All these methods got to
be part of each class and
hence were introduced
in the universal super
class following the need
originating from
Inheritiance
6

class
String s;
MyClass{
public MyClass(String s) {
this.s = s;
}
public void
throws finalize()
Throwable {
cleanupAllResources(); //
The method that cleans up
}
private
cleanupAllResources() {
void
// do some relavant
task
System.out.println("I
am
dying....
: says
"+s);
}
}

This method is
overridden to free up
extra resources when
they are no longer in use
and subject to Garbage
Collection once the
object is eligible to be
swallowed up by the
garbage collector
This works very much
These
like the
methods
destructors
is only
in C+
for
+ system purpose
and optimization and
its execution is subject
to GC, depending on
the VM and platform
and Execution
Environment and
timeline

class MC
{ int
marks;
String name;
publicMC(int marks, String
name)
{ super();
this.marks =
this.name = name;
marks;
}
void show()
{
System.out.println(name
+
" scored " +
marks);
}
this.marks
Void
setMarks(int
marks) {
=
}
marks;
}

class Main { public static void


main(String[]){
MC mc1=new MC(94,"Soham");
mc1.show();
MC mc2 = mc1;
mc2.show(); // mc2 an copy of mc1
mc2.setMarks(880);//change
mc2.show();// mc2 changed
mc1.show();// mc1 also!
}
}

class
MyClass{
}
public class Main1 {
public staticvoid
main(String[] args) {
MyClass obj=new MyClass();
System.out.println(obj.toString()
);
}
}

Output:MyClass@addbf1

class MyClass { public


String String(){
return "I am here";
}}
public class Main1 {
public static void main
(String[] args) {
MyClass obj = new
MyClass();
System.out.println(obj.to
String() ); } }
Output: I am here
1

2.2) Class Math


public final class Math extends Object
The classMathcontains methods for performing basic numeric operations
such as the elementary exponential, logarithm, square root, and trigonometric
functions.

public class MathLibraryExample {


public static void main(String[] args) {
int i = 7;
int j = -9;
double x = 72.3;
double y = 0.34;
System.out.println("i is " + i);
System.out.println("j is " + j);
System.out.println("x is " + x);
System.out.println("y is " + y);
the absolute value of a number is equal to the
number if the number is positive or zero and
equal to the negative of the number if the
number is nefative.
System.out.println("|" + i + "| is " +
Math.abs(i));
System.out.println("|" + j + "| is " +
Math.abs(j));
System.out.println("|" + x + "| is " +
Math.abs(x));
System.out.println("|" + y + "| is " +
Math.abs(y));
System.out.println(x + " is approximately "
+ Math.round(x));
System.out.println(y + " is approximately "
+ Math.round(y));

The "ceiling" of a number is the smallest integer


greater than or equal to the number. Every integer is
its own ceiling

System.out.println("The ceiling of " + i + " is " +


Math.ceil(i));
System.out.println("The ceiling of " + j + " is "
+ Math.ceil(j));
System.out.println("The ceiling of " + x + " is "
+ Math.ceil(x));
System.out.println("The ceiling of " + y + " is "
+ Math.ceil(y));
The "floor" of a number is the largest integer less
than or equal to the number. Every integer is its own
floor.

System.out.println("The
Math.floor(i));
System.out.println("The
Math.floor(j));
System.out.println("The
Math.floor(x));
System.out.println("The
Math.floor(y));

floor of " + i + " is " +


floor of " + j + " is " +
floor of " + x + " is " +
floor of " + y + " is " +

min() returns the smaller of the two arguments


you pass it
System.out.println("min(" + i + "," + j + ") is "
+ Math.min(i,j));
System.out.println("min(" + x + "," + y + ") is "
+Math.min(x,y));
System.out.println("min(" + i + "," + x + ") is "
+ Math.min(i,x));
System.out.println("min(" + y + "," + j + ") is "
+ Math.min(y,j));

There's a corresponding max()


method that returns the larger of two
numbers
System.out.println("max(" + i + "," + j + ")
is " + Math.max(i,j));
System.out.println("max(" + x + "," + y
+ ") is " + Math.max(x,y));
System.out.println("max(" + i + "," + x
+ ") is " + Math.max(i,x));
System.out.println("max(" + y + "," + j
+ ") is " + Math.max(y,j));

System.out.println("Pi is " + Math.PI);


System.out.println("e is " + Math.E);
Trigonometric methods .All arguments
are given in radians
double angle = 45.0 * 2.0 *
Math.PI/360.0;
System.out.println("cos(" + angle + ") is
" + Math.cos(angle));
System.out.println("sin(" + angle + ") is
" + Math.sin(angle));
double value = 0.707;

System.out.println("acos(" + value + ")


is " + Math.acos(value));
System.out.println("asin(" + value + ")
is " + Math.asin(value));
System.out.println("atan(" + value + ")
is " + Math.atan(value));

Exponential and Logarithmic


Methods
System.out.println("exp(1.0) is " + Math.exp(1.0));
System.out.println("exp(10.0) is " + Math.exp(10.0));
System.out.println("exp(0.0) is " + Math.exp(0.0));
log(a) returns the natural logarithm (base e) of a.

System.out.println("log(1.0) is " + Math.log(1.0));


System.out.println("log(10.0) is " + Math.log(10.0));
System.out.println("log(Math.E) is " + Math.log(Math.E));
pow(x, y) returns the x raised to the y th power.
System.out.println("pow(2.0, 2.0) is " +
Math.pow(2.0,2.0));
System.out.println("pow(10.0, 3.5) is " +
Math.pow(10.0,3.5));
System.out.println("pow(8, -1) is " + Math.pow(8,-1));

sqrt(x) returns the square root of x.


for (i=0; i < 10; i++) {

System.out.println(
the square root of " + i + " is " + Math.sqrt(i)); }
System.out.println("Here's one random number: " +
Math.random());
System.out.println("Here's another random number:
" + Math.random());
Finally there's one Random method that returns a
pseudo-random number between 0.0 and 1.0;

2.3) Class Compiler


public final class Compiler
extends Object
TheCompilerclass is provided to support Java-to-nativecode compilers and related services. By design,
theCompilerclass does nothing; it serves as a placeholder for
a JIT compiler implementation.
If no compiler is available, these methods do nothing.
Since: JDK1.0

2.3) Class Compiler


METHOD DETAIL

* command
public staticObjectcommand(Objectany)
Examines the argument type and its fields and perform some
documented operation. No specific operations are required.
*Enable
public staticvoidenable()
Cause the Compiler to resume operation.
*Disable
public staticvoiddisable()
Cause the Compiler to cease operation.

2. The main classes and interfaces


in 2.3)
java.lang
Class Compiler

2.4) Class String


public final class String extends Object
implements Serializable, Comparable <String>, CharSequence
TheStringclass represents character strings. All string literals in Java
programs, such as"abc", are implemented as instances of this class.
Strings are constant; their values cannot be changed after they are
created. String buffers support mutable strings. Because String objects are
immutable they can be shared.
For example:
String str = "abc";
is equivalent to:
char data[] = {'a', 'b', 'c'};
String str = new String(data);

2.4) Class String


For example:

String str = "abc";


is equivalent to:

char data[] = {'a', 'b', 'c'}; String str = new String(data);


Here are some more examples of how strings can be used:

System.out.println(abc);
String cde = cde;
System.out.println(abc + cde);
String c = abc.substring(2,3);
String d = cde.substring(1,2);

2.5) Class StringBuilder


A mutable sequence of characters. This class provides an API
compatible withStringBuffer, but with no guarantee of
synchronization. This class is designed for use as a drop-in
replacement forStringBufferin places where the string buffer
was being used by a single thread (as is generally the case).

2. The main classes and interfaces


in java.lang
2.5) Class StringBuilder
Example:
class HIHI{
public static void main(String args[]){
StringBuilder sb=new StringBuilder("Hello ");
sb.append("Java");//the first String was changed
System.out.println(sb) ;//print the result is Hello Java }}
class HIHI{ public static void main(String args[]){
StringBuilder sb=new StringBuilder("Hello ");
sb.insert(1,"Java");//the first String was changed
System.out.println(sb);//print the result is Hello Java } }

2.6) Class Number


public abstract class Number
extends Object
implements Serializable
The abstract class Number is the superclass of classes BigDecimal, BigInteger,
Byte, Double, Float, Integer, Long, and Short.
Subclasses of Number must provide methods to convert the represented
numeric value to byte, double, float, int, long, and short

public class Test{


public static void main(String args[]){
Integer x = 5;
// unbox int to object Integer
x= x+10; // Open box Integer to int
System.out.println(x);
}
}

2.6) Class Number


METHOD DETAIL
*intValue
public abstractintintValue()
Returns the value of the specified number as anint. This may
involve rounding or truncation
*shortValue
publicshortshortValue()
Returns the value of the specified number as ashort. This may
involve rounding or truncation.

2.7) Interface Comparable<T>


public interface Comparable<T>
T- the type of objects that this object may be compared to
This interface imposes a total ordering on the objects of each
class that implements it. This ordering is referred to as the
class'snatural ordering, and the class'scompareTomethod is
referred to as itsnatural comparison method.

2.7) Interface Comparable<T>


METHOD DETAIL
*compareTo
intcompareTo(To)
Compares this object with the specified object for order.
Returns a negative integer, zero, or a positive integer as this
object is less than, equal to, or greater than the specified
object.

1
public class Product{

private int code;


private String name;
private double price;

public Product(int code, String name, double price) {


this.code = code;
this.name = name;
this.price = price;
}

public int getCode() {


return code;
}

public void setCode(int code) {


this.code = code;
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public double getPrice() {


return price;
}

public void setPrice(double price) {


this.price = price;
}

@Override
public String toString() {
return &quot;Product{ &quot; + code + &quot;, &quot; + name + &quot;, &quot; + price + '}';
}
}

public class Product implements Com parable& lt;Product& gt; {


//....
@ O verride
public int com pareTo(Product o) {
//this case w e com pare product by price

//this product and o are the sam e


if (this.price = = o.price) {
return 0;
}
//this product is greater than o
if (this.price & gt; o.price) {
return 1;
}
//this product is less than o
return -1;
}
}

0 if product1 and product2 equal.


1 if product1 lager than product2.
-1 if product1 less than product2.

public static void main(String[] args) {


//Instantiate an object of list product
List&lt;Product&gt; products = new LinkedList&lt;Product&gt;();

//Prepare mock data to test


products.add(new Product(1, &quot;TV&quot;, 300));
products.add(new Product(2, &quot;Iphone&quot;, 600));
products.add(new Product(3, &quot;Sony Vaio E&quot;, 200));
products.add(new Product(4, &quot;Asus E&quot;, 800));

//Sort list
java.util.Collections.sort(products);

oduct{code=1,
price=300.0}Product{code=2, name=Iphone,
//print sortedname=TV,
list
for (Product p : products) {
System.out.println(p);
}
}

price=

RESULT
Product{code=3, name=Sony Vaio E, price=200.0}
300.0}Product{code=2,
name=Iphone,
price=600.0}Product{code=4, name=As
Product{code=1, name=TV,
price=300.0}
Product{code=2, name=Iphone, price=600.0}
Product{code=4, name=Asus E, price=800.0}

THANKS FOR YOUR


ATTENTION

You might also like