Chapter 3 Using Classes and Objects Blanca Polo

Chapter 3 Using Classes and Objects Blanca Polo ICS 111 -Introduction to Computer Science I

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 2

Remember… Primitive Data Types 5 X int x = 5; A primitive data type variable contains the value itself, but an object variable contains the address of the object Primitive Data Type 3

References to Objects § An object reference variable holds the address of an object String s = “Hello”; //same as: String s = new String(“Hello”); “Hello” s An object variable contains the address of the object Memory location 4

Declaring the Object § The object itself must be created separately String s; s Memory location 5

Creating the Object itself § The object itself must be created separately String s; s s = “Hello”; “Hello” Memory location 6

Is this assignment? String s; String t; s = “hello”; t = s; t “Hello” s Memory location 7

Creating Objects, the formula § Generally, we use the new operator to create an object Object object. Reference = new Class name Object Class name ( 0 or more parameters ); The identifier or variable name Specifications for the newly created object 8

Creating Objects § Generally, we use the new operator to create an object String name = new String (”Blanca J. Polo"); This keyword new calls the String constructor That is the one that creates/builds the object Creating an object is called instantiation An object is an instance of a particular class 9

Invoking Methods § We've seen that once an object has been instantiated, we can use the dot operator to invoke its methods int name. Length = 0; name. Length = name. length() § A method may return a value § The returned value can be used in an assignment or expression 10

The main method is void A void method doesn’t return anything Return types can be of any Primitive Data Type Return types can be of any Object kind, either pre-made by JAVA or made by you 11

Review: The difference int num 38 Srting name ”Blanca Polo" 12

Primitive Data Type Assignment Review § The act of assignment takes a copy of a value and stores it in a variable § For primitive types: Before: num 1 38 num 2 96 num 2 = num 1; After: num 1 38 num 2 38 13

Object Assignment Review § For object references, assignment copies the address: Before: name 1 ”Tom Hanks" name 2 ”Kurt Russell" name 2 = name 1; After: name 1 ”Tom Hanks" name 2 14

Aliases § 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 one object 15

Garbage 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 16

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 § Other class libraries can be obtained through third party vendors, or you can create them yourself 17

What is in a Package § A package is a directory of files that contain utilities. § All classes of the java. lang package are imported automatically into all programs. § The String class, and the System. out class are part of this package. 18

Packages § The classes of the Java standard class library are organized into packages § Some of the packages in the standard class library are: Package Purpose java. lang java. applet java. awt javax. swing java. net java. util javax. xml. parsers General support Creating applets for the web Graphics and graphical user interfaces Additional graphics capabilities Network communication Utilities XML document processing 19

The import Declaration § When you want to use a class from a package, you could use its fully qualified name java. util. Random § Or you can import the class, and then use just the class name import java. util. Random; § To import all classes in a particular package, you can use the * wildcard character import java. util. *; 20

The import Declaration § Import statements are always at the top of the program. import java. lang. *; § Many of the classes that we will use in this class will need to be imported. § Different JAVA programming environments have different libraries § We will use the standard libraries that can be found in any programming environment. 21

Counting 22

The Random Class § The Random class is part of the java. util package § It provides methods that generate pseudorandom numbers 23

Random Class Methods § Constructor: Random( ) Random r = new Random( ); • float next. Float( ) Returns a random number between 0. 0 and 1. 0 inclusive float f; f = r. next. Float( ); 24

More Random Class Methods • int next. Int( ) Returns a random number that ranges over all possible int values positive and negative int i; i = r. next. Int( ); • int next. Int( int num ) Returns a random number between 0 and num-1 (inclusive) int i; i = r. next. Int(5 ); // generates a number between 0 and 4 See 25 Java/Numbers/Gen. One. Random. java Java/Numbers/One. Random. java

Questions 26

Questions? 27
- Slides: 27