Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.

Similar presentations


Presentation on theme: "Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used."— Presentation transcript:

1 Chapter 3 Using Classes and Objects

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

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

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

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

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

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

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

9 9 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

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

11 11 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

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

13 13 Primitive Data Type Assignment Review  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:

14 14 Object Assignment Review  For object references, assignment copies the address: name2 = name1; name1 name2 Before: ”Tom Hanks" ”Kurt Russell" name1 name2 After: ”Tom Hanks"

15 15 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 only one object

16 16 Questions


Download ppt "Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used."

Similar presentations


Ads by Google