Lecture Java: Chapter 3
Số trang: 85
Loại file: pptx
Dung lượng: 1.36 MB
Lượt xem: 16
Lượt tải: 0
Xem trước 9 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Lecture Java: Chapter 3 (Using Classes and Objects) presented Using Classes and Objects, Outline, Creating Objects, Creating Objects, Invoking Methods, Reference Assignment, The String Class.
Nội dung trích xuất từ tài liệu:
Lecture Java: Chapter 3 Chapter 3Using Classes and Objects Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William Loftus Copyright © 2012 Pearson Education, Inc.Using 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 API class library – the Random and Math classes – formatting output – enumerated types – wrapper classes – graphical components and containers – labels and images Copyright © 2012 Pearson Education, Inc.Outline Creating Objects The String Class The Random and Math Classes Formatting Output Enumerated Types Wrapper Classes Components and Containers Images Copyright © 2012 Pearson Education, Inc.Creating Objects• A variable holds either a primitive value or a reference to an object• A class name can be used as a type to declare an object reference variable String 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 Copyright © 2012 Pearson Education, Inc. Creating Objects• Generally, we use the new operator to create an object• Creating an object is called instantiation• An object is an instance of a particular class title = new String (Java Software Solutions); This calls the String constructor, which is a special method that sets up the object Copyright © 2012 Pearson Education, Inc. Invoking Methods• Weve seen that once an object has been instantiated, we can use the dot operator to invoke its methods numChars = 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 Copyright © 2012 Pearson Education, Inc.References• 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 num1 38 name1 Steve Jobs Copyright © 2012 Pearson Education, Inc.Assignment Revisited• The act of assignment takes a copy of a value and stores it in a variable• For primitive types: num1 38 Before: num2 96 num2 = num1; num1 38 After: num2 38 Copyright © 2012 Pearson Education, Inc.Reference Assignment• For object references, assignment copies the address: name1 Steve Jobs Before: name2 Steve Wozniak name2 = name1; name1 Steve Jobs After: name2 Copyright © 2012 Pearson Education, Inc.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 Copyright © 2012 Pearson Education, Inc.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 objects memory to the system for future use• In other languages, the programmer is responsible for performing garbage collection Copyright © 2012 Pearson Education, Inc.Outline ...
Nội dung trích xuất từ tài liệu:
Lecture Java: Chapter 3 Chapter 3Using Classes and Objects Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William Loftus Copyright © 2012 Pearson Education, Inc.Using 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 API class library – the Random and Math classes – formatting output – enumerated types – wrapper classes – graphical components and containers – labels and images Copyright © 2012 Pearson Education, Inc.Outline Creating Objects The String Class The Random and Math Classes Formatting Output Enumerated Types Wrapper Classes Components and Containers Images Copyright © 2012 Pearson Education, Inc.Creating Objects• A variable holds either a primitive value or a reference to an object• A class name can be used as a type to declare an object reference variable String 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 Copyright © 2012 Pearson Education, Inc. Creating Objects• Generally, we use the new operator to create an object• Creating an object is called instantiation• An object is an instance of a particular class title = new String (Java Software Solutions); This calls the String constructor, which is a special method that sets up the object Copyright © 2012 Pearson Education, Inc. Invoking Methods• Weve seen that once an object has been instantiated, we can use the dot operator to invoke its methods numChars = 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 Copyright © 2012 Pearson Education, Inc.References• 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 num1 38 name1 Steve Jobs Copyright © 2012 Pearson Education, Inc.Assignment Revisited• The act of assignment takes a copy of a value and stores it in a variable• For primitive types: num1 38 Before: num2 96 num2 = num1; num1 38 After: num2 38 Copyright © 2012 Pearson Education, Inc.Reference Assignment• For object references, assignment copies the address: name1 Steve Jobs Before: name2 Steve Wozniak name2 = name1; name1 Steve Jobs After: name2 Copyright © 2012 Pearson Education, Inc.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 Copyright © 2012 Pearson Education, Inc.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 objects memory to the system for future use• In other languages, the programmer is responsible for performing garbage collection Copyright © 2012 Pearson Education, Inc.Outline ...
Tìm kiếm theo từ khóa liên quan:
Kỹ thuật lập trình Lecture Java Chapter 3 Lập trình Java Lập trình Mobile Ngôn ngữ lập trình Công nghệ thông tinGợi ý tài liệu liên quan:
-
52 trang 429 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 312 0 0 -
74 trang 295 0 0
-
96 trang 291 0 0
-
Báo cáo thực tập thực tế: Nghiên cứu và xây dựng website bằng Wordpress
24 trang 289 0 0 -
Đồ án tốt nghiệp: Xây dựng ứng dụng di động android quản lý khách hàng cắt tóc
81 trang 278 0 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 274 0 0 -
Giáo trình Lập trình hướng đối tượng: Phần 2
154 trang 272 0 0 -
Tài liệu dạy học môn Tin học trong chương trình đào tạo trình độ cao đẳng
348 trang 269 1 0 -
Bài thuyết trình Ngôn ngữ lập trình: Hệ điều hành Window Mobile
30 trang 264 0 0