Check here and read about java.lang package
Getting the Size of the Java Memory Heap
The heap is the area in memory in which objects are created.
// Get current size of heap in bytes
long heapSize = Runtime.getRuntime().totalMemory();
// Get maximum size of heap in bytes. The heap cannot grow beyond this size.
// Any attempt will result in an OutOfMemoryException.
long heapMaxSize = Runtime.getRuntime().maxMemory();
// Get amount of free memory within the heap in bytes. This size will increase
// after garbage collection and decrease as new objects are created.
long heapFreeSize = Runtime.getRuntime().freeMemory();
2. Computing Elapsed Time// Get current time
long start = System.currentTimeMillis();// Do something …// Get elapsed time in milliseconds
long elapsedTimeMillis = System.currentTimeMillis()-start;// Get elapsed time in seconds
float elapsedTimeSec = elapsedTimeMillis/1000F;// Get elapsed time in minutes
float elapsedTimeMin = elapsedTimeMillis/(60*1000F);// Get elapsed time in hours
float elapsedTimeHour = elapsedTimeMillis/(60*60*1000F);// Get elapsed time in days
float elapsedTimeDay = elapsedTimeMillis/(24*60*60*1000F);
Implementing a Class That Can Be SortedIn order for a class to be used in a sorted collection such as a SortedTree or for it to be sortable by Collections.sort(), the class must implement Comparable.public class MyClass implements Comparable {
public int compareTo(Object o) {
// If this o, return a positive value
}
}
Wrapping a Primitive Type in a Wrapper ObjectIn the Java language, the eight primitive types — boolean, byte, char, short, int, long, float, double — are not objects. However, in certain situations, objects are required. For example, collection classes such as Map and Set only work with objects. This issue is addressed by wrapping a primitive type in a wrapper object. There is a wrapper object for each primitive type.This example demonstrates how to wrap the value of a primitive type in a wrapper object and then subsequently retrieve the value of the primitive type.// Create wrapper object for each primitive type
Boolean refBoolean = new Boolean(true);
Byte refByte = new Byte((byte)123);
Character refChar = new Character(‘x’);
Short refShort = new Short((short)123);
Integer refInt = new Integer(123);
Long refLong = new Long(123L);
Float refFloat = new Float(12.3F);
Double refDouble = new Double(12.3D); // Retrieving the value in a wrapper object
boolean bool = refBoolean.booleanValue();
byte b = refByte.byteValue();
char c = refChar.charValue();
short s = refShort.shortValue();
int i = refInt.intValue();
long l = refLong.longValue();
float f = refFloat.floatValue();
double d = refDouble.doubleValue();
Getting a Class ObjectThere are three ways to retrieve a Class object.// By way of an object
Class cls = object.getClass();// By way of a string
try {
cls = Class.forName(“java.lang.String”);
} catch (ClassNotFoundException e) {
}// By way of .class
cls = java.lang.String.class;
Getting the Name of a Class Object// Get the fully-qualified name of a class
Class cls = java.lang.String.class;
String name = cls.getName(); // java.lang.String// Get the fully-qualified name of a inner class
cls = java.util.Map.Entry.class;
name = cls.getName(); // java.util.Map$Entry// Get the unqualified name of a class
cls = java.util.Map.Entry.class;
name = cls.getName();
if (name.lastIndexOf(‘.’) > 0) {
name = name.substring(name.lastIndexOf(‘.’)+1); // Map$Entry
}
// The $ can be converted to a .
name = name.replace(‘$’, ‘.’); // Map.Entry// Get the name of a primitive type
name = int.class.getName(); // int
// Get the name of an array
name = boolean[].class.getName(); // [Z
name = byte[].class.getName(); // [B
name = char[].class.getName(); // [C
name = short[].class.getName(); // [S
name = int[].class.getName(); // [I
name = long[].class.getName(); // [J
name = float[].class.getName(); // [F
name = double[].class.getName(); // [D
name = String[].class.getName(); // [Ljava.lang.String;
name = int[][].class.getName(); // [[I
// Get the name of void
cls = Void.TYPE;
name = cls.getName(); // void
Constructing a StringIf you are constructing a string with several appends, it may be more efficient to construct it using a StringBuffer and then convert it to an immutable String object.StringBuffer buf = new StringBuffer(“Java”);// Append
buf.append(” Almanac v1/”); // Java Almanac v1/
buf.append(3); // Java Almanac v1/3
// Set
int index = 15;
buf.setCharAt(index, ‘.’); // Java Almanac v1.3
// Insert
index = 5;
buf.insert(index, “Developers “);// Java Developers Almanac v1.3
// Replace
int start = 27;
int end = 28;
buf.replace(start, end, “4″); // Java Developers Almanac v1.4
// Delete
start = 24;
end = 25;
buf.delete(start, end); // Java Developers Almanac 1.4
// Convert to string
String s = buf.toString();
Comparing Strings String s1 = “a”;
String s2 = “A”;
String s3 = “B”;// Check if identical
boolean b = s1.equals(s2); // false// Check if identical ignoring case
b = s1.equalsIgnoreCase(s2); // true
// Check order of two strings
int i = s1.compareTo(s2); // 32; lowercase follows uppercase
if (i 0) {
// s1 follows s2
} else {
// s1 equals s2
}
// Check order of two strings ignoring case
i = s1.compareToIgnoreCase(s3); // -1
if (i 0) {
// s1 follows s3
} else {
// s1 equals s3
}
// A string can also be compared with a StringBuffer;
// see e70 Constructing a String
StringBuffer sbuf = new StringBuffer(“a”);
b = s1.contentEquals(sbuf); // true
Shifting Elements in an Array// Shift all elements right by one
System.arraycopy(array, 0, array, 1, array.length-1);// Shift all elements left by one
System.arraycopy(array, 1, array, 0, array.length-1);
Getting and Setting the Value of a System Property // Get a system property
String dir = System.getProperty(“user.dir”);// Set a system property
String previousValue = System.setProperty(“application.property”, “newValue”);
January 15, 2009 at 2:28 pm
excuse me can u tell me what’s the package of class(IntClass)