You are on page 1of 26

The java.

lang package

Overview of java.lang

Provides classes that are considered fundamental to the design of the Java programming language. The java.lang package does NOT have to be imported

Classes of java.lang
The following is a partial list of classes found in the java.lang package.

Object String StringBuffer System The Wrapper Classes (list shown later) Math

The import Declaration

All classes of the java.lang package are imported automatically into all programs It's as if all programs contain the following line: import java.lang.*;

That's why we didn't have to import the System or String classes explicitly in earlier programs The Scanner class, on the other hand, is part of the java.util package, and therefore must be imported

The String and StringBuffer Classes

Because strings are so common, we deal with them in a separate unit. StringBuffer objects can be used almost anywhere that String can be used but they are mutable.

The Math Class


The Math class is part of the java.lang package The Math class contains methods that perform various mathematical functions These include: Absolute value Exponentiation Square root Trigonometric functions Rounding Random numbers And More
6

The Math Class

The methods of the Math class are all static methods (also called class methods) Static methods can be invoked through the class name no object of the Math class is ever needed.
value = Math.cos(angle);//returns the cosine of angle root = Math.sqrt(num);//returns the square root of num dist = Math.abs(val);//returns the absolute value of val small = Math.min(var1, var2);//returns the smaller value

The Math Class

The Math class also includes two symbolic constants (which are also static)
Math.E

The double value that is closer than any other to e, the base of the natural logarithms.

Math.PI

The double value that is closer than any other to pi, the ratio of the circumference of a circle to its diameter.

Wrapper Classes
The java.lang package contains wrapper classes that correspond to each primitive type:
Primitive Type
byte
short int long

Wrapper Class
Byte
Short Integer Long

float
double char boolean

Float
Double Character Boolean

Except for int and char, the wrapper class name is exactly the same as the primitive type name EXCEPT that it starts with a capital letter. double is a primitive Double is a class.

void

Void
9

Wrapper Class Names


Except for int and char, the wrapper class name is exactly the same as the primitive type name EXCEPT that it starts with a capital letter.

double is a primitive
boolean is a primitive long is a primitive

Double is a class.
Boolean is a class. Long is a class.

The wrapper classes for int and char are different.

int is a primitive char is a primitive

Integer is a class. Character is a class.


10

Wrapper Classes

An object of a wrapper class can be used in any situation where a primitive value will not suffice
For example, some objects serve as containers of other objects. Primitive values could not be stored in such containers, but wrapper objects could be.

Another example is that some methods take an Object as a parameter. So a primitive cannot be used, but an object of any class can be used.
11

Instantiating Wrapper Classes

All wrapper classes can be instantiated using the corresponding primitive type as an argument.
Integer age = new Integer(40); Double num = new Double(8.2); Character ampersand = new Character(&); Boolean isDone = new Boolean(false);

All wrapper classes EXCEPT Character and Void can be instantiated using a String argument. Integer age = new Integer(40); Double num = new Double(8.2); Boolean isDone = new Boolean(true);
The Void wrapper class cannot be instantiated.
12

Wrapper Class Methods

Wrapper classes all contain various methods that help convert from the associated type to another type. Integer num = new Integer(4); float flt = num.floatValue(); //stores 4.0 in flt

Double dbl = new Double(8.2); int val = dbl.intValue(); //stores 8 in val


13

More Wrapper Class Methods

Some Wrapper classes also contain methods that will compare the value of two objects of that class. Integer num1 = new Integer(4); Integer num2 = new Integer(11); if(num1.compareTo(num2)< 0) //executes if num1 < num2

14

Wrapper Class Methods

Wrapper classes also contain static methods that help manage the associated type Each numeric class contains a method to convert a representation in a String to the associated primitive:
int num1 = Integer.parseInt(3); double num2 = Double.parseDouble(4.7); long num3 = Long.parseLong(123456789);

15

Wrapper Class Constants

The wrapper classes often contain useful constants as well The Integer class contains MIN_VALUE and MAX_VALUE which hold the smallest and largest int values Other numeric classes will also have MIN_VALUE and MAX_VALUE which hold the smallest and largest value for their corresponding types.
16

Autoboxing

Autoboxing is the automatic conversion of a primitive value to a corresponding wrapper object: Integer obj; int num = 42; obj = num; The assignment creates the appropriate Integer object The reverse conversion (called unboxing) also occurs automatically as needed
17

Summary

The java.lang package

contains essential classes needed by Java does NOT need to be imported the String and StringBuffer classes the Math class
the wrapper classes

18

Questions
1.
a. b.

c.

d.

The java.lang package contains classes that are considered essential for almost every program. assist in translating program from English to other languages (i.e. Spanish.). assist in translating Java programs into other computer languages (i.e. C++). can allow your programs to recognize vocal commands. What is unique about the java.lang package? None of the classes in the java.lang package have methods. All of the classes in the java.lang package are abstract. This is a trick question. There is nothing unique about the java.lang package. The java.lang package is automatically imported.
19

2.
a. b. c.

d.

Questions Cont
3.
a. b. c. d.

The Object class


contains no methods. is a superclass of every other class. is required when any program interacts with physical objects. is abstract it cannot be instantiated.

4.
a. b.
c. d.

Which of the following statements about the wrapper classes is false?


For every primitive data type, there is a corresponding wrapper class. An object of a wrapper class can be used in any situation where a primitive value will not suffice, for example, in a method call that only accepts object parameters. Wrapper classes contain a variety of useful fields and methods for working with primitive data. These are usually static/class methods or fields. Wrapper classes wrap a border around your programs to make them look professional.

20

Questions Cont
5. Which of the following is NOT a Wrapper class? a. Character b. Boolean c. Float d. Int

6.

What does the following code do? num = Double.parseDouble(str); a. Returns a String with the value of the Double object. b. Returns a new double initialized to the value represented by the specified String c. Returns a new Double initialized to the value represented by the specified String d. None of the above.
21

Questions Cont
7.
a.

b.

c.

d.

What is the following? Long.MAX_VALUE A method that will compare two Long objects and return the larger one. A method that will compare two long primitives and return the larger one. A symbolic constant from the Long class that is holding the maximum value a long (or a Long) can have. None of the above. Which of the following is invalid? Integer nine = new Integer(9); Double pi = new Double(3.14); Character letter = new Character(&); All are valid.
22

8.
a.

b.
c. d.

Questions Cont
9.
a. b. c. d.

Which wrapper class cannot be instantiated?


Boolean Integer Character None of the above.

10.
a.

Autoboxing is
b. c.

d.

two automobiles boxing like in the Dodge truck commercial. the automatic drawing of windows in a box-like shape. the automatic conversion of a primitive value to a corresponding wrapper object. None of the above.
23

Questions Cont
11.

The Math Class


a. b.

c.

d.

can solve mathematical equations and inequalities. provides objects that represent mathematical equations and inequalities. contains a variety of static constants and methods that are mathematical in nature. All of the above.

12.

True or False, an object of the Math class does NOT need to be instantiated.
a. b.

true false
24

Questions Cont
13.
a. b.

c.

d.

What is the following? Math.PI A method that will return a double that represents Pi. A symbolic constant that is holding the double value that represents Pi. A symbolic constant referencing a Double object that represents Pi. None of the above Which of the following would return the absolute value of the parameter? Math.cos(num) Math.sqrt(num) Math.abs(num) None of the above
25

14.
a.

b.
c. d.

The End

Dont forget to turn your answer sheet in.

You might also like