Anda di halaman 1dari 43

Working with

Objects

how Java implements OOP

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Contents
Review: Datatypes & OOP
OOP in Java
Object Instantiation
References
Garbage Collection
Calling Instance Members
Parameter Passing
Statics
Casting
Comparison Operators
Determining the Class of an Object

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Review: Datatypes
 Java has two kinds of datatype:
 Primitives
 Objects

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Review: Datatypes
 Ideally, all datatypes should be objects,
but some compromises were made for
performance.

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Review: Classes and
Objects
 Class
 can be thought of as a template, a prototype or a blueprint
of an object
 is the fundamental structure in object-oriented
programming

 Two types of class members:


 Attributes / Fields / Properties
 specify the data types defined by the class
 Methods.
 specify the operations

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Review: Classes and
Objects
 Object
 An object is an instance of a class.
 Even if several objects are instances of the same
class, each of the objects has its own set of data.
 An instance must be created before you can call
its members.

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
OOP in Java

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Object Instantiation
 To create an object or an instance of a class, we
use the new operator.
 For example, if you want to create an instance of
the class Integer, we write the following code:

BigDecimal salary = new BigDecimal(“2600.00”);

data type
constructor
of “new”
reference operator
reference

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Object Instantiation
The constructor
 A special method which instantiates an object,
it has the same name as the class. Must be
used with the “new” operator.
 Code for initialization of the object can be
found here. It can have initialization
parameters.

BigDecimal(“2600.00”);

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Object Instantiation
 The new operator
 allocates a memory location for that object
and returns a reference of that memory
location to you.

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
References
Reference
 Number pointing to a memory location.

salary value = 2600.00


points to

reference to the BigDecimal


the BigDecimal object
object

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
References
 More than one reference can point to the
same object.
BigDecimal salary = new BigDecimal(“2600.00”);
BigDecimal mySalary = salary;
BigDecimal hisSalary = salary;

mySalary

salary value = 2600.00

hisSalary
Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
References
 A reference can also point to no object at
all.

BigDecimal yourSalary = null;

yourSalary null

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
References
Using final on a reference
 Prevents the reference from changing
location

final char[] array = {‘d’, ‘o’, ‘g’};

array ‘d’ ‘o’ ‘g’


won’t point elsewhere

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
References
Using final on a reference
 Does not keep the value of the object from
being changed.
array[1] = ‘i’;

array ‘d’ ‘i’ ‘g’


won’t point elsewhere

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Garbage Collection
 When there are no more references to an
object, it becomes ready for garbage
collection.

garbage collector

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Garbage Collection
 A reference is lost when:
 The reference goes out of scope.
 The reference is pointed to another object.
 The reference is pointed to null.

if (a > b) {
BigDecimal salary = new BigDecimal(“2600”);
}
// salary is now out of scope

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Garbage Collection
 A reference is lost when:
 The reference goes out of scope.
 The reference is pointed to another object.
 The reference is pointed to null.

BigDecimal salary = new BigDecimal(“2600.00”);


...
salary = new BigDecimal(“3000.00”);
...
salary = null;

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Calling Instance Members
 Once an object is instantiated, you can call
its members:
BigDecimal salary = new BigDecimal(“2600.00”);
BigDecimal tax = new BigDecimal(“0.32”);
BigDecimal deduction = salary.multiply(tax);
BigDecimal takeHome =
salary.subtract(deduction);
System.out.println(“my take-home pay: ” +
takeHome);

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Calling Instance Members
 Once an object is instantiated, you can call
its members:

int[] numbers = {1, 2, 3, 4, 5};


int lengthOfArray = numbers.length;
System.out.println(“The length of”
+ “ my array is: ” + lengthOfArray);

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Methods
 The following are characteristics of
methods:
 It can return one or no values
 It may accept as many parameters it needs or
no parameter at all. Parameters are also called
arguments.
 After the method has finished execution, it
goes back to the method that called it.

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Parameter Passing
 Pass-by-Value
 when a pass-by-value occurs, the method
makes a copy of the value of the variable
passed to the method. The method cannot
modify the original argument even if it modifies
the parameters during calculations.
 all primitive data types when passed to a
