ECE 122 Engineering Problem Solving with Java Lecture 4 Creating and Using Objects 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?
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 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
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 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
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 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
Reference Variables What happens when we create a string? String teststr = new String ("Hello."); 1. Locations are allocated in memory for the string 2. 8 bit characters fill up the location in the memory 3. Address of first character saved in another location called teststr So, we can say that teststr is a pointer to a series of characters in memory 2047 2048 2049 2050 2051 2052 2053 2054 2055 H e l l o. Reference Variables 1544 1555 1556 1557 1558 1559 1560 1561 1562 teststr 2047 2048 2049 2050 2051 2052 2053 2054 2055 H e l l o.
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 num1 38 name1 "Steve Jobs" Assignment Revisited The act of assignment takes a copy of a value and stores it in a variable For primitive types: Before: num1 38 num2 96 num2 = num1; After: num1 38 num2 38
Reference Assignment For object references, assignment copies the address: Before: name1 name2 "Steve Jobs" "Steve Wozniak" name2 = name1; After: name1 name2 "Steve Jobs" 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
Classes A class can contain data declarations and method declarations int acctnumber; float acctbalance; Data declarations Method declarations Bank Account Example acct1 acctnumber 72354 balance 102.56 acct2 acctnumber 69713 balance 40.00
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 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
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 String Class
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 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, EasyIn, 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
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.lang java.applet java.awt javax.swing java.net java.util javax.xml.parsers Purpose General support Creating applets for the web Graphics and graphical user interfaces Additional graphics capabilities Network communication Utilities XML document processing 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
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 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
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; java.util.random Methods: int k = generator.nextint (); All 2 32 possible int values are produced with (approximately) equal probability int k = generator.nextint (n); 0 k < n double x = generator.nextdouble (); 0 x < 1
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