DOC PREVIEW
UNF COP 2551 - Lecture Notes

This preview shows page 1-2-3-25-26-27-28-50-51-52 out of 52 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 52 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 52 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 52 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 52 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 52 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 52 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 52 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 52 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 52 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 52 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 52 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Chapter 3Using Classes and ObjectsOutlineCreating ObjectsSlide 5Invoking MethodsReferencesAssignment RevisitedReference AssignmentAliasesGarbage CollectionSlide 12The String ClassString MethodsString IndexesSlide 16Class LibrariesPackagesThe import DeclarationSlide 20The Random ClassSlide 22The Math ClassSlide 24Slide 25Formatting OutputSlide 27Slide 28Slide 29Slide 30Slide 31Enumerated TypesSlide 33Ordinal ValuesSlide 35Slide 36Wrapper ClassesSlide 38Slide 39AutoboxingSlide 41Graphical ApplicationsGUI ComponentsGUI ContainersSlide 45Frames and Panels - moreLabelsSlide 48Nested PanelsSlide 50ImagesSlide 52Chapter 3Using Classes and Objects© 2004 Pearson Addison-Wesley. All rights reserved 3-2Using 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graphical components and containerslabels and images© 2004 Pearson Addison-Wesley. All rights reserved 3-3OutlineCreating ObjectsThe String ClassPackagesFormatting OutputEnumerated TypesWrapper ClassesComponents and ContainersImages© 2004 Pearson Addison-Wesley. All rights reserved 3-4Creating 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 variableString 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© 2004 Pearson Addison-Wesley. All rights reserved 3-5Creating Objects•Generally, we use the new operator to create an objecttitle = new String ("Java Software Solutions");This calls the String constructor, 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 3-6Invoking Methods•We've seen that once an object has been instantiated, we can use the dot operator to invoke its methodscount = 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© 2004 Pearson Addison-Wesley. All rights reserved 3-7References•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"name1num138© 2004 Pearson Addison-Wesley. All rights reserved 3-8Assignment 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 3-9Reference Assignment•For object references, assignment copies the address:name2 = name1;name1name2Before:"Steve Jobs""Steve Wozniak"name1name2After:"Steve Jobs"© 2004 Pearson Addison-Wesley. All rights reserved 3-10Aliases•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© 2004 Pearson Addison-Wesley. All rights reserved 3-11Garbage 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 3-12OutlineCreating ObjectsThe String ClassPackagesFormatting OutputEnumerated TypesWrapper ClassesComponents and ContainersImages© 2004 Pearson Addison-Wesley. All rights reserved 3-13The 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•Each string literal (enclosed in double quotes) represents a String object© 2004 Pearson Addison-Wesley. All rights reserved 3-14String 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 on page 119 and in Appendix M© 2004 Pearson Addison-Wesley. All rights reserved 3-15String 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•The indexes begin at zero in each string•In the string "Hello", the character 'H' is at index 0 and the 'o' is at index 4•See StringMutation.java (page 120)© 2004 Pearson Addison-Wesley. All rights reserved 3-16OutlineCreating ObjectsThe String ClassPackagesFormatting OutputEnumerated TypesWrapper ClassesComponents and ContainersImages© 2004 Pearson Addison-Wesley. All rights reserved 3-17Class 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•Its classes are not part of the Java language per se, but we rely on them heavily•Various classes we've already used (System , Scanner, String) are part of the Java standard class library•Other class libraries can be obtained through third party vendors, or you can create them yourself© 2004 Pearson Addison-Wesley. All rights reserved 3-18Packages•The classes of the Java standard class library are organized into packages•Some of the packages in the standard class library are:Packagejava.langjava.appletjava.awtjavax.swingjava.netjava.utiljavax.xml.parsersPurposeGeneral supportCreating applets for the webGraphics and graphical user interfacesAdditional graphics capabilitiesNetwork communicationUtilitiesXML document


View Full Document

UNF COP 2551 - Lecture Notes

Download Lecture Notes
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 Lecture Notes 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 Lecture Notes 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?