The Java.lang Package | |
Instructor Jeff Zhuk |
Primitive Data Types and Wrapper Classes |
|
The java.lang package is automatically loaded by the Java compiler,so you
do not have to import it to use the classes in this packge.
Java's primitive data types
Java is not just a language that supports object oriented programming. Java is an object oriented programming language. Java
only has a small number of primitive data types.
Primitive data types
Type | Contains | Default | Size | Min Value Max Value
|
boolean | true or false | false | 1 bit | N.A. N.A.
|
char | Unicode character | \u0000 | 16 bits | \u0000 \uFFFF
|
byte | signed integer | 0 | 8 bits | -128 127
|
short | signed integer | 0 | 16 bits | -32768 32767
|
int | signed integer | 0 | 32 bits | -2147483648 2147483647
|
long | signed integer | 0 | 64 bits | -9223372036854775808 9223372036854775807
|
float | IEEE 754 floating-point | 0.0 | 32 bits | 3.40282347E+38 1.40239846E-45
|
double | IEEE 754 floating-point | 0.0 | 64 bits | 1.79769313486231570E+308 4.94065645841246544E-324
|
The Character class |
The wrapper classes like Character provide convinient methods
to operate with primitive data |
Synopsis |
public final class Character extends Object.
When dealing with characters, several tests and translations functions.
prove to be useful. This tests and functions have been bundled into the
CHARACTER CLASS. |
Declarations |
- char c; /* a character variable */
- Character o; /* a Character object */
|
Constructors |
- public Character (char value) -Constracts a Character
object with the specified value.
|
Methods |
- public static boolean islowerCase(char ch)-
Determins wether the specified character is ISO-LATIN-1 lowercase.
- public static boolean isUpperCase(char ch)-
Determins wether the specified character is ISO-LATIN-1 uppercase.
- public static boolean isDigit(char ch)-
Determins wether the specified character is ISO-LATIN-1 digit.
- public static boolean isSpace(char ch)-
Determins wether the specified character is ISO-LATIN-1 white space.
- public static char toLowerCase(char ch)-
Returns the lowercase character value of the specified ISO-LATIN-1 character.
Characters that are alredy lower case are returned unmodified.
- public static char toUpperCase(char ch)-
Returns the uppercase character value of the specified ISO-LATIN-1 character.
Characters that are alredy uppercase are returned unmodified.
- public static int digit(char ch,int radix)-
Returns numeric value of the character digit using the specified radix. If
the character is not a valid digit, it returns -1.
- public static char forDigit(int digit,int radix)-
Returns the character value for the specified digit in the specified radix. If
the digit is not valid in the radix, the 0 character is returned.
- public char charValue()-
Returns the value of this Character object.
- public String toString()-
Returns a String Object representing the character's value.
|
|
Example
public class MyChar {
public static void main(String args[]){
char c1 = 'A';
char c2 = 'z';
char c3 = '5';
Character objectChar = new Character('O');
System.out.println("Is character "+ c1 + "uppercase? " +
Character.isUppercase(c1));
System.out.println("Is character "+ c2 + "uppercase? " +
Character.isUppercase(c2));
System.out.println("Is character "+ c3 + "a digit? " +
Character.isDigit(c3));
System.out.println("Convert "+ c1 + "to lowercase: " +
Character.toLowerCase(c1));
System.out.println("Character object objectChar is: " +
objectChar.charValue());
}
}
This program produces the following output:
$ java MyChar
Is character A uppercase? true
Is character z uppercase? false
Is character 5 a digit? true
Convert A to lowercase: a
Character object objectChar is: O
Example of using Integer class with parseInt method
to convert a string into an integer:
public class MyApplet extends Applet {
public void init() {
String number = getParameter("numberOfImages");
int nImages = 0;
try {
nImages = Integer.parseInt(number);
} catch(Exception e) {
System.out.println(
"MyApplet.init: exception: numberOfImages parameter is " +
numberOfImages + " - expected integer.");
}
// ... do smth ...
}
// .. more methods
}
BOOLEAN CLASS
Even boolean values have a class: BOOLEAN.
While the functions for the Boolean class are more limited
then the numeric classes, you still have the ability to access booleans as object.
Synopsis |
public final class Boolean extends Object |
Declarations |
- boolean b; /*boolean variable b */
- Boolean b; /* Boolean Object b */
|
Constructors |
- public Boolean(boolean b)-
Constructs a Boolean object initialized to the specified boolean value
- public Boolean(String s)-
Constructs a Boolean object initialized to the value specified by the String
parameter.
|
Boolean Variables |
- public final static Boolean FALSE-
Defines the Boolean to be false.
- public final static Boolean TRUE-
Defines the Boolean to be true.
|
Boolean Methods |
- public boolean booleanValue()-
Returns the value of this Boolean object as a boolean primitive.
- public String toString()-
Returns a new String object representing this Boolean's value.
- public boolean equals(Object obj)-
Compares this object against the specified object, returning true if the values
or contents of objects are the same.
|
|
THE MATH CLASS
Synopsis |
public final class Math extends Object
The Math class represents the math library for the Java language. The Math
class conctructor is private, so you create an instance of the Math class.
You also cannot subclass this class because it is final. |
Defined Variables |
- public final static double E-
The float representation of value e(2.71828...).
- public final static double PI-
The float representation of value pi(3.14159...).
Methods |
|
The methods in The Math class are called by using the syntax Math.method().
Some commonly used methods are shown below:
- public static type abs(type x)-
Returnes the absolute value of x, where type
is one of int, long, float, or double.
- public static double sqrt(double x)throws ArithmeticException-
Returns the squre root of x, h>=0.0
- public static double row(double x, double y) throws ArithmeticException-
Returns x(y).
- public static synchronized double random()-
Generates a random number between 0.0 and 1.0.
- public static type max(type a,type b)-
Returnes the greater of the two numbers, where type is one of int, long, float, or
double.
- public static long round(double x)-
Rounds off a double value by first adding .5, then returning the largest intager
<=this new value.
|
|