Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Exam Objectives
Java Certification - Week 7
Fundamental Classes
Chris Harris
Write code using the following methods of
the java.lang.Math class: abs, ceil, floor,
max, min, random, round, sin, cos, tan, and
sqrt.
Describe the significance of the immutability
of String objects.
Overview
The java.lang Package
The java.lang Package
The Object Class
The Wrapper Classes
The Math Class
The String Class
The StringBuffer Class
The java.lang package contains the
building blocks of the Java language.
Contains the mother of all classes
java.lang.Object
The wrapper classes.
Boolean, Number, Byte etc.
Contains many classes that are essential for
Java development.
It is automatically imported into every source
file by the compiler.
The Object Class
All classes in Java directly or indirectly
extend the Object class.
The root of the inheritance hierarchy.
The Object class provides some general
utility methods.
public int hashCode( )
Used to get a unique hash value for an object,
used in hash tables.
The Object Class (continued)
public Class getClass( )
Returns the runtime class of the object.
public boolean equals( Object obj
)
Returns true if the two references denote the
same object.
protected Object clone( )throws
CloneNodeSupportedException
If the object implements the Clonable interface
this method creates a shallow copy of the
object.
The Object Class (continued)
The Object Class (continued)
public String toString( )
Returns a textual representation of the object.
protected void finalize( ) throws
Throwable
This method is called before an object is
garbage collected. Can be used to perform
clean up.
public void notify( )
public void notifyAll( )
Methods used to wake up either a single
thread or all threads that are waiting on this
object's monitor.
void wait(long time) throws
InterruptedException
void wait(long time, int nanos)
throws InterruptedException
void wait()throws
InterruptedException
These methods can be used by a thread to
wait until notified by another thread that a
condition has occurred on this object.
The Wrapper Classes
A wrapper class is provided for each of the
primitive datatypes.
They are provided to manipulate values of
these primitive datatypes.
Objects of the wrapper classes are
immutable.
Common Wrapper Methods
static <type> valueOf( String s )
Returns the wrapper object corresponding to
the primitive value represented by s.
(except Character)
String toString( )
Returns a String object representing the
primitive value held by the wrapper.
<type>Value( )
Returns the primitive value held by the
wrapper.
boolean b = bObj.booleanValue( )
The Wrapper Classes
(continued)
All the wrapper classes have two
constructors apart from Character
One that takes a primitive value.
One that takes a string representing the
primitive value.
Boolean bObj1 = new Boolean(true);
Boolean bObj2 = new Boolean("true");
Common Wrapper Methods
(continued)
boolean equals( Object obj )
Overridden from Object, returns true if primitive
data values are equal.
int hashCode( )
Overridden from Object, returns a hash value
based on the primitive value held by the
wrapper.
Boolean Class
The Boolean class defines two objects to
represent the primative values true and
false.
Boolean.TRUE
Boolean.FALSE
Character Class
Defines many constants.
Defines many static methods for handling
various attributes of a character.
See JavaDoc.
static boolean isLowerCase( char c )
static boolean isUpperCase( char c )
static boolean isLetter( char c )
...
Numeric Wrapper Classes
Void Class
Byte, Short, Integer, Long, Float
and Double.
Not a wrapper of any primitive value. It only
denotes the Class object representing the
primitive type void.
Subclasses of Number.
All have many constants and methods
including:
<wrapper name>.MIN_VALUE
<wrapper name>.MAX_VALUE
The Math Class
The final class Math defines a set of static
methods to support mathematical functions.
Rounding, trigonometry, pseudo random
numbers ...
Let <type> =
int|long|float|double static
<type> abs( <type> )
Returns the absolute value of the argument
The Math Class (continued)
static floor( double d )
Returns the largest double value that is not
greater than d, and is a mathematical integer.
static int round( float f )
static long round( double d )
Returns the integer closest to the argument.
static double ceil( double d )
Returns the smallest double value that is not
less than the argument d, and is a
mathematical integer.
The Math Class (continued)
static
static
static
static
int max(int a,int b)
long max(long a,long b)
float max(float a,float b)
double max(double a,double b)
Returns the greater of the two values.
static
static
static
static
int min(int a,int b)
long min(long a,long b)
float min(float a,float b)
double min(double a,double b)
Returns the smaller of the two values.
The Math Class (continued)
static double pow(double d1,double
d2)
Returns the value of d1 raised to the power of
d2.
static double exp(double d)
Returns the exponential number e raised to the
power of d.
static double log(double d)
Returns the natural logarithm ( base e ) of d.
static double sqrt(double d)
Returns the square root of d. For NaN or
negative arguments, the result is NaN.
Trigonometry Functions
static double sin( double a )
Returns the trigonometric sine of an angle a
specified in radians.
static double cos( double a )
Returns the trigonometric cosine of an angle a
specified in radians.
Pseudo Random Number
Generator
static double random( )
Returns a random number greater or equal to
0.0 and less than 1.0, where the value is
selected randomly from the range according to
a uniform distribution.
static double tan( double a )
Returns the trigonometric tangent of an angle
a specified in radians.
The String class
The String class implements immutable
character strings.
Read-only once created.
The characters in a string are represented
using Unicode.
Creating and Initializing Strings
The String class has many constructors
to create and initialize String objects.
The easiest way to create and initialize a
String object is based on a String
literal.
String str = "my string";
Creating and Initializing Strings
(continued)
A string literal is implemented as a
anonymous String object.
Java optimizes handling of string literals.
Only one anonymous String object is shared by
all string literals with the same content.
str1 and str2 denote the same anonymous
String object.
String str1 = "my string";
String str2 = "my string";
Comparing Strings
boolean equals( Object obj )
Returns true if the two String objects contain
the same sequence of characters, false
otherwise.
boolean equalsIgnoreCase(String
str)
Returns true if the two String objects contain
the same sequence of characters ignoring the
case, false otherwise.
Reading Individual Characters
int length( )
Returns the number of characters in a string.
char charAt( int index )
Returns the character at index, the first
character is at index 0. If the index is not valid,
StringIndexOutOfBoundsException is
thrown.
Comparing Strings (continued)
int compareTo(String str)
int compareTo(Object obj)
Returns a value based on the outcome of the
comparison.
0, if this string is equal to the string argument.
a value less than 0, if this is lexicographically
less than the string argument.
a value greater than 0, if this string is
lexicographically greater than the string
argument.
The second method will throw a
ClassCastException if obj is not actually a
String object.
Character Case in a String
String
String
String
String
toUpperCase( )
toUpperCase(Locale loc)
toLowerCase( )
toLowerCase(Locale loc)
Returns the original string if none of the
characters need their case changed, but a new
string object is returned if any of the characters
need their case changed.
Searching for Characters of
Substrings
int indexOf(int ch)
Returns the index of the first occurrence of the
argument character in a string.
int indexOf(int ch,int from)
Returns the index of the first occurrence of the
argument character in a string starting at
from.
int indexOf(String str)
Returns the index of the first occurrence of the
argument substring in a string.
Concatenation of Strings
Concatenation of two strings results in a
string, which represents the first string's
characters followed by the second string's
characters.
A new String object is created.
Can be inefficient.
Can also use the concat(...) method.
Searching for Characters of
Substrings (continued)
int indexOf(String str,int from )
Returns the index of the first occurrence of the
argument substring in a string starting at
from.
There are similar set of lastIndex(...)
methods.
Extracting Substrings
String trim()
Returns a new string with the white space
removed from the font and end.
String substring(int start)
Returns a new string that starts at start and
extends to the end of the string.
Conversion of Objects to Strings
static String valueOf(Object obj)
Equivalent to toString( );
static String valueOf(char[]
chars)
Converts character arrays to String objects.
static String valueOf(boolean b)
Converts boolean values true and false to
"true" and "false".
static String valueOf(char c)
Returns a string consisting of the character c.
Extracting Substrings
(continued)
String substring(int start, int
end)
Returns a new string that starts at start and
extends to end.
If the index value is not valid, a
StringIndexOutOFBoundsException is
thrown.
Conversion of Objects to Strings
(continued)
static
static
static
static
String
String
String
String
valueOf(int i)
valueOf(long l)
valueOf(float f)
valueOf(double d)
Equivalent to the toString() methods in the
corresponding wrapper classes.
The StringBuffer Class
The StringBuffer class implements
mutable character strings.
The characters in a string buffer can be
changed as well as its capacity.
Using a StringBuffer can be used to
efficiently append many string together.
See JavaDoc for more details.