Back to CS302 labs

Useful Java Classes


All links below take you to SUN's Java API site: http://java.sun.com/j2se/1.4.2/docs/api/index.html

Wrapper Classes

Boolean, Character, Byte, Short, Integer, Long, Float, Double

Boolean

The Boolean class wraps a value of the primitive type boolean in an object. An object of type Boolean contains a single field whose type is boolean.

In addition, this class provides many methods for converting a boolean to a String and a String to a boolean, as well as other constants and methods useful when dealing with a boolean.

For more information:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Boolean.html

Back to top


Character

The Character class wraps a value of the primitive type char in an object. An object of type Character contains a single field whose type is char.

In addition, this class provides several methods for determining the type of a character and converting characters from uppercase to lowercase and vice versa.

For more information:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Character.html

Back to top


Byte

The Byte class is the standard wrapper for byte values.

For more information:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Byte.html

Back to top


Short

The Short class is the standard wrapper for short values.

For more information:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Short.html

Back to top


Integer

The Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field whose type is int.

In addition, this class provides several methods for converting an int to a String and a String to an int, as well as other constants and methods useful when dealing with an int.

For more information:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Integer.html

Back to top


Long

The Long class wraps a value of the primitive type long in an object. An object of type Long contains a single field whose type is long.

In addition, this class provides several methods for converting a long to a String and a String to a long, as well as other constants and methods useful when dealing with a long.

For more information:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Long.html

Back to top


Float

The Float class wraps a value of primitive type float in an object. An object of type Float contains a single field whose type is float.

In addition, this class provides several methods for converting a float to a String and a String to a float, as well as other constants and methods useful when dealing with a float.

For more information:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Float.html

Back to top


Double

The Double class wraps a value of the primitive type double in an object. An object of type Double contains a single field whose type is double.

In addition, this class provides several methods for converting a double to a String and a String to a double, as well as other constants and methods useful when dealing with a double.

For more information:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Double.html

Back to top


String Class

The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class.

Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared. For example:

          String str = "abc";

is equivalent to:

          char data[] = {'a', 'b', 'c'};
          String str = new String(data);
      

Here are some more examples of how strings can be used:

          System.out.println("abc");
          String cde = "cde";
          System.out.println("abc" + cde);
          String c = "abc".substring(2,3);
          String d = cde.substring(1, 2);

The class String includes methods for examining individual characters of the sequence, for comparing strings, for searching strings, for extracting substrings, and for creating a copy of a string with all characters translated to uppercase or to lowercase.

The Java language provides special support for the string concatentation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuffer class and its append method. String conversions are implemented through the method toString, defined by Object and inherited by all classes in Java.

For more information:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html

Back to top


StringBuffer Class

A string buffer implements a mutable sequence of characters. A string buffer is like a String, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls.

String buffers are safe for use by multiple threads. The methods are synchronized where necessary so that all the operations on any particular instance behave as if they occur in some serial order that is consistent with the order of the method calls made by each of the individual threads involved.

String buffers are used by the compiler to implement the binary string concatenation operator +. For example, the code:

          x = "a" + 4 + "c"

is compiled to the equivalent of:

          x = new StringBuffer().append("a").append(4).append("c")
                                .toString()

which creates a new string buffer (initially empty), appends the string representation of each operand to the string buffer in turn, and then converts the contents of the string buffer to a string. Overall, this avoids creating many temporary strings.

The principal operations on a StringBuffer are the append and insert methods, which are overloaded so as to accept data of any type. Each effectively converts a given datum to a string and then appends or inserts the characters of that string to the string buffer. The append method always adds these characters at the end of the buffer; the insert method adds the characters at a specified point.

For example, if z refers to a string buffer object whose current contents are "start", then the method call z.append("le") would cause the string buffer to contain "startle", whereas z.insert(4, "le") would alter the string buffer to contain "starlet".

In general, if sb refers to an instance of a StringBuffer, then sb.append(x) has the same effect as sb.insert(sb.length(), x).

Every string buffer has a capacity. As long as the length of the character sequence contained in the string buffer does not exceed the capacity, it is not necessary to allocate a new internal buffer array. If the internal buffer overflows, it is automatically made larger.

For more information:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/StringBuffer.html

Back to top


Math Class

The class Math contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions.

Unlike some of the numeric functions of class StrictMath, all implementations of the equivalent functions of class Math are not defined to return the bit-for-bit same results. This relaxation permits better-performing implementations where strict reproducibility is not required.

By default many of the Math functions simply delegate to the equivalent functions in StrictMath for their implementations. Code generators are encouraged to use platform-specific native libraries or microprocessor instructions, where available, to provide higher-performance implementations of Math functions. Such higher-performance implementations still must conform to the specification for Math.

For more information:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Math.html

Back to top


Random Class

An instance of this class is used to generate a stream of pseudorandom numbers. The class uses a 48-bit seed, which is modified using a linear congruential formula.

If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers. In order to guarantee this property, particular algorithms are specified for the class Random. Java implementations must use all the algorithms shown here for the class Random, for the sake of absolute portability of Java code. However, subclasses of class Random are permitted to use other algorithms, so long as they adhere to the general contracts for all the methods.

The algorithms implemented by class Random use a protected utility method that on each invocation can supply up to 32 pseudorandomly generated bits.

Many applications will find the random method in class Math simpler to use.

For more information:
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Random.html

Back to top