presentation on java.lang package

20
E X P L O R I N G JAVA.LANG A tour of new features By: Saba Chaudhary

Upload: saba-chaudhary

Post on 18-Apr-2015

16 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Presentation on java.lang package

E X P L O R I N G JAVA.LANG

A tour of new features By: Saba Chaudhary

Page 2: Presentation on java.lang package

java.lang Overview

•Object

•Math

•The wrapper classes

•Thread, ThreadGroup and Runnable

•String

•StringBuffer

•Class

•Class Loader

•Interfaces CharSequence Interface Comparable Interface

Page 3: Presentation on java.lang package

THE JAVA.LANG PACKAGE CONTAINS CLASSES THAT ARE MOST CENTRAL TO THE JAVA LANGUAGE.

java.lang: Class Hierarc

hy

Page 4: Presentation on java.lang package

Java.lang (Non-Objectives)

The Cloneable interface defines no members. It is used to indicate that a class allows a bitwise copy of an object(i.e, a clone) to be made.If you try to call clone() on a class that doesn’t implement Cloneable,a CloneNotSuppprtedException is thrown.Const. for an obj being cloned is not called.

The security manager is a class that your classes can subclass to to create a security manager.

The class Exception and its subclasses are a form of Throwable that indicates conditions that a reasonable application might want to catch.

Security Manager

Clone() & Cloneable Interface

Exceptions

Page 5: Presentation on java.lang package

java.lang (Non-Objectives)

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions

The class ClassLoader is an abstract class. Applications can create subclasses that extend ClassLoader in order to load classes in some way other than the way they are normally loaded by the Java run-time System .

Errors

ClassLoader

Page 6: Presentation on java.lang package

java.lang.Object Class Object is the root of the class hierarchy.

•Every class has Object as a superclass.

All objects, including arrays, implement the methods of this class.

If a class doesn’t extend another class, then compiler extends it from Object.

For instance:

