Fundamental Classes in the java.lang Package

In this section you should be able to:

1.      Write code using the following methods or the java.lang.Math class: abs, ceil, floor, max, min, random, round, sin, cos, tan, sqrt.

2.      Describe the significance of the immutability of String objects.

3.      Make appropriate selections in the wrapper classes to suit specified behaviour requirements.

4.      State the result of executing a fragment of code that includes an instance of one of the wrapper classes.

5.      Write code using the following methods of the wrapper classes (e.g. Integer, Double, etc.): doublevalue, floatValue, intValue, longValue, parseXxx, getXxx, toString, toHexString.

 

Fundamental Classes in the java.lang Package

 

The Object class

All classes in Java™ are directly or indirectly derived from the object class. Some of the classes are – Boolean, Number, Void, Math, String, StringBuffer etc.

 

Some of the important methods defined in the Object class are given below. These methods are available to all Java™ classes.

·        boolean equals(Object obj) – The equals method in the Object class returns true if two references point to the same object. Some classes like String and Boolean overload this method. The difference between the equals function and the equality operator is covered here.

·        String toString() – This function is used to convert objects to String. If a subclass does not override this method, the method returns a textual representation of the object, which has the following format: <name_of_the_class>@<hash_code_value_of_the_object>”.

·        The following methods related to threads are also defined in the Object class –

void notify()

void notifyall()

void wait(long millisec) throws InterruptedException

void wait(long millisec, int nanosec) throws InterruptedException

void wait() throws InterruptedException

 

Wrapper classes

Corresponding to all the primitive types, Java™ defines wrapper classes.Some examples of these wrapper classes are – Character, Boolean, Integer, Double.

 

Important methods in the Math class

Some of the methods defined in the Math class are used frequently. These are explained below. Besides the functionality, it is important to understand the arguments and return types of these functions.

static double ceil(double val) – The method ceil returns the smallest double value equal to a mathematical integer, that is not less than the argument. For example:

ceil(3.4)      returns     4.0

ceil(-2.3)    returns     -2.0

ceil(3.0)      returns     3.0

 

Static double floor(double val) – The method floor returns the largest double value equal to a mathematical integer, that is not greater than the argument. For example:

floor(3.4)    returns     3.0

floor(-2.3)   returns     -3.0

floor(3.0)    returns     3.0

 

static int round(float val) and static long round(double val) – The method round returns the integer value closest to the argument. For example:

round(3.7)    returns     4

round(3.2)    returns     3

round(3.0)    returns     3

round(-3.1)   returns     3

 

String class

The String class is used to implement immutable character strings. This means that the character string cannot be changed once it has been created. Some of the important methods are explained below.

int length() – The number of characters in the String class are returned by the length() method.

String substring(int startIndex)

String substring(int startIndex, int endIndex)

The method substring extracts a substring from the string. The method extracts a string from the startIndex to the index endIndex. Note: if the endIndex is not specified then string till the end of the input string is returned. The example below illustrates this:

String str = “I am a string”;

int len = str.length();

String str2 = str.substring(2,5);

If I run the above code in the test class

public class test

{

     public static void main(String[] args)

     {

          String str = “I am a string”;

          int len = str.length();

          String str2 = str.substring(2, 5);

          System.out.println(“\nlen = “ + len + “\nsubstring = \”” + str2 + “\””);

     }//end of main method

}//end of test class

I get the following result

 

After the above statements have run, the string str2 contains the string “am “. The string str still contains the same value “I am a string”. The variable len has the value 13.

 

StringBuffer class

The StringBuffer class implements mutable strings. This means that the characters stored in the string and the capacity of the string can be changed.

 

 

 

back