Chapter 2: Objects and Primitive Data Classes and Objects String, Random, Math, NumberFormat, DecimalFormat and Wrapper Classes.

Download Chapter 2: Objects and Primitive Data Classes and Objects String, Random, Math, NumberFormat, DecimalFormat and Wrapper Classes.

Post on 28-Dec-2015




1 download

Embed Size (px)


Chapter 2: Objects and Primitive Data

Chapter 2: Objects and Primitive Data Classes and Objects

String, Random, Math, NumberFormat, DecimalFormat and Wrapper Classes2Object-Oriented ProgrammingThe following concepts are important to object-oriented programming:


3Introduction to ObjectsAn object represents something with which we can interact in a programAn object provides a collection of services that we can tell it to perform for usThe services are defined by methods in a class that defines the objectA class represents a concept, and an object represents the embodiment of a classA class can be used to create multiple objects4Class OutlineBankAccountAttributesbalance, interest, name, address, minBalance, accountNumMethodsdeposit, withdraw, addInterest, transfer5Objects and ClassesBank AccountA class(the concept)Johns Bank AccountBalance: $5,257An object(the realization)Bills Bank AccountBalance: $1,245,069Marys Bank AccountBalance: $16,833Multiple objectsfrom the same class56InheritanceOne class can be used to derive another via inheritanceClasses can be organized into inheritance hierarchiesBank AccountAccountCharge AccountSavings AccountChecking Account7Using ObjectsThe System.out object represents a destination to which we can send outputIn the Lincoln program, we invoked the println method of the System.out object:

System.out.println ("Whatever you are, be a good one.");objectmethodinformation provided to the method(parameters)8AbstractionAn abstraction hides (or suppresses) the right details at the right timeAn object is abstract in that we don't have to think about its internal details in order to use itFor example, we don't have to know how the println method works in order to invoke itA human being can manage only seven (plus or minus 2) pieces of information at one timeBut if we group information into chunks (such as objects) we can manage many complicated pieces at onceClasses and objects help us write complex software

9Creating ObjectsA variable holds either a primitive type or a reference to an objectA class name can be used as a type to declare an object reference variableString title;No object is created with this declarationAn object reference variable holds the address of an objectThe object itself must be created separately10Creating ObjectsGenerally, 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 objectCreating an object is called instantiationAn object is an instance of a particular class11Creating ObjectsBecause 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 stringsOnce an object has been instantiated, we can use the dot operator to invoke its methodstitle.length()12String MethodsThe String class has several methods that are useful for manipulating stringsMany of the methods return a value, such as an integer or a new String objectSee the list of String methods on page 84See (page 86)

13Class LibrariesA class library is a collection of classes that we can use when developing programsThe Java standard class library is part of any Java development environmentIts classes are not part of the Java language per se, but we rely on them heavilyThe System class and the String class are part of the Java standard class libraryOther class libraries can be obtained through third party vendors, or you can create them yourself14PackagesThe classes of the Java standard class library are organized into packagesSome of the packages in the standard class library are:Package


General supportCreating applets for the webGraphics and graphical user interfacesAdditional graphics capabilities and componentsNetwork communicationUtilitiesXML document processing15Java API

16The import DeclarationWhen you want to use a class from a package, you could use its fully qualified namejava.util.RandomOr you can import the class, and then use just the class nameimport java.util.Random;To import all classes in a particular package, you can use the * wildcard characterimport java.util.*;

17The import DeclarationAll classes of the java.lang package are imported automatically into all programsThat's why we didn't have to import the System or String classes explicitly in earlier programsThe Random class is part of the java.util packageIt provides methods that generate pseudorandom numbersSee (page 93)18Class MethodsSome methods can be invoked through the class name, instead of through an object of the classThese methods are called class methods or static methodsThe Math class contains many static methods, providing various mathematical functions, such as absolute value, trigonometry functions, square root, etc.temp = Math.cos(90) + Math.sqrt(delta);

19Formatting OutputThe NumberFormat class has static methods that return a formatter objectgetCurrencyInstance()getPercentInstance()Each formatter object has a method called format that returns a string with the specified information in the appropriate formatSee (page 100)20Formatting OutputThe DecimalFormat class can be used to format a floating point value in generic waysFor example, you can specify that the number should be printed to three decimal placesThe constructor of the DecimalFormat class takes a string that represents a pattern for the formatted numberSee (page 102)21Wrapper ClassesA wrapper class represents a particular primitive typeFor exampleInteger ageObj = new Integer (20);uses the Integer class to create an object which effectively represents the integer 20 as an objectThis is useful when a program requires an object instead of a primitive typeAutoboxing automatically converts between wrapper classes and primitive types, so that the following is also valid:Integer ageObj = 20;Methods on the Integer and Double wrapper classes are shown on page 87


View more >