java.lang Package
java.lang
Package
Automatically imported into all programs.
java.lang includes the following classes:
Boolean Byte Character
Class ClassLoader Compiler
Double Float InheritableThreadLocal (Java 2)
Integer Long Math
Number Object Package (Java 2)
Process Runtime RuntimePermission (Java 2)
SecurityManager Short String
StringBuffer System Thread
ThreadGroup ThreadLocal Throwable
Void
In addition, there are two classes defined by Character: Character.Subset and Character.UnicodeBlock (added by Java 2).
Java.lang also defines the following interfaces:
Clonable
Comparable(added by Java 2)
Runnable
Java uses simple types, such as int and char, for performance reasons. These data types are not a part of the object hierarchy. They are passed by value to methods and cannot be directly passed by reference. At times you will need to create an object representation for one of these simple types.eg., there are enumeration classes that deal only with objects. Java provides classes that correspond to each of the simple types. In essence, these classes encapsulate, or wrap, the simple types within a class. Thus, they are commonly referred to as type wrappers.
Abstract class. Defines a superclass that is implemented by the classes that wrap numeric types byte, short, int, long, float, and double.Six concrete subclasses.
Number has abstartct methods that return the value of the object of different numeric formats.These methods may involve rounding up and truncation.
byte byteValue()
double doubleValue()
float floatValue()
int intValue()
long longValue()
short shortValue()
Float(double num)
Float(float num)
Float(String str) throws NumberFormatException
Double(double num)
Double(String str) throws NumberFormatException
Constants defined by Float and Double are:
MAX_VALUE Maximum positive value
MIN_VALUE Minimum positive value
NaN Not a number
POSITIVE_INFINITY Positive infinity
NEGATIVE_INFINITY Negative infinity
TYPE The class object for float or double
class DoubleDemo {
public static void main (String args[ ]) {
Double d1 = new Double(3.14159);
Double d2 = new Double(314159E-5);
System.out.println(d1 + = + d2 + : + d1.equals(d2));
}
}
Output:
3.14159 = 3.14159: true
class InfNaN {
public static void main (String args[ ]) {
Double d1 = new Double(1/0.);
Double d2 = new Double(0/0.);
System.out.println(d1 + : + d1.isInfinite() + , + d1.isNaN());
System.out.println(d2 + : + d2.isInfinite() + , + d2.isNaN());
}
}
Output:
Infinity : true, false
NaN: false, true
Byte (byte num)
Byte (String str) throws NumberFormatException
Short (short num)
Short (String str) throws NumberFormatException
Integer (int num)
Integer (String str) throws NumberFormatException
Long (long num)
Long (String str) throws NumberFormatException
The following constants are defined:
MIN_VALUE Minimum value
MAX_VALUE Maximum value
TYPE The Class object for byte, short, int, or long
// use parseInt() of Integer class to convert Strings to their int equivalents.
class StringConversions {
public static void main(String args[ ]){
int num = 19648;
System.out.println(num + in binary: + Integer.toBinaryString(num));
System.out.println(num + in octal: + Integer.toOctalString(num));
System.out.println(num + in hexadecimal: + Integer.toHexString(num));
}
}
Output:
19648 in binary: 100110011000000
19648 in octal: 46300
19648 in hexadecimal: 4cc0
Character is a simple wrapper around a char.
Character (char ch)
To obtain the char value contained in a Character object, call charValue()
char charValue()
class IsDemo{
public static void main(String args[ ]){
char a[ ] = {a, b, 5, ?, A, };
for (int i = 0; i< a.length; i++){
if (Character.isDigit(a[i]))
System.out.println(a[i] + is a digit.);
if (Character.isLetter(a[i]))
System.out.println(a[i] + is a letter.);
if (Character.isWhiteSpace(a[i]))
System.out.println(a[i] + is whitespace.);
if (Character.isUpperCase(a[i]))
System.out.println(a[i] + is uppercase.);
if (Character.isLowerCase(a[i]))
System.out.println(a[i] + is lowercase.);
}
}
}
Output:
a is a letter.
a is lowercase.
b is a letter.
b is lowercase.
5 is a digit.
A is a letter.
A is uppercase.
is whitespace.
Boolean is a very thin wrapper around boolean values.
Boolean (boolean boolValue)
BoolValue must be either true or false.
Boolean (String boolStr)
If boolStr contains the String true (in uppercase or lowercase), then the new Boolean object will be true. Otherwise, it will be false.
//Recall the return type void
The void is an uninstantiable placeholder class, and has one field, TYPE, which holds a reference to the Class object for type void.
You do not create instances of this class.
public final class Void extends Object
The System class holds a collection of static methods and variables. The standard input, output, and error output of the Java run time are stored in the in, out, and err variables.
class Elapsed {
public static void main (String args[ ]) {
long start, end;
System.out.println(Timing a for loop from 0 to 1,000,000);
start =System.currentTimeMillis();
for(int i = 0; i<1000000; i++);
end = System.currentTimeMillis();
System.out.println(Elapsed Time: + (end - start));
}
}
Output:
Timing a for loop from 0 to 1,000,000
Elapsed Time: 10
The currentTimeMillis() method returns the current time in terms of milliseconds since midnight, January 1, 1970.
arraycopy()
The arraycopy() method can be used to copy quickly an array of any type from one place to another. This is much faster than the equivalent loop written out longhand in Java.
Static vois arraycopy (Object source, int sourceStart, Object target, int targetStart, int size)
class ACDemo {
static byte a[ ] = {65, 66, 67, 68, 69, 70, 71, 72, 73, 74};
static byte b[ ] = {77, 77, 77, 77, 77, 77, 77, 77, 77, 77};
public static void main(String args[ ]){
System.out.println(a = + new String (a));
System.out.println(b = + new String (b));
System.arraycopy(a, 0, b, 0, a.length);
System.out.println(a = + new String (a));
System.out.println(b = + new String (b));
System.arraycopy(a, 0, a, 1, a.length - 1);
System.arraycopy(b, 1, b, 0, b.length - 1);
System.out.println(a = + new String (a));
System.out.println(b = + new String (b));
}
}
Output:
a = ABCDEFGHIJ
b = MMMMMMMMMM
a = ABCDEFGHIJ
b = ABCDEFGHIJ
a = AABCDEFGHI
b = BCDEFGHIJJ
Class encapsulates the run-time state of an object or interface. Object of type Class are automatically created when classes are loaded. You can not explicitly declare a Class object.
class X {
int a;
int b;
}
class Y extends X {
int c;
}
class RTTI {
public static void main (String args[ ]) {
X x = new X();
Y y = new Y();
Class clObj;
clObj = x.getClass();
System.out.println(x is object of type + clObj.getName());
clObj = y.getClass();
System.out.println(y is object of type + clObj.getName());
clObj = x.getSuperclass();
System.out.println(ys superclass is + clObj.getName());
}
}
Output:
x is object of type X
y is object of type Y
ys superclass is X
Math class contains all the floating-point functions that are used for geometry and trigonometry, as well as several general-purpose methods. Math defines two double constants: E (approximately 2.72) and PI (approximately 3.14).
class Angles {
public static void main (String args [ ]){
double theta = 120.0;
System.out.println(theta + degrees is + Math.toRadians(theta) + radians.);
theta = 1.312;
System.out.println(theta + radians is + Math.toDegrees(theta) + degrees.);
}
}
Output:
120.0 degrees is 2.0943951023931953 radians.
1.312 radians is 75.17206272116401 degrees.