public class MyName { // stuff }

is created by the compiler as:

public class MyName extends Object{ // stuff}

Methods provided by Object class:

clone() equals() finalize()

Page 7: Presentation on java.lang package

JAVA.LANG.OBJECT METHODS•Thread control

- wait(), notify(), notifyAll()•General

- equals(), toString()•Not tested

-finalize(), hashCode(), clone(), getClass()•public boolean equals( Object object )

- should be overrided- provides “deep” comparison- not the same as ==!

== checks to see if the two objects are actually the same objectequals() compares the relevant instance variables of the two objects

•public String toString()should be overridedreturns a textual representation of the objectuseful for debugging

Page 8: Presentation on java.lang package

‘class’

Class encapsulates the run-time state of an object/interface. Objects of Class type are created automatically when classes are loaded.The getClass() mathod is used to obtain a class object.Class is a generic type declared as –

class Class<T>T is the class/interface represented.

Commonly used methods defined by Class :forName() getClass()getSuperClass()getClassLoader() and many more..

UsageClass<?> clobj;Clobj =x.getClass() ; //get Class referenceSOP(“x is object of type: ”+clobj.getName());

Page 9: Presentation on java.lang package

‘classloader’ This class is the abstract superclass of objects that

know how to load Java classes into a Java VM. Given a ClassLoader object, you can dynamically

load a class by calling the public loadClass() method, specifying the full name of the desired class. You can obtain a resource associated with a class by calling getResource(), getResources(), and getResourceAsStream().

Many applications do not need to use ClassLoader directly; these applications use the Class.forName() and Class.getResource() methods to dynamically load classes and resources using the ClassLoader object that loaded the application itself.

Page 10: Presentation on java.lang package

It contains all the floating point fxn used for geometry

& trigo. Math defines 2 double constants: E(2.72)

and PI(3.14 ) approx.Categories are:

1.Transcendental 2.Exponential 3.Rounding 4.Miscellaneous

Math Class

Page 11: Presentation on java.lang package

Transcendental FunctionsThe following methods accept a double parameter for an angle in radians and return theresult of their respective transcendental function:static double sin(double arg) Returns the sine of the angle specified by arg in radians.cos(),tan(),asin(),acos(),atan()static double atan2(double x, double y)returns the angle whose tangent is x/y.sinh(),cosh(),tanh()

Exponential FunctionsMath defines the following exponential methods:

static double cbrt(double arg) Returns the cube root of arg.static double exp(double arg) Returns e to the arg.static double expm1(double arg) Returns e to the arg–1static double log(double arg) Returns the natural logarithm of arg.

Page 12: Presentation on java.lang package

MISCELLANEOUS MATH METHODS

Rounding FunctionsThe Math class defines several methods that provide various types of rounding operations.static int abs(int arg)static int max(int x,int y),static double floor(double arg),static double ceil(double arg),min(),max(),round() …many more

In addition to the methods just shown, Math defines several other methods, which are:copySign(), IEEEremainder(), hypot(), random(), toDegrees(), toRadians()…

Page 13: Presentation on java.lang package

Thread, ThreadGroup and RunnableThe Runnable interface and the Thread and ThreadGroup classes support multithreaded programming.

Runnable: This interface must be implemented by any class that will initiate a separate thread of execution. It defines only one abst. method, “void run() “ ,which is the entry point to the thread.

Thread: It creates a new thread of execution.

It defines the foll commonly used constructors: Thread( )Thread(Runnable threadOb)Thread(Runnable threadOb, String threadName)Thread(String threadName)Thread(ThreadGroup groupOb, Runnable threadOb)Thread(ThreadGroup groupOb, Runnable threadOb, String threadName)Thread(ThreadGroup groupOb, String threadName)threadOb is an instance of a class that implements the Runnable interface and defines where execution of the thread will begin. The name of the thread is specified by threadName. When a name is not specified, one is created by the Java Virtual Machine. groupOb specifies the thread group to which the new thread will belong. When no thread group is specified, the new thread belongs to the same group as the parent thread.

Page 14: Presentation on java.lang package

The following constants are defined by Thread:

MAX_PRIORITY

MIN_PRIORITY

NORM_PRIORITY

They specify the maximum, minimum, and default thread priorities.

Methods defined by Thread:

•static int activeCount() :returns no. of threads in group to which the thread belongs.

•static Thread currentThread() : returns a Thread object that encapsulates the thread that calls this method.

•final String getname()

•long getID()

•final int getPriority()

•void interrupt()

•void start() …many more

ThreadGroup: It creates a group of threads. It defines these two constructors:•ThreadGroup(String groupName)•ThreadGroup(ThreadGroup parentOb, String groupName)groupName specifies the name of the thread group. The first version createsa new group that has the current thread as its parent. In the second form, the parent isspecified by parentObThey offer a convenient way to manage groups of threads as a unit. eg: useful when u want to suspend & resume a no. of related threads.Methods:int activeCount()int activeGroupCount() //returns no. of groups for which invoking thread is a parent.final void destroy() ….

Page 15: Presentation on java.lang package

ThreadLocal and InheritableThreadLocal

Java defines two additional thread-related classes in java.lang:

• ThreadLocal Used to create thread local variables. Each thread will have its own copy of a thread local variable.

• InheritableThreadLocal Creates thread local variables that may be inherited.

Throwable

The Throwable class supports Java’s exception-handling system and is the class from which all exception classes are derived.

EnumAll enumerations automatically inherit Enum. Enum is a generic class that is declared as :

class Enum<E extends Enum<E>>

Here, E stands for the enumeration type. Enum has no public constructors.

clone() , comopareTo(), equals() are some of the methods it provides.

Page 16: Presentation on java.lang package

The CharSequence InterfaceIt defines methods that grant read-only access to a sequence of characters. This interface is implemented by String,

StringBuffer, and StringBuilder. It is also implemented by CharBuffer, which is in the

java.nio package

The Comparable Interface

Objects of classes that implement Comparable can be ordered. In other words, classes that implement Comparable contain objects that can be compared in some meaningful manner.

Comparable is generic and is declared like this:

interface Comparable<T>

Here, T represents the type of objects being compared.

The Comparable interface declares one method that is used to determine what Java calls the natural ordering of instances of a class. The signature of the method is shown here:

int compareTo(T obj)

Methods provided : charAt(), length(), toString(), subSequence() .

Page 17: Presentation on java.lang package

THE JAVA.LANG SUBPACKAGES

java.lang.refjava.lang.reflect

Page 18: Presentation on java.lang package

java.lang.ref

The garbage collection facilities in Java automatically determine when

no references exist to an object. The object is then assumed to be no longer needed and its

memory is reclaimed.

The classes in the java.lang.ref package provide more flexible control

over the garbage collection process. For example, assume that your program has created

numerous objects that you want to reuse at some later time. You can continue to hold references

to these objects, but that may require too much memory.

Instead, you can define “soft” references to these objects. An object that is “softly reachable”

can be reclaimed by the garbage collector, if available memory runs low. In that case, the

garbage collector sets the “soft” references to that object to null. Otherwise, the garbage

collector saves the object for possible future use.

Aprogrammer has the ability to determine whether a “softly reachable” object has been

reclaimed. If it has been reclaimed, it can be re-created. Otherwise, the object is still available

for reuse. You may also create “weak” and “phantom” references to objects.

Page 19: Presentation on java.lang package

Java.Lang.Reflect

Reflection is the ability of a program to analyze itself. The java.lang.reflect package provides the ability to obtain

information about the fields, constructors, methods, and modifiers of a class. Among other reasons, you need this

information to build software tools that enable you to work with Java Beans components. The tools use reflection to

determine dynamically the characteristics of a component. java.lang.reflect defines several classes, including

Method, Field, and Constructor. It also

defines several interfaces, including AnnotatedElement, Member, and Type. In addition, the

java.lang.reflect package includes the Array class that enables you to create and access arrays

dynamically.

Page 20: Presentation on java.lang package

Thank s!

Content Taken from the book Complete Reference –Java – 7th Edition by Herbert Schildt