1 / 27

Using Classes and Objects: Creating and Invoking Methods

This chapter explains how to create objects using the new operator, invoke object methods using the dot operator, and the differences between primitive and object assignment. It also covers garbage collection, class libraries, packages, and importing classes.

sims
Télécharger la présentation

Using Classes and Objects: Creating and Invoking Methods

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 3 Using Classes and Objects

  2. 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

  3. Remember… Primitive Data Types 5 int x = 5; A primitive data type variable contains the value itself, but an object variable contains the address of the object X Primitive Data Type

  4. “Hello” s Memory location References to Objects • An object reference variable holds the address of an object String s = “Hello”; //same as: String s = new String(“Hello”); An object variable contains the address of the object

  5. s Memory location null Declaring the Object • The object itself must be created separately String s; null is nothing

  6. s “Hello” Memory location Creating the Object itself • The object itself must be created separately String s; s = “Hello”;

  7. t s “Hello” Memory location Is this assignment? String s; String t; s = “hello”; t = s;

  8. Creating Objects, the formula • Generally, we use the newoperator to create an object ( ); Object objectReference = new Object 0 or more Class name Class name parameters The identifier or variable name Specifications for the newly created object

  9. Creating Objects • Generally, we use the new operator to create an object String name = new String (”Blanca J. Polo"); This keyword new calls the String constructor That is the one that creates/builds the object Creating an object is called instantiation An object is an instance of a particular class

  10. Invoking Methods • We've seen that once an object has been instantiated, we can use the dot operator to invoke its methods int nameLength = 0; nameLength = name.length() • A method mayreturn a value • The returned value can be used in an assignment or expression

  11. The main method is void A void method doesn’t return anything Return types can be of any Primitive Data Type Return types can be of any Object kind, either pre-made by JAVA or made by you

  12. Review: The difference int num 38 Srting name ”Blanca Polo"

  13. 38 num1 Before: 96 num2 38 num1 After: 38 num2 Primitive Data TypeAssignment Review • The act of assignment takes a copy of a value and stores it in a variable • For primitive types: num2 = num1;

  14. ”Tom Hanks" name1 Before: ”Kurt Russell" name2 ”Tom Hanks" name1 After: name2 Object AssignmentReview • For object references, assignment copies the address: name2 = name1;

  15. 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

  16. 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

  17. Class 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 • Other class libraries can be obtained through third party vendors, or you can create them yourself

  18. What is in a Package • A package is a directory of files that contain utilities. • All classes of the java.lang package are imported automatically into all programs. • The String class, and the System.out class are part of this package.

  19. Package java.lang java.applet java.awt javax.swing java.net java.util javax.xml.parsers Purpose General support Creating applets for the web Graphics and graphical user interfaces Additional graphics capabilities Network communication Utilities XML document processing Packages • The classes of the Java standard class library are organized into packages • Some of the packages in the standard class library are:

  20. The import Declaration • When you want to use a class from a package, you could use its fully qualified name java.util.Random • Or you can import the class, and then use just the class name import java.util.Random; • To import all classes in a particular package, you can use the * wildcard character import java.util.*;

  21. The import Declaration • Import statements are always at the top of the program. import java.lang.*; • Many of the classes that we will use in this class will need to be imported. • Different JAVA programming environments have different libraries • We will use the standard libraries that can be found in any programming environment.

  22. Counting JAVA is zero based

  23. The Random Class • The Random class is part of the java.util package • It provides methods that generate pseudorandom numbers

  24. Random Class Methods • Constructor: Random( ) Random r = new Random( ); • float nextFloat( ) • Returns a random number between • 0.0 and 1.0 inclusive • float f; • f = r.nextFloat( );

  25. More Random Class Methods • int nextInt( ) • Returns a random number that • ranges over all possible int values • positive and negative • int i; • i = r.nextInt( ); • int nextInt( int num ) • Returns a random number between • 0 and num-1 (inclusive) • int i; • i = r.nextInt(5 ); • // generates a number between 0 and 4 See Java/Numbers/GenOneRandom.java Java/Numbers/OneRandom.java

  26. Questions References Aliases Primitive Data Types Objects

  27. Questions? import References Aliases Method calling Primitive Data Types Random Objects

More Related