excerpt3.pdf - 301038 Programming Proficiency Parramatta...

Info icon This preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Programming Proficiency Parramatta Campus 301038 3
Image of page 1

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

View Full Document Right Arrow Icon
2 Topics Create objects The String class The Random and Math classes Formatting output (optional) Enumerated types Wrapper classes
Image of page 2
Creating Objects A variable holds either a primitive value 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 3 Reference variable; No object created!
Image of page 3

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

View Full Document Right Arrow Icon
Creating Objects Generally, we use the new operator to create an object Creating an object is called instantiation An object is an instance of a particular class title = new String("Java Software Solutions"); This calls the String constructor , which is a special method that sets up the object 4
Image of page 4
Invoking Methods We've seen that once an object has been instantiated, we can use the dot operator to invoke its methods numChars = 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 5
Image of page 5

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

View Full Document Right Arrow Icon
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
Image of page 6
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: 7
Image of page 7

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

View Full Document Right Arrow Icon
Reference Assignment For object references , assignment copies the address: name2 = name1; name1 name2 Before: "Steve Jobs" "Steve Wozniak" name1 name2 After: "Steve Jobs" 8 1 object can be accessed via multiple variables/aliases Two or more references that refer to the same object are called aliases of each other
Image of page 8
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 9
Image of page 9

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

View Full Document Right Arrow Icon
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 • String object is immutable : once a String object has been created, neither its value nor its length can be changed 10 title = new String("Java Software Solutions");
Image of page 10
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
Image of page 11

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

View Full Document Right Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.