Presentation is loading. Please wait.

Presentation is loading. Please wait.

ECE122 L4: Creating Objects February 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 4 Creating and Using Objects.

Similar presentations


Presentation on theme: "ECE122 L4: Creating Objects February 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 4 Creating and Using Objects."— Presentation transcript:

1 ECE122 L4: Creating Objects February 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 4 Creating and Using Objects

2 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?

3 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

4 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

5 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

6 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

7 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

8 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

9 ECE122 L4: Creating Objects February 8, 2007 Reference Variables 2047 2048 2049 2050 2051 2052 2053 2054 2055 H e l l o. °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

10 ECE122 L4: Creating Objects February 8, 2007 Reference Variables 2047 2048 2049 2050 2051 2052 2053 2054 2055 H e l l o. 1544 1555 1556 1557 1558 1559 1560 1561 1562 testStr

11 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

12 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 96 Before: num2 = num1; num1 38 num2 38 After:

13 ECE122 L4: Creating Objects February 8, 2007 Reference Assignment °For object references, assignment copies the address: name2 = name1; name1 name2 Before: "Steve Jobs" "Steve Wozniak" name1 name2 After: "Steve Jobs"

14 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

15 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

16 ECE122 L4: Creating Objects February 8, 2007 Bank Account Example acct1 72354 acctNumber 102.56 balance acct2 69713 acctNumber 40.00 balance

17 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

18 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

19 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

20 ECE122 L4: Creating Objects February 8, 2007 String Class

21 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

22 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

23 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.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

24 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

25 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

26 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

27 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;

28 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 2 32 possible int values are produced with (approximately) equal probability 0  k < n 0  x < 1

29 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


Download ppt "ECE122 L4: Creating Objects February 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 4 Creating and Using Objects."

Similar presentations


Ads by Google