12. Explain how objects are created in Java and how the memory used by objects i
ID: 3536113 • Letter: 1
Question
12. Explain how objects are created in Java and how the memory used by objects is recovered. Compare this to object creation and destruction in C++.
Explanation / Answer
Objects Now that you know objects are passed by reference, we should discuss how they are created, used, and destroyed. The following subsections provide a very brief overview of objects. Section 3, Classes and Objects in Java, explains classes and objects in much greater detail. Creating Objects Declaring a variable to hold an object does not create the object itself; the variable only holds the reference to the object. To actually create an object, you must use the new keyword. This is followed by the object's class (i.e., its type) and an optional argument list in parentheses. These arguments are passed to the constructor method for the class, which serves to initialize internal fields in the new object. For example: java.awt.Button b = new java.awt.Button(); ComplexNumber c = new ComplexNumber(1.0, 1.414); There are actually two other ways to create an object. First, you can create a String object simply by enclosing characters in double quotes: String s = "This is a test"; Because strings are used so frequently, the Java compiler provides this technique as a shortcut. The second alternative way to create objects is by calling the newInstance() method of a Class object. This technique is generally used only when dynamically loading classes, so we won't discuss it here. The memory for newly created objects is dynamically allocated. Creating an object with new in Java is like calling malloc() in C to allocate memory for an instance of a struct. It is also, of course, a lot like using the new operator in C++. (Below, though, we'll see where this analogy to malloc() in C and new in C++ breaks down.) Accessing Objects As you've probably noticed in various example code fragments by now, the way you access the fields of an object is with a dot: ComplexNumber c = new ComplexNumber(); c.x = 1.0; c.y = -1.414; This syntax is reminiscent of accessing the fields of a struct in C. Recall, though, that Java objects are always accessed by reference, and that Java performs any necessary dereferencing for you. Thus, the dot in Java is more like -> in C. Java hides the fact that there is a reference here in an attempt to make your programming easier. The other difference between C and Java when accessing objects is that in Java you refer to an object's methods as if they were fields in the object itself: ComplexNumber c = new ComplexNumber(1.0, -1.414); double magnitude = c.magnitude(); Garbage Collection Objects in Java are created with the new keyword, but there is no corresponding old or delete keyword or free() method to get rid of them when they are no longer needed. If creating an object with new is like calling malloc() in C or using new in C++, then it would seem that Java is full of memory leaks, because we never call free() or use the delete operator. In fact, this isn't the case. Java uses a technique called garbage collection to automatically detect objects that are no longer being used (an object is no longer in use when there are no more references to it) and to free them. This means that in our programs, we never need to worry about freeing memory or destroying objects--the garbage collector takes care of that. If you are a C or C++ programmer, it may take some getting used to to just let allocated objects go without worrying about reclaiming their memory. Once you get used to it, however, you'll begin to appreciate what a nice feature this is. We'll discuss garbage collection in more detail in the next chapter. Arrays Most of what we learned in the previous sections about reference types and objects applies equally well to arrays in Java: Arrays are manipulated by reference. They are dynamically created with new. They are automatically garbage collected when no longer referred to. The following subsections explain these and other details. Creating and Destroying Arrays There are two ways to create arrays in Java. The first uses new, and specifies how large the array should be: byte octet_buffer[] = new byte[1024]; Button buttons[] = new Button[10]; Since creating an array does not create the objects that are stored in the array, there is no constructor to call, and the argument list is omitted with this form of the new keyword. The elements of an array created in this way are initialized to the default value for their type. The elements of an array of int are initialized to 0, for example, and the elements of an array of objects are initialized to null. The second way to create an array is with a static initializer, which looks just like it does in C: int lookup_table[] = {1, 2, 4, 8, 16, 32, 64, 128}; This syntax dynamically creates an array and initializes its elements to the specified values. The elements specified in an array initializer may be arbitrary expressions. This is different than in C, where they must be constant expressions. Arrays are automatically garbage collected, just like objects are. http://oreilly.com/catalog/javanut/excerpt/#objects
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.