The Java.lang Package

           

Instructor Jeff Zhuk

Primitive Data Types and Wrapper Classes

Math Class

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

TypeContainsDefaultSizeMin Value

Max Value

booleantrue or falsefalse1 bitN.A.

N.A.

charUnicode character\u000016 bits\u0000

\uFFFF

bytesigned integer08 bits-128

127

shortsigned integer016 bits-32768

32767

intsigned integer032 bits-2147483648

2147483647

longsigned integer064 bits-9223372036854775808

9223372036854775807

floatIEEE 754 floating-point0.032 bits3.40282347E+38

1.40239846E-45

doubleIEEE 754 floating-point0.064 bits1.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.

Back To Java School


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.

Back To Java School