1. Trang chủ
  2. » Công Nghệ Thông Tin

Using Classes and Objects ppt

63 471 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 63
Dung lượng 1,45 MB

Nội dung

Chapter 2 Using Classes and Objects Outline  Creating object  Class libraries and Packages  The String class  The Scanner class  The Random class  The Math class  Formatting output  Wrapper classes - Autoboxing  Writing method Slide 2 Creating object • Way 1: String title = new String("Java Software Solutions"); • Way 2: String title; // this is called a reference variable title = new String("Java Software Solutions"); ClassName object_name = new ClassName( ); ClassName object_name; object_name = new ClassName( ); This calls the String constructor, which is a special method that sets up the object • Creating an object is called instantiation • An object is an instance of a particular class Slide 3 Invoking method • Once an object has been instantiated, we can use the dot operator to invoke its methods title.length(); • Note: A method may return a value or not  Example: String s = new String(“Hello"); int count = s.length(); System.out.println("Length of s is " + count); Slide 4 References • A primitive variable contains the value itself • An object reference contains the address of the object  An object reference can be thought of as a pointer to the location of the object "Java" name1 num1 38 int num1 = 38; String name1 = "Java"; Slide 5 Assignment revisited • For primitive types, assignment copies the value and stores it in a variable • Example: num1 38 num2 96 Before: num2 = num1; num1 38 num2 38 After: int num1=38, num2=96; num2 = num1; Slide 6 Assignment revisited (cont.) • For object references, assignment copies the address • Example: name2 = name1; name1 name2 Before: "Java" "C/C++" name1 name2 After: "Java" String name1="Java", name2="C/C++"; name2 = name1; Slide 7 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 Slide 8 Garbage collection • When an object no longer has any valid references to it, it can no longer be accessed by the program • Therefore the object is useless, it is called garbage • Java performs automatic garbage collection periodically, returning an object's memory to the system for future use • In some other languages, the programmer is responsible for performing garbage collection Slide 9 Outline  Creating object  Class libraries and Packages  The String class  The Scanner class  The Random class  The Math class  Formatting output  Wrapper classes - Autoboxing  Writing method Slide 10 [...]... a random number between 0.0 (inclusive) and 1.0 (exclusive)  int nextInt() • Returns a random number that ranges over all possible int values (positive and negative)  int nextInt(int num) • Returns a random number in the range 0 to num-1 Slide 27 Example: RandomNumbers.java import java.util.Random; public class RandomNumbers { public static void main (String[] args) { Random generator = new Random();... Class libraries and Packages  The String class  The Scanner class  The Random class  The Math class  Formatting output  Wrapper classes - Autoboxing  Writing methods Slide 26 The Random class • It provides methods that generate pseudorandom numbers • The Random class belongs to java.util package • Some methods of the Random class:  Random() • Constructor: creates a new pseudorandom number generator... library is a set of classes that supports the development of programs  Java standard class library • Some classes in Java standard class library: System, String Slide 11 Packages • The classes can be grouped into packages • Therefore, each class belongs to a particular package  Example: the String class, System class belong to the java.lang package • Some of the packages in the Java standard class library... matches the full package name Example: class Employee is saved at directory path com\horstmann\corejava • If you don't package your classes, then the classes belong to the default package The default package has no package name Slide 14 Using Packages • You can access the public classes in another package in two ways:  Add the full package name in front of every classname Example: java.util.Date today =... java.lang, it’s default Slide 15 Outline  Creating object  Class libraries and Packages  The String class  The Scanner class  The Random class  The Math class  Formatting output  Wrapper classes - Autoboxing  Writing method Slide 16 The String class • String is a real class in Java, not an array of characters as in C and C++ • We use the new operator to create a String object String st = new... } } Slide 34 Outline  Creating object  Class libraries and Packages  The String class  The Scanner class  The Random class  The Math class  Formatting output  Wrapper classes - Autoboxing  Writing methods Slide 35 Formatting output • It is often necessary to format values in certain ways so that they can be presented properly • Some classes in java.text package provide formatting capabilities:... class • StringBuilder class (Core Java vol 1, chapter 12) • StringTokenizer class (p.169) Slide 22 Outline  Creating object  Class libraries and Packages  The String class  The Scanner class  The Random class  The Math class  Formatting output  Wrapper classes - Autoboxing  Writing methods Slide 23 The Scanner class • Use Scanner class to read data from user • The Scanner class belongs to java.util... // 0.0 to 5.999999 num1 = (int)num2 + 1; System.out.println ("From 1 to 6: " + num1); } } Slide 28 Outline  Creating object  Class libraries and Packages  The String class  The Scanner class  The Random class  The Math class  Formatting output  Wrapper classes - Autoboxing  Writing methods Slide 29 The Math class • The Math class contains methods that perform various mathematical functions:... java.net java.sql java.io General support (default) Creating applets for the web Graphics and graphical user interfaces Additional graphics capabilities Network communication Interact with databases For input and output functions Slide 12 Class libraries - Packages Slide 13 Adding a class into a package • To place classes inside a package, you must put the name of the package at the top of your source... Random(); int num1; float num2; num1 = generator.nextInt(); System.out.println ("A random integer: " + num1); num1 = generator.nextInt(10); System.out.println ("From 0 to 9: " + num1); num1 = generator.nextInt(10) + 1; System.out.println ("From 1 to 10: " + num1); num2 = generator.nextFloat(); System.out.println ("A random float (between 0-1): " + num2); num2 = generator.nextFloat() * 6; // 0.0 to 5.999999 . 2 Using Classes and Objects Outline  Creating object  Class libraries and Packages  The String class  The Scanner class  The Random class  The Math class  Formatting output  Wrapper classes. is a set of classes that supports the development of programs  Java standard class library • Some classes in Java standard class library: System, String Slide 11 Packages • The classes can. don't package your classes, then the classes belong to the default package. The default package has no package name Slide 14 Using Packages • You can access the public classes in another package

Ngày đăng: 29/03/2014, 02:21

TỪ KHÓA LIÊN QUAN