- We can obtain a handle to the Class object with the method getClass ().
- The Reflection API uses this Class a lot and because of this Class only
Reflection API is possible.
- We have used the method String getName () – Returns the name of the
object as a String
Class Math
- All the methods of this class, which performs basic numeric operations, are
static.
4) static double sqrt (double a) – This returns the square root of the
double parameter.
Example:
class MathExample {
int i = 7;
int j = -9;
double x = 72.3;
double y = 0.34;
/* You can round off a floating point number to the nearest integer
with round() */
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));
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("cos(" + angle +
") is " + Math.cos(angle));
System.out.println("sin(" + angle +
") is " + Math.sin(angle));
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));
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));
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));
System.out.println(
"Here's one random number: " + Math.random());
System.out.println(
"Here's another random number: " +
Math.random());
}
}
/*
(Friends , i have cut and pasted the output for your reference)
i is 7
j is -9
x is 72.3
y is 0.34
|7| is 7
|-9| is 9
|72.3| is 72.3
|0.34| is 0.34
72.3 is approximately 72
0.34 is approximately 0
The ceiling of 7 is 7
The ceiling of -9 is -9
The ceiling of 72.3 is 73
The ceiling of 0.34 is 1
The floor of 7 is 7
The floor of -9 is -9
The floor of 72.3 is 72
The floor of 0.34 is 0
min(7,-9) is -9
min(72.3,0.34) is 0.34
min(7,72.3) is 7
min(0.34,-9) is -9
max(7,-9) is 7
max(72.3,0.34) is 72.3
max(7,72.3) is 72.3
max(0.34,-9) is 0.34
Pi is 3.14159
e is 2.71828
cos(0.785398) is 0.707107
sin(0.785398) is 0.707107
acos(0.707) is 0.785549
asin(0.707) is 0.785247
atan(0.707) is 0.615409
exp(1.0) is 2.71828
exp(10.0) is 22026.5
exp(0.0) is 1
log(1.0) is 0
log(10.0) is 2.30259
log(Math.E) is 1
pow(2.0, 2.0) is 4
pow(10.0, 3.5) is 3162.28
pow(8, -1) is 0.125
The square root of 0 is 0
The square root of 1 is 1
The square root of 2 is 1.41421
The square root of 3 is 1.73205
The square root of 4 is 2
The square root of 5 is 2.23607
The square root of 6 is 2.44949
The square root of 7 is 2.64575
The square root of 8 is 2.82843
The square root of 9 is 3
Here's one random number: 0.820582
Here's another random number: 0.866157
*/
Class Runtime
- This class encapsulates the Runtime Environment
- We can get a handle to the Runtime Class by the static method getRuntime
().
- The main use of this class is to create other processes by calling the exec ()
method.
Example:
class RuntimeExec
{
public static void main(String[] args)
{
Runtime r = Runtime.getRuntime();
Process p = null;
try
{
p = r.exec("notepad.exe");
}
catch(Exception e)
{
System.out.println("Error executing notepad");
}
System.out.println("This is after closing the notepad window");
}
}
- The exit (int i) method in this class terminates the JVM and the conventional
way of calling this method is through System.exit(0).
- The loadLibrary () method loads a native library and the conventional way of
using this is System.loadLibrary ().
- The other two methods are totalMemory () and freeMemory (), which will tell
us the memory available in the object heap.
Example:
class Memory
{
public static void main(String[] args)
{
Runtime r = Runtime.getRuntime();
long a,b;
Integer c[] = new Integer[1000];
a = r.freeMemory();
a = r.freeMemory ();
b = r.freeMemory();
System.out.println("Free Memory after allocation :" + b);
b = r.freeMemory();
}
}
Class Vector
- A Vector is a class, which groups together objects of different data types.
- The capacity of a vector is always atleast as large as the size of the vector.
Vector Constructors
- Vector () –Constructs an empty vector so that its internal data array has size
10 and its standard capacity increment is zero.
For Eg. Vector (10,5) – Will construct a Vector with a initial capacity of 10 and a
increment capacity of 5 elements and when the 11th element is added the
capacity would increase to 15 directly and so on.
Vector Methods
- The size-related operations allow the byte size and number of elements of
the vector to be determined and the vector size to be increased to a certain
capacity or trimmed to the minimum capacity needed.
Method Summary
void add(int index, Object element)
Inserts the specified element at the specified position in this
Vector.
boolean add(Object o)
Appends the specified element to the end of this Vector.
boolean addAll(Collection c)
Appends all of the elements in the specified Collection to the
end of this Vector, in the order that they are returned by the specified
Collection's Iterator.
boolean addAll(int index, Collection c)
Inserts all of the elements in in the specified Collection into this
Vector at the specified position.
void addElement(Object obj)
Adds the specified component to the end of this vector,
increasing its size by one. – Same result as the add method.
int capacity()
Returns the current capacity of this vector.
void clear()
Removes all of the elements from this Vector.
boolean contains(Object elem)
Tests if the specified object is a component in this vector.
void copyInto(Object[] anArray)
Copies the components of this vector into the specified array.
Object elementAt(int index)
Returns the component at the specified index.
void ensureCapacity(int minCapacity)
Increases the capacity of this vector, if necessary, to ensure that
it can hold at least the number of components specified by the
minimum capacity argument.
Object firstElement()
Returns the first component (the item at index 0) of this vector.
Object get(int index)
Returns the element at the specified position in this Vector.
int hashCode()
Returns the hash code value for this Vector.
int indexOf(Object elem)
Searches for the first occurence of the given argument, testing
for equality using the Object’s equals method, which compares only
the object references and not object contents. It will return -1 if the
element is not found.
int indexOf(Object elem, int index)
Searches for the first occurence of the given argument,
beginning the search at index, and testing for equality using the equals
method.
void insertElementAt(Object obj, int index)
Inserts the specified object as a component in this vector at the
specified index.
boolean isEmpty()
Tests if this vector has no components.
Object lastElement()
Returns the last component of the vector.
int lastIndexOf(Object elem)
Returns the index of the last occurrence of the specified object
in this vector.
int lastIndexOf(Object elem, int index)
Searches backwards for the specified object, starting from the
specified index, and returns an index to it.
Object remove(int index)
Removes the element at the specified position in this Vector.
boolean remove(Object o)
Removes the first occurrence of the specified element in this
Vector If the Vector does not contain the element, it is unchanged.
boolean removeAll(Collection c)
Removes from this Vector all of its elements that are contained
in the specified Collection.
void removeAllElements()
Removes all components from this vector and sets its size to
zero.
boolean removeElement(Object obj)
Removes the first (lowest-indexed) occurrence of the argument
from this vector.
void removeElementAt(int index)
Deletes the component at the specified index.
protected removeRange(int fromIndex, int toIndex)
void Removes from this List all of the elements whose index is
between fromIndex, inclusive and toIndex, exclusive.
boolean retainAll(Collection c)
Retains only the elements in this Vector that are contained in
the specified Collection.
Object set(int index, Object element)
Replaces the element at the specified position in this Vector with
the specified element.
void setElementAt(Object obj, int index)
Sets the component at the specified index of this vector to be
the specified object.
int size()
Returns the number of components in this vector. This is not the
same as the Vector’s Capacity.
List subList(int fromIndex, int toIndex)
Returns a view of the portion of this List between fromIndex,
inclusive, and toIndex, exclusive.
Object[] toArray()
Returns an array containing all of the elements in this Vector in
the correct order.
String toString()
Returns a string representation of this Vector, containing the
String representation of each element.
void trimToSize()
Trims the capacity of this vector to be the vector's current size.
Example 1:
import java.util.*;
class VectorDemo
{
public static void main(String args[])
{
// Create a vector and its elements
v.addElement(new Integer(5));
v.addElement(new Float(-14.14f));
v.addElement(new String("Hello"));
v.addElement(new Long(120000000));
v.addElement(new Double(-23.45e-11));
v.insertElementAt(s, 1);
System.out.println(v);
System.out.println(v);
// Elements in a Vector
System.out.println(v.size());
}
}
Example 2:
import java.util.Vector;
class Vect1
{
String ename;
Integer sal;
v.addElement(v1.ename);
v.addElement(v1.sal);
v.addElement(v2.ename);
v.addElement(v2.sal);
System.out.println(v.capacity());
System.out.println(v.elementAt(0));
System.out.println(v.elementAt(1));
System.out.println(v.elementAt(2));
System.out.println(v.elementAt(3));
v.removeElementAt(1);
System.out.println(v.elementAt(0));
System.out.println(v.elementAt(1));
System.out.println(v.elementAt(2));
// System.out.println(v.elementAt(3));
// System.out.println(v.indexOf("Gopal"));
v.setElementAt("Gopal",1);
System.out.println(v.elementAt(1));
}
}
Example 3:
import java.util.Vector;
import java.util.Enumeration;
Enumeration Interface
- The Enumeration Interface facilitates a standard mechanism to retrieve
elements across the different data structures in the util package.
- The Enumeration Interface defines the method by which you can enumerate
(obtain one at a time) the elements in a collection of objects.
boolean hasMoreElements()
Tests if this enumeration contains more elements.
Object nextElement()
Returns the next element of this enumeration if this enumeration
object has at least one more element to provide.
Random Class
- The Random class provides a template for the creation of random number
generators.
- The Math. Random () method provides a static function for the generation of
random double values. This static method is shared by all program code.
- The Random class provides six access methods, five of which are used to
generate random values.
Example:
import java.util.Random;
r = new Random(123456789);
for(int i=0;i<4;++i) System.out.println(r.nextDouble()+" ");
System.out.println();
r.setSeed(234567890);
for(int i=0;i<4;++i) System.out.println(r.nextGaussian()+" ");
System.out.println();
}
}
Class StringTokenizer
- The StringTokenizer class is used to create a parser for String objects.
2) String nextToken() – This will return the next token in the string,
separated by the specified or default delimiter.
Example
import java.util.*;
import java.io.*;
while (st.hasMoreTokens())
System.out.println(" "+st.nextToken());
} while(numTokens!=0);
}
}
Class Object
- This is the super class of all java classes.
3) hashcode() – This will give the memory location of the object and the
equals () method uses this method for comparison.
Class Date
- The class Date represents the specific date with millisecond precision.
2) String toString () –
3) boolean after(Date d) – This will return true if the invoking date is later
than the Date in the parameter.
4) boolean before(Date d) –
Class DateFormat
- This is the abstract class for formatting and parsing dates and times.
- The static getDateInstance (int style, Locale locale) method returns a handle
to the DateFormat class by which we can format dates. The options for style
are DEFAULT, SHORT, MEDIUM, LONG AND FULL. The locale gives u a
static reference defined in the Locale class.
- The most common method in this class is the String format(Date d).
Example:
import java.text.*;
import java.util.*;
class DateFormat1
{
public static void main(String[] args)
{
Date d = new Date();
DateFormat df;
df = DateFormat.getDateInstance(DateFormat.SHORT);
System.out.println("The day in India is now :"+df.format(d));
df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.FRANCE);
System.out.println("The day in Japan is now :"+df.format(d));
df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.UK);
System.out.println("The day in UK is now :"+df.format(d));
df = DateFormat.getDateInstance(DateFormat.LONG, Locale.KOREA);
System.out.println("The day in KOREA is now :"+df.format(d));
}
}
Class SimpleDateFormat
- This is the concrete class for formatting dates. This extends the DateFormat
class.
- First of all u will have to create a instance of SimpleDateFormat giving in the
parameters how u want to format the date or time and then use the format ()
for this purpose.
Example:
class SimpleDateFormat1
{
public static void main(String[] args)
{
Date d = new Date();
SimpleDateFormat sdf;
Another Example
Class Calender
- Calendar is an abstract base class for converting between a Date object and
a set of integer fields such as YEAR, MONTH, DAY, HOUR, and so on.
- The most important method is the int get(field), which will return the various
fields. The GregorianCalender uses this the most.
Class GregorianCalender
- We have 7 constructors, which create a GregorianCalender with the current
date and time and default locale etc.
Date d = gc.getTime () // this will get us the current instant in time. We can also
use the setTime ().
Incase we want to alter the current instant in the calendar we can use the add ()
method which has 2 parameters int field and int amount. so