View
215
Download
1
Embed Size (px)
Citation preview
ECE122 L4: Creating Objects February 8, 2007
ECE 122
Engineering Problem Solving with Java
Lecture 4
Creating and Using Objects
ECE122 L4: Creating Objects February 8, 2007
Outline
° Problem: How do I create multiple objects from a class• Java provides a number of ‘built-in’ classes for us
° Understanding referencing• Requires understanding of how computer memory works
° Garbage collection• Java automatically “cleans up unused items”
° Generating random numbers in Java programs• Why is this important?
ECE122 L4: Creating Objects February 8, 2007
Objects in the computer
° Abstractions for the parts of a problem that matter
° Objects contain data items
• Data description: data value
• My car has a model year, an integer, with value 1999
• Data values are stored in variables
• Variables storing the data values for a particular object are the object’s instance variables
• The values of the object’s instance variables form the object’s state
ECE122 L4: Creating Objects February 8, 2007
Classes
° Class: a template for creating objects• Bank account class
• String class
• Student class
° Every Java object is an instance of some class
° The class of an object determines its data description and functionality
ECE122 L4: Creating Objects February 8, 2007
English to Java dictionary
° Piece of data: value
° Kind of data: type
° Place to store a value: variable
° Structured group of variables: object
° Kind of object: class
° Object of a particular class: class instance
ECE122 L4: Creating Objects February 8, 2007
Creating 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
ECE122 L4: Creating Objects February 8, 2007
Creating Objects
° Generally, we use the new operator to create an object
title = new String ("Java Software Solutions");
° Creating an object is called instantiation
° An object is an instance of a particular class
ECE122 L4: Creating Objects February 8, 2007
Invoking Methods
° We've seen that 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
ECE122 L4: Creating Objects February 8, 2007
Reference Variables
204720482049205020512052205320542055
HH
ee
llll
oo
..
° What happens when we create a string?
String testStr = new String ("Hello.");
1. Locations are allocated in memory for the string2. 8 bit characters fill up the location in the memory3. Address of first character saved in another
location called ‘testStr”
So, we can say that “testStr” is a pointer to a seriesof characters in memory
ECE122 L4: Creating Objects February 8, 2007
Reference Variables
204720482049205020512052205320542055
HH
ee
llll
oo
..
154415551556155715581559156015611562
testStr
ECE122 L4: Creating Objects February 8, 2007
References
° 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
"Steve Jobs"name1
num1 38
ECE122 L4: Creating Objects February 8, 2007
Assignment Revisited
° The act of assignment takes a copy of a value and stores it in a variable
° For primitive types:
num1 38
num2 96Before:
num2 = num1;
num1 38
num2 38After:
ECE122 L4: Creating Objects February 8, 2007
Reference Assignment
° For object references, assignment copies the address:
name2 = name1;
name1
name2Before:
"Steve Jobs"
"Steve Wozniak"
name1
name2After:
"Steve Jobs"
ECE122 L4: Creating Objects February 8, 2007
Aliases
° Two or more references that refer to the same object are called aliases of each other
° 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
• There is really only one object
ECE122 L4: Creating Objects February 8, 2007
Classes° A class can contain data declarations and method
declarations
int acctNumber;float acctBalance;
Data declarations
Method declarations
ECE122 L4: Creating Objects February 8, 2007
Bank Account Example
acct1 72354acctNumber
102.56balance
acct2 69713acctNumber
40.00balance
ECE122 L4: Creating Objects February 8, 2007
Garbage Collection
° If 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
• Returns an object's memory to the system for future use
° In other languages, the programmer is responsible for performing garbage collection
ECE122 L4: Creating Objects February 8, 2007
The 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
title = new String(“Java Software Solutions”);
alternative
ECE122 L4: Creating Objects February 8, 2007
String 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
° Several methods of the String class return new String objects
• They are modified versions of the original
ECE122 L4: Creating Objects February 8, 2007
String 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
ECE122 L4: Creating Objects February 8, 2007
Class 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, but we rely on them heavily
° Various classes we've already used (System , Scanner, String) are part of the Java standard class library
° Other class libraries can be obtained through third party vendors, or you can create them yourself
ECE122 L4: Creating Objects February 8, 2007
Packages° The classes of the Java standard class library are
organized into packages
° Some of the packages in the standard class library are:
Package
java.langjava.appletjava.awtjavax.swingjava.netjava.utiljavax.xml.parsers
Purpose
General supportCreating applets for the webGraphics and graphical user interfacesAdditional graphics capabilitiesNetwork communicationUtilitiesXML document processing
ECE122 L4: Creating Objects February 8, 2007
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
ECE122 L4: Creating Objects February 8, 2007
Random Numbers
• Streams of numbers that have been produced by a process that is believed to be random• Truly random numbers.
• One example might be the spinning of a roulette wheel.
• Most truly random activities involve some sort of physical process which is difficult to replicate
• Unfortunately, computers are not very effective at generating truly random numbers
Use pseudorandom number generator
ECE122 L4: Creating Objects February 8, 2007
The 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
ECE122 L4: Creating Objects February 8, 2007
java.util.Random ° The java.util.Random class — a more controlled way to
generate random numbers.
° If we set the same “seed,” we get the same “random” sequence.
Random generator = new Random();
Random generator2 = new Random(seed); long seed;
ECE122 L4: Creating Objects February 8, 2007
java.util.Random
° Methods:
int k = generator.nextInt ();
int k = generator.nextInt (n);
double x = generator.nextDouble ();
All 232 possible int values are produced with (approximately) equal probability
0 k < n
0 x < 1
ECE122 L4: Creating Objects February 8, 2007
Summary
° Very important to understand the difference between objects and classes
° The “new” keyword creates (instantiates) objects in memory.
• A reference (pointer) is used to indicate the location of the object
• Note that there may be multiple references to the same object
° Strings are “immutable”. They cannot be lengthened once they are “instantiated”
° Random number are useful. Java provides a class which generates pseudorandom numbers