References - References Java distinguishes two kinds of...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
References Java distinguishes two kinds of variables. Variables of primitive type store a value directly. Primitive types are: byte 8-bit integer -128 to 127 short 16-bit integer -32768 to 32767 int 32-bit integer - 2 31 to 2 31 - 1 long 64-bit integer - 2 63 to 2 63 - 1 float 32-bit floating-point 6 significant digits double 64-bit floating-point 15 significant digits char Unicode character boolean Boolean false or true All other variables store a reference to an object which is stored in the Java runtime system’s heap . All objects exist (only) in this heap. The contents of a non-primitive variable can be null , which means that it does not reference any object. The local variables of a method are stored inside the method’s activation record (also called stack frame ). The activation record is created each time the method is called. This method public static void test(int m) { int k = m + 27; String s = "Hello World"; int [] A = { s.length(), k, m }; } has four local variables, namely m , k , s , and A . (The parameters of a method are in fact also local variables, with the only difference that the runtime system automatically places a value in the variable when the method is called.) In this example, if test(13) is called, an activation record with space for four variables is created. m stores the value 13 , k stores the value 40 , but s and A only store a reference to an object on the heap. The assignment operation (as in A = B ) only copies the reference. After the assignment, A and B refer to the same object in the heap. This explains why this code: int [] A = { 1, 2, 3, 4 }; int [] B = A; B[0] = 42; System.out.println(A[0]); prints out 42 . There is only one array in this code, and both A and B refer to this same array. Passing a reference in a function call has the same effect. The operators == and != only test whether the references are identical, and so the output of String s = "Hello World"; String t = "Hello World"; System.out.println(s == t); is false . If you want to test whether two objects have the same contents (value), you need to use the equals method, as in System.out.println(s.equals(t)); Garbage collection Java objects are garbage collected: If the runtime system runs out of memory, it will check all the objects in the heap. If an object no longer has any reference pointing to it, the object is no longer useful, and will be deleted. It is hard to predict when garbage-collection will happen. If you run a small program only, probably no garbage-collection at all occurs. Garbage collection allows the Java programmers not to worry about the memory management. There are other languages which do not provide an automatic garbage collection. For example, in C++ the programmer 1
Image of page 1

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

View Full Document Right Arrow Icon
is responsible for the memory management. It is common for C++ programs to contain mistakes where objects are created but never destroyed, and so more and more unused and unusable objects fill up the heap.
Image of page 2
Image of page 3
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