Download presentation
Presentation is loading. Please wait.
1
References, Aliases, Garbage Collection and Packages Packages and Importing Classes Reading for this Lecture: L&L, 3.2 - 3.3 Familiarize yourself with Appendix M as a reference for the Java Class Library
2
References A primitive variable contains the value itself, but an object variable contains an object reference An object reference can be thought of as a pointer to the location of the object in memory Rather than dealing with arbitrary address values, we often depict a reference graphically “Moby Dick" title num1 38
3
Assignment Revisited The act of assignment takes a copy of a value and stores it in a variable For primitive types: num1 38 num2 96 Before: num2 = num1; num1 38 num2 38 After:
4
Reference Assignment For object references, assignment copies the address: title2 = title; title title2 Before: “Moby Dick" “Silas Marner" title title2 After: “Moby Dick" “Silas Marner" Garbage: See later slide
5
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 more than one reference variable Aliases can be useful, but should be managed carefully Changing an object via one reference variable changes it for all of its aliases, because there is really only one object
6
Garbage Collection When there are no longer any variables containing a reference to an object (e.g. “Silas Marner” on the earlier slide), the program can no longer access it The object is useless and is considered garbage Periodically, Java performs automatic garbage collection and returns an object's memory to the system for future use In other languages such as C/C++, the programmer must write explicit code to do the garbage collection
7
Garbage Collection Setting object variable’s value equal to null, makes the object garbage (unavailable): title = null; title Before: “Moby Dick" Garbage now null title After: “Moby Dick" No object
8
Garbage Collection If an object variable’s value is equal to null, any reference to an attribute or method of that object will cause your program to fail. String title = new String(“Moby Dick”); System.out.println(title.length()); // prints 9 title = null; System.out.println(title.length()); // fails
9
The String Class Because strings are so common, we don't have to use the new operator to create a String object title = “Moby Dick"; This special syntax works only for strings Each string literal (enclosed in double quotes) represents a String object
10
String Methods Once a String object has been created, neither its value nor its length can be changed Thus we say that an object of the String class is immutable However, several methods of the String class return new String objects that are modified versions of the original See the list of String methods on page 119 and in Appendix M
11
String Indexes It is occasionally helpful to refer to a particular character within a string This can be done by specifying the character's numeric index The indexes begin at zero in each string In the string “Moby Dick", the character ‘M' is at index 0 and the ‘D' is at index 5 See StringMutation.java (page 120)StringMutation.java
12
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 Its classes are not part of the Java language per se, but we rely on them heavily Various classes we've already used ( System, Scanner, String ) are part of the Java standard class library (Look them up in Appendix M!) Other class libraries can be obtained through third party vendors, or you can create them yourself
13
Packages The classes of the Java standard class library are organized into packages Some packages in the standard class library are: 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
14
The import Declaration When you want to use a class contained in a package, you can use its fully qualified name java.util.Scanner scan =... Or you can import the package containing the class and just use the class name Scanner import java.util.Scanner; Scanner scan =... To import all classes in a particular package, you can use the * wildcard character import java.util.*;
15
The import Declaration All classes of the java.lang package are imported automatically into all programs It's as if all programs contain the following line: import java.lang.*; That's why we didn't have to import the System or String classes explicitly in earlier programs The Scanner class, on the other hand, is part of the java.util package, so that class must be imported as part of its package
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.