Java 2 API: java.lang
Wrapper Classes for Primitive Types
java.lang contains some of the core classes used in Java. Specifically,
it contains the Object super class discussed in the previous lecture.
Also, it contains object wrappers for all of the primitive types:
Boolean, Byte, Character, Double, Float, Integer, Long. These
objects, once created as immutable. In general "value" objects are
immutable in Java. To create one of these objects, you pass a value
using a primitive type to its constructor, e.g.
Float aFloat = new Float(1.3);
To extract the primtive type, there is generally a "xValue" method,
e.g.
float val = aFloat.floatValue();
In addition to being useful for storing primitive types in collections,
these wrapper classes also contain a variety of useful utility methods.
Math
The Math class contains the kinds of operations found in scientific
calculators as well as some other goodies, e.g.
- Math.abs() computes the absolute value of a number
- Trigonometric funtions: Math.sin(), Math.cos(), Math.tan(),
Math.asin(), Math.acos(), Math.atan(), Math.toDegrees(),
Math.toRadians(), Math.floor(), Math.ceil(), Math,exp(), Math.pow(),
Math.min(), Math.max(), Math.random(), Math.sqrt()
Runtime
Runtime.getRuntime() returns a Runtime object. Runtime can be used for
a few different things. Among other, you can use Runtime to execute
external programs from Java using the exec() function. You can also use
it to find out things about your environment, such as the amount of
memory used and the total amount of available memory.
System
System contains a grab-bag of system-related functions. For example,
System.currentTimeMillis() returns the current time in milliseconds
since Jan 1, 1970 UTC.You can use this value to generate Date and
Calendar objects. System.getProperty() returns a variety of useful
properties (see link at top of page). System.getSecurityManager returns
the security context -- though this is usually configured using policy
files.
String and StringBuffer
String is the class normally used for String manipulation. String is a
value object, and is therefore immutable. Thus, the following code
produce a lot of temporary objects:
String hello = "h" + "e" + "l" + "l" + "o";
This expression probably creates 9 temporary objects which are
immediately garbage collected. StringBuffer is designed to let you
build strings without creating so many temporary objects, e.g.
StringBuffer helloBuffer = new StringBuffer();
helloBuffer.append("h");
helloBuffer.append("e");
helloBuffer.append("l");
helloBuffer.append("l");
helloBuffer.append("o");
String hello =
helloBuffer.toString();