Simple classification, in addition to the value type in Java is the object. A value type is a basic type in Java, except that the base types are objects.
Memory "A data with a value type is stored in a variable within the stack. That is, allocate memory space in the stack, store the value directly, and its value represents the data itself.
Data of a value type has a faster access speed.
A data with a reference type (reference type) does not reside in the stack, but is stored in the heap. That is, allocating memory space in the heap, not directly storing the contained value, but pointing to the value to be stored, whose value represents the address pointed to. When accessing a data with a reference type, you need to check the contents of the variable into the stack, which refers to an actual data in the heap. Data of a reference type data ratio type has greater storage size and lower access speed. "(cainiao123hack,2012)
(cainiao123hack,2012)
This topic will be divided into three parts of the code:
The first part of the code is a simple example, the second part is a value and a reference in an array, and the third part refers to passing and referencing passing
Here is the first part:
Public class topcoder{ publicstaticvoid main (string[] args) { int a=2; int b=A; System.out.println (b); System.out.println (a); b=3; System.out.println (b); System.out.println (a); }}
Output is:
2
2
3
2
class number{ public int num;} public class topcoder{ static void main (string[] args) {Nu Mber a =new number (); A.num =2; Number b =A; System.out.println (B.num); System.out.println (A.num); B.num =3; System.out.println (B.num); System.out.println (A.num); }}
Output is:
2
2
3
3
Reference
cainiao123hack,2012, http://blog.csdn.net/cainiao123hack/article/details/7749386
Different instances of value types and reference types in Java (i)