Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
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); | 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 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 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 References A . | 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 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 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 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 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 Revisited xem xét lại 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 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 .