DOC PREVIEW
UNF COP 2551 - Using Classes and Objects

This preview shows page 1-2-3-20-21-22-41-42-43 out of 43 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 43 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 43 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 43 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 43 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 43 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 43 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 43 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 43 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 43 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 43 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Chapter 3Using Classes and ObjectsOutlineCreating ObjectsSlide 5More on String ObjectsInvoking MethodsReferencesAssignment RevisitedReference AssignmentGarbage CollectionSlide 12The String ClassString MethodsString IndexesSlide 16Class LibrariesPackagesThe import DeclarationSlide 20The Random ClassPowerPoint PresentationThe Math ClassSlide 24Slide 25Formatting OutputFormatting Output: NumberFormatSlide 28Formatting Output: Decimal FormatDecimal Format (from web) Customizing FormatsSlide 31Slide 32Enumerated TypesSlide 34Ordinal ValuesSlide 36Slide 37Slide 38Wrapper ClassesSlide 40Slide 41More on Wrapper ClassesAutoboxingChapter 3Using Classes and Objects© 2004 Pearson Addison-Wesley. All rights reserved 2/42Using 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© 2004 Pearson Addison-Wesley. All rights reserved 3/42OutlineCreating ObjectsThe String ClassPackagesFormatting OutputEnumerated TypesWrapper Classes© 2004 Pearson Addison-Wesley. All rights reserved 4/42Creating Objects•A variable holds either a primitive type or a reference to an objectx is primitive: int x = 4;•A class name can be used as a type to declare an object reference variable. String is a class.String title;title is a ‘reference’ to an object.•No object is created with this declaration. Title names an object to be created.•An object reference variable holds the address of an object, once the object is created.•The object itself must be created separately© 2004 Pearson Addison-Wesley. All rights reserved 5/42Creating Objects•Generally, we use the new operator to create an object, as in: String title;title = new String ("Java Software Solutions");This calls the String co nstructor , which isa special method that sets up the object•Creating an object is called instantiation•An object is an instance of a particular class© 2004 Pearson Addison-Wesley. All rights reserved 6/42More on String Objects•Unlike other objects, String objects are special and we have some short forms to create String objects. •(NB Strings are objects, not primitives!!)•E.g. •String name = “Joseph”;–name is a String reference that points to a storage location that contains the characters ‘Joseph.’–Creates a string name whose value is Joseph.–Same as: String name = new String (“Joseph”);–In String objects, we don’t have to cite ‘new’ operator.–Only String is special like this. –All other objects created require the ‘new’ operator.•Above in new String (“Joseph”); the “Joseph” is a parameter sent to String’s constructor. As a parameter, it must be enclosed in parentheses; Because the parameter is a String, it must include the double quotes.© 2004 Pearson Addison-Wesley. All rights reserved 7/42Invoking Methods•We've seen that once an object has been created. Together with the object name, we use the dot operator to invoke its methods count = title.length()•Note first that length is a method! (How can you tell?)•A method may return a value, which can be used in an assignment or expressionSome methods do not ‘return’ primitives or objects.•A method invocation can be thought of as asking an object (title) to perform a service (return the length of title) to me. (The number of characters in title is assigned to count by the assignment statement.)© 2004 Pearson Addison-Wesley. All rights reserved 8/42References•Note that a primitive variable contains the value itself, but an object variable contains the address of the object, that is, a ‘reference’ to 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 graphicallyMemory address"Steve Jobs"name1num138© 2004 Pearson Addison-Wesley. All rights reserved 9/42Assignment Revisited•The act of assignment takes a copy of a value and stores it in a variable•For primitive types:num138num296Before:num2 = num1;num138num238After:© 2004 Pearson Addison-Wesley. All rights reserved 10/42Reference Assignment•For object references, assignment copies the address:name2 = name1;name1name2Before:"Steve Jobs""Steve Wozniak"name1name2After:"Steve Jobs"What happened to the reference to Steve Wozniak?Contents of name2 (an address) was copied into name 1. They now point to the same memory location (aliases)© 2004 Pearson Addison-Wesley. All rights reserved 11/42Garbage 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© 2004 Pearson Addison-Wesley. All rights reserved 12/42OutlineCreating ObjectsThe String ClassPackagesFormatting OutputEnumerated TypesWrapper Classes© 2004 Pearson Addison-Wesley. All rights reserved 13/42The String Class•Because strings are so common, we don't have to use the new operator to create a String objecttitle = "Java Software Solutions";•This is special syntax that works only for strings (Check this out in NetBeans!)•Each string literal (enclosed in double quotes) represents a String object•Recall: if formally declaring a String object, can use the new operator and the String in quotes in parentheses – the syntax required for creating new objects.© 2004 Pearson Addison-Wesley. All rights reserved 14/42String 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•However, several methods of the String class return new String objects that are modified versions of the original•See the list of String methods in your book!!•Included methods are: charAt(), compareTo(), concat(), equals(), length() substring(), toLowerCase() and others…•Note: all of these methods require parameters (not shown above).© 2004 Pearson Addison-Wesley. All rights reserved 15/42String Indexes•It is occasionally helpful to refer to a particular


View Full Document

UNF COP 2551 - Using Classes and Objects

Download Using Classes and Objects
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Using Classes and Objects and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Using Classes and Objects 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?