9 Definingclasses-Partc
9 Definingclasses-Partc
Introduction to Programming
Chapter 4 & 5 Defining Classes
Part C
These slides has been extracted, modified and updated from original slides of Absolute Java 3 rd Edition by Savitch;
which has originally been prepared by Rose Williams of Binghamton University. Absolute Java is published by
Pearson Education / Addison-Wesley.
5-3
Wrapper Classes
Unboxing: the process of going from an object of a
wrapper class to the corresponding value of a
primitive type
5-5
Constants and Static Methods in Wrapper
Classes
Wrapper classes include useful constants that provide
the largest and smallest values for any of the primitive
number types
For example, Integer.MAX_VALUE,
Integer.MIN_VALUE, Double.MAX_VALUE,
Double.MIN_VALUE, etc.
5-6
Constants and Static Methods in Wrapper
Classes
Wrapper classes have static methods that convert a
correctly formed string representation of a number to the
number of a given type
The methods Integer.parseInt, Long.parseLong,
Float.parseFloat, and Double.parseDouble do this
for the primitive types (in order) int, long, float, and
double
Wrapper classes also have static methods that convert from
a numeric value to a string representation of the value
For example, the expression
Double.toString(123.99);
returns the string value "123.99"
The Character class contains a number of static
methods that are useful for string processing
5-7
Some Methods in the Class Character (Part
1 of 3)
5-8
Some Methods in the Class Character (Part
2 of 3)
5-9
Some Methods in the Class Character (Part
3 of 3)
5-10
Class Parameters
All parameters in Java are call-by-value parameters
A parameter is a local variable that is set equal to the
value of its argument
Therefore, any change to the value of the parameter
cannot change the value of its argument
5-11
Class Parameters
The value plugged into a class type parameter is a
reference (memory address)
Therefore, the parameter becomes another name for
the argument
Any change made to the object named by the parameter
(i.e., changes made to the values of its instance
variables) will be made to the object named by the
argument, because they are the same object
Note that, because it still is a call-by-value parameter,
any change made to the class type parameter itself (i.e.,
its address) will not change its argument (the reference
or memory address)
5-12
Parameters of a Class Type
5-13
The Constant null
null is a special constant that may be assigned to a variable of
any class type
YourClass yourObject = null;
5-14
Pitfall: Null Pointer Exception
Even though a class variable can be initialized to null, this
does not mean that null is an object
null is only a placeholder for an object
5-15
Using and Misusing References
When writing a program, it is very important to insure
that private instance variables remain truly private
5-16
Copy Constructor for a Class with Primitive
Type Instance Variables
public Date(Date aDate)
{
if (aDate == null) //Not a real date.
{
System.out.println("Fatal Error.");
System.exit(0);
}
month = aDate.month;
day = aDate.day;
year = aDate.year;
}
5-17
Copy Constructor for a Class with Class Type
Instance Variables
public Person(Person original)
{
if (original == null)
{
System.out.println("Fatal error.");
System.exit(0);
}
name = original.name;
born = new Date(original.born);
if (original.died == null)
died = null;
else
died = new Date(original.died);
}
5-18
Copy Constructor for a Class with Class Type
Instance Variables
Unlike the Date class, the Person class contains
three class type instance variables
5-19
Pitfall: Privacy Leaks
The previously illustrated examples from the Person
class show how an incorrect definition of a constructor can
result in a privacy leak
A similar problem can occur with incorrectly defined
mutator or accessor methods
For example:
public Date getBirthDate()
{
return born; //dangerous
}
Instead of:
public Date getBirthDate()
{
return new Date(born); //correct
}
5-20
Mutable and Immutable Classes
The accessor method getName from the Person class
appears to contradict the rules for avoiding privacy leaks:
public String getName()
{
return name; //Isn't this dangerous?
}
5-21
Mutable and Immutable Classes
A class that contains no methods (other than
constructors) that change any of the data in an object of
the class is called an immutable class
Objects of such a class are called immutable objects
5-22
Mutable and Immutable Classes
A class that contains public mutator methods or other
public methods that can change the data in its objects is
called a mutable class, and its objects are called mutable
objects
Never write a method that returns a mutable object
5-23