Chapter3 Notes

Chapter3 Notes - Using Classes and Objects We can create...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
1 1 Using Classes and Objects ± We can create more interesting programs using predefined classes and related objects ± Chapter 3 focuses on ² object creation and object references ² the String class and its methods ² the Java standard class library ² the Random and Math classes ² formatting output ² enumerated types ² wrapper classes and autoboxing 2 3.1 – 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
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 3 3.1 – Creating Objects ± Generally, we use the new operator to create an object title = new String ("Java Software Solutions"); This calls the String constructor , which is a special method that sets up the object ± Creating an object is called instantiation ± An object is an instance of a particular class 4 3.1 – 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
Background image of page 2
3 5 3.1 – References ± Note that a 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 6 3.1 – 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:
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 7 3.1 – Reference Assignment ± For object references, assignment copies the address name2 = name1; name1 name2 Before: "Steve Jobs" "Steve Wozniak" name1 name2 After: "Steve Jobs" 8 3.1 – 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
Background image of page 4
5 9 3.1 – 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 10 3.2 – 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";
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 22

Chapter3 Notes - Using Classes and Objects We can create...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online