method are pass-by-value.

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Pass-by-Value

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Parameter Passing
 Pass-by-Reference
 When a pass-by-reference occurs, the reference to an
object is passed to the calling method. This means
that, the method makes a copy of the reference of the
variable passed to the method.
 Unlike in pass-by-value, the method can modify the
actual object that the reference is pointing to,
since, although different references are used in the
methods, the location of the data they are pointing to
is the same.

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Pass-by-Reference

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Pass-by-Reference

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Static Members
 Static members belong to the class as a whole,
not to a certain instance.
 Static members can be invoked without
instantiating an object.
 Statics are part of Java’s non-OO nature (like
primitives).
 Static methods are distinguished from
instance methods in a class definition by the
keyword static.

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Static Members
 To call a static method, just type:

Classname.staticMethodName(params);

 To call a static variable, just type:

Classname.staticVaraibleName;

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Calling Static Members
 Examples of static methods, we've used so
far in our examples are,

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Calling Static Members
 Examples of calling static variables:

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Static Members
 Static methods are usually found in “Utility” classes –
classes that just hold routines for other classes:

Math.round(int a)
 The Math class holds various mathematical routines.
 The “round” method rounds a floating point primitive to an
integer.

Arrays.sort(int[] a)
 The Arrays class holds various routines to work on arrays.
 The “sort” method sorts an array in ascending order.

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Static Members
 Static variables are usually constants or environment
properties.

Integer.MAX_VALUE
 Returns the maximum value that the Java int data type can
hold, 231 – 1.

System.out
 Returns a PrintWriter to the standard output stream, usually
prints to console.

File.separator
 Returns the file separator for the current operating system.

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Casting Objects
 Instances of classes can be cast into instances of
other classes, with one restriction: The source
and destination classes must be related by
inheritance; the source object's (not the
reference's) class must be the same as or a
subclass of the destination reference's class.
 Casting objects is analogous to converting a
primitive value to a larger type, some objects
might not need to be cast explicitly.

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Casting Objects
 To cast,
(classname)object
where,
classname, is the name of the destination class
object, is a reference to the source object
 Casting with superclass and subclass
 Subclasses can be cast into their superclasses.
 A superclass can be cast into subclasses IF the
superclass reference happens to be pointing to an
instance of the desired subclass.

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Casting Objects Example
 The following example casts an instance of the class
VicePresident to an instance of the class Employee;
VicePresident is a subclass of Employee with more
information, which here defines that the VicePresident has
executive washroom privileges.

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Casting Objects
 if you cast to an incompatible type, JVM
will throw a
“ClassCastException” (Exceptions will be
discussed later.)
 ClassCastException is also thrown if you
try to cast a superclass into one of its
subclasses when it is not referencing an
instance of that subclass.

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Operators
 With the exception of the String class, all
arithmetic and logical operators cannot be used
with objects.

 == and != work with objects, but they only test if


two references are pointing to the same instance.

 No other comparison operators work with


objects. To perform such tasks you have to
create methods for them.

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Using Equality Operators
with Objects
 Example:

Object o1 = new Object();


Object o2 = o1;
o1 == o2  true

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Using Equality Operators
with Objects
 Example:

Object o1 = new Object();


Object o2 = new Object();
o1 == o2  false

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Determining the Class of an
Object
 Want to find out what an object's class is? Here's
the way to do it.

 Suppose we have the following object:

SomeClassName key = new SomeClassName();

Now, we'll discuss two ways to know the type of


the object pointed to by the reference key.

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
getClass() method
 The getClass() method returns a Class object (where
Class is itself a class) that has a method called
getName().

 In turn, getName() returns a string representing the


name of the class.

String name =
key.getClass().getName();

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
instanceof operator
 The instanceof operator has two operands: a
reference to an object on the left and a class name
on the right.

 The expression returns true or false based on


whether the object is an instance of the named class
or any of that class's subclasses.

boolean ex1 = "Texas" instanceof String; // true


Object pt = new Point(10, 10);
boolean ex2 = pt instanceof String; // false

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
The End

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved

Anda mungkin juga menyukai