All objects are stored on the heap in our example

Info icon This preview shows pages 9–11. Sign up to view the full content.

View Full Document Right Arrow Icon
instance variables of an objects on the stack. All objects are stored on the heap . In our example with Circle and UseCircles , c is a primitive type variable that is only used in main() and is not an instance variable. Is c a primitive type? Indeed it is. In Java, c is a reference or pointer to an object, and pointers are primitive types. A pointer simply stores a memory address. Let us use two tables to represent the stack and heap, respectively. We will see how the stack and the heap evolve after a sequence of statements. (The memory addresses used in this example are arbitrary and are used only for illustration.) • After: Circle c; radius = 22; After the statement, space is allocated on the stack for c at address 10097. (The c in brackets is for readability. The JVM only tracks memory addresses.) The statement simply declares c to be a reference that is supposed to point to an object of type Circle . It is not pointing to a particular object and therefore the value at that memory address is set to NULL . On a 64-bit processor, a reference will use 64 bits or 8 bytes of memory. The heap is not necessarily empty at this point in time but for this example we show it as empty to indicate that no relevant data is on the heap. We will only show the portion of the heap that is relevant in this example. Figure 3: After Circle c; radius = 22; • After: c = new Circle(); CPEN 221 – Fall 2016
Image of page 9

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
An Introduction to Java 10 When this statement is executed, a new Circle object is created and space is allocated for it on the heap. Let us suppose that the address at which memory for the object starts is 516. This address is then assigned to c , which now points to the object on the heap. The double type uses 8 bytes of space. For simplicity, we will assume that the only data associated with the object that was created are x , y and r , each of which is 8 bytes long. Figure 4: After c = new Circle(); • After: c.x = 10; c.y = 20; c.r = radius; When these statements are executed, the appropriate locations on the heap are up- dated. Figure 5: After c.x = 10; c.y = 20; c.r = radius; Java and Pointers Sometimes one is told that to use Java one does not need to understand pointers. This view is inaccurate and can lead to significant mistakes. Java does rely on pointers because all objects are accessed using variables that point/refer to the objects. The difference between Java and, say, C or C++ is that CPEN 221 – Fall 2016
Image of page 10
An Introduction to Java 11 one cannot manipulate the pointers directly (perform pointer arithmetic), which eliminates the possibility of certain types of errors at the cost of reduced low-level control. We will discuss these matters in more detail in subsequent readings. CPEN 221 – Fall 2016
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern