Title: CSE 501N Fall 06 03: Introduction to Objects
1CSE 501NFall 0603 Introduction to Objects
2Lecture Outline
- Administrative
- Lab 1 Questions
- Textbook TA
- Boolean expressions
- Objects
- Methods
- Packages
- Scopes
3TA
- Tom Mooney will be a TA for this class
- tbmooney_at_artsci.wustl.edu
- Office Hours
- 600 PM - 800 PM on Tuesdays
- Help with labs and answer questions
4Expressions
- An expression is a combination of one or more
operators and operands - Boolean expressions compute truth results and
make use of the boolean operators
AND OR NOT ! XOR
Examples on the board
5Objects What is an object
- An object is a specific instance of a class
- Objects have an internal state
- Objects have methods that manipulate the internal
state
Bank Account int balance 0
withdraw(int amt)
deposit(int amt)
6Objects
- Objects support encapsulation
- The internal state is hidden
- Done via access modifiers on its internal state
- Access to / modification of internal state is
strictly via provided methods - Prevents rogue programmer from compromising the
object - Access modifiers ensure only the right people
can access the methods
Bank Account int balance 0
withdraw(int amt)
balance 100,000,000
deposit(int amt)
7ObjectsAccess Modifiers
- Access modifiers are used for internal state and
methods - Java provides four types of access modifiers
- public
- Anyone
- protected
- All other instances of this class and their
subclasses - default or Package
- All other classes in the package
- private
- Only instances of this class can
8ObjectsAccess Modifiers in practice
Bank Account private int balance 0
public withdraw(int amt)
default addInterest(int amt)
public deposit(int amt)
Why or why not does this make sense?
9Methods
- Methods support abstraction
- Abstraction is a process of hiding detail
- Methods specify what inputs it needs (parameters)
- Methods specify what outputs it produces (return
values) - How it translates the inputs to outputs is hidden
from the programmer
Square Root Method
a
Sqrt(a)
10Methods
- Methods can be used to achieve different kinds of
purposes - Read the value of the internal state of an object
(accessors) - Change the value of the internal state of an
object (mutators) - Control the behavior of an object
- Act as a black box for some complex operation
11Method Declarations
- A method declaration specifies the code that will
be executed when the method is invoked (called) - When a method is invoked, the flow of control
jumps to the method and executes its code - When complete, the flow returns to the place
where the method was called and continues - The invocation may or may not return a value,
depending on how the method is defined
12Method Control Flow
- If the called method is in the same class, only
the method name is needed
13Method Control Flow
- The called method is often part of another class
or object
14Method Header
- A method declaration begins with a method header
char calc (int num1, int num2, String message)
method name
parameter list
The parameter list specifies the type and name of
each parameter The name of a parameter in the
method declaration is called a formal parameter
return type
15Method Body
- The method header is followed by the method body
char calc (int num1, int num2, String message)
int sum num1 num2 char result
message.charAt (sum) return result
sum and result are local data They are created
each time the method is called, and are destroyed
when it finishes executing
The return expression must be consistent with the
return type
16The return Statement
- The return type of a method indicates the type of
value that the method sends back to the calling
location - A method that does not return a value has a void
return type - A return statement specifies the value that will
be returned - return expression
- Its expression must conform to the return type
17Parameters
- When a method is called, the actual parameters in
the invocation are copied into the formal
parameters in the method header
ch obj.calc (25, count, "Hello")
18Local Data
- As weve seen, local variables can be declared
inside a method - The formal parameters of a method create
automatic local variables when the method is
invoked - When the method finishes, all local variables are
destroyed (including the formal parameters) - Keep in mind that instance variables, declared at
the class level, exists as long as the object
exists
19Constructors
- Note that a constructor has no return type
specified in the method header, not even void - A common error is to put a return type on a
constructor, which makes it a regular method
that happens to have the same name as the class - The programmer does not have to define a
constructor for a class - Each class has a default constructor that accepts
no parameters
20Creating Objects
- A variable holds either a primitive type or a
reference to an object - A class name can be used as a type to declare an
object reference variable - String title
- No object is created with this declaration
- An object reference variable holds the address of
an object - The object itself must be created separately
21Creating Objects
- Generally, we use the new operator to create an
object
title new String (Hello World")
This calls the String constructor, which is a
special method that sets up the object
- Creating an object is called instantiation
- An object is an instance of a particular class
22Invoking Methods
- Once an object has been instantiated, we can use
the dot operator to invoke its methods - count title.length()
- A method may return a value, which can be used in
an assignment or expression - A method invocation can be thought of as asking
an object to perform a service or to manipulate
its state
23References
- Note that a primitive variable contains the value
itself, but an object variable contains the
address of the object - An object reference can be thought of as a
pointer to the location of the object - Rather than dealing with arbitrary addresses, we
often depict a reference graphically
24Assignment Revisited
- The act of assignment takes a copy of a value and
stores it in a variable - For primitive types
num2 num1
25Reference Assignment
- For object references, assignment copies the
address
name2 name1
26Aliases
- Two or more references that refer to the same
object are called aliases of each other - That creates an interesting situation one object
can be accessed using multiple reference
variables - Aliases can be useful, but should be managed
carefully - Changing an object through one reference changes
it for all of its aliases, because there is
really only one object
27Garbage Collection
- When an object no longer has any valid references
to it, it can no longer be accessed by the
program - The object is useless, and therefore is called
garbage - Java performs automatic garbage collection
periodically, returning an object's memory to the
system for future use - In other languages, the programmer is responsible
for performing garbage collection
28The String Class
- Because strings are so common, we don't have to
use the new operator to create a String object - title "Java Software Solutions"
- This is special syntax that works only for
strings - Each string literal (enclosed in double quotes)
represents a String object
29String Methods
- Once a String object has been created, neither
its value nor its length can be changed - Thus we say that an object of the String class is
immutable - However, several methods of the String class
return new String objects that are modified
versions of the original
30String Indexes
- It is occasionally helpful to refer to a
particular character within a string - This can be done by specifying the character's
numeric index - The indexes begin at zero in each string
- In the string "Hello", the character 'H' is at
index 0 and the 'o' is at index 4
31Class Libraries
- A class library is a collection of classes that
we can use when developing programs - The Java standard class library is part of any
Java development environment - Its classes are not part of the Java language per
se, but we rely on them heavily - Other class libraries can be obtained through
third party vendors, or you can create them
yourself
32Packages
- The classes of the Java standard class library
are organized into packages - Some of the packages in the standard class
library are
33The import Declaration
- When you want to use a class from a package, you
could use its fully qualified name - java.util.Scanner
- Or you can import the class, and then use just
the class name - import java.util.Scanner
- To import all classes in a particular package,
you can use the wildcard character - import java.util.
34The 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
35The Random Class
- The Random class is part of the java.util package
- It provides methods that generate pseudorandom
numbers - A Random object performs complicated calculations
based on a seed value to produce a stream of
seemingly random values
36The 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
- square root
- exponentiation
- trigonometric functions
37The Math Class
- The methods of the Math class are static methods
(also called class methods) - Static methods can be invoked through the class
name no object of the Math class is needed - value Math.cos(90) Math.sqrt(delta)
- Variables can be declared static too
- In such cases, the variable is shared across all
instances of the class - Changing the value in one instance will change it
in all others - Usually used to capture a property of a class
that will be common across all instances
38Scopes
- A scope of a variable defines the context in
which it is accessible - state or instance variables are accessible from
any method in the class - If declared public / default / protected,
accessible from other classes as well - local variables are accessible only within the
method in which they are declared - Examples on board
39Conclusion