References - References Java distinguishes two kinds of...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 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 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....
View Full Document

This note was uploaded on 04/20/2010 for the course CS CS206 taught by Professor Lee during the Fall '09 term at 카이스트, 한국과학기술원.

Page1 / 7

References - References Java distinguishes two kinds of...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online