Talk about Java's reference mechanism

Talk about Java's "reference"

  • Let me talk about Java data types, after all, learning the computer, Java outset to understand the two types of data:

Here Insert Picture Description
Statement about, many students may, like me, feel that the type and class is a thing, but it is still a difference, can be seen from the chart, the class is a type of one (probably a bit inaccurate to say), good know the future, we'll talk about the quote.
Java language evolved from C ++, the most important there is one, Java **It does not support pointer types, to achieve the function pointer by reference model.**
So what pointer function is it?
       Pointer to address the data in memory, so the reference is not also point to the data memory address?
       OnlyJava referencesMore concise (looks like a python seems also, we own to explore, not to mislead you), we know that C ++ pointer there are two very important actions:
       * content access address
       & to take the contents of the address in memory
but Java do a simple, direct these two operations when we define the data, access, give the automatic operation.
Then take a look to see what difference the Java reference mechanism for different types of data:

  • The basic type (byte, short, int, long, float, double, char, boolean) definition

int i = 0;
defined when the heap has opened up space to 0 this data, as this heap memory address space, Java underlying know for sure, but we do not know, why not let us know, it would have to ask Java was the founder, and we can understand at this time there is a special memory space, the data stored 0, Java and then give it a name called i. And you know, we can only be accessed by name i i called this space the data content of the stored, this can not access the address space (want to know why, you have to ask yourself the founder of Java, and I I do not know why)

  int j=i
  这个过程有没有分配空间呢?没有,Java只是给名字为i这块堆内存空间又取了个名字,叫j

j = 2;
with the first sentence of the analysis is the same, but now the Java j the name given to the new open space,
so this time we take a look at the heap memory, you know, there are two spaces, a name i it stores this data 0, j called another space, which stores the data 2, and i and j without any contact the

Look at the following statement
int i = 0;
int j = i
i ++;
then ask now is how much you j?
Many students may think that is one, why, because the habitual think that name actually i and j represent the same piece space, space for now i represents has the data changes, then j is not also changed
, but unfortunately, you do not understand the thinking of the computer and in fact our brains are not the same, all instructions must now open up the control stores, and then operation, during operation, is now required for storage, and then continue to operate
, such as I ++, actually performed is
I = I;
I = I +. 1
mainly depends. 1 + I = I
I +. 1 with its result or need to store things, if you do not need to say that i'm sorry, i suggest you change jobs, Java will open up a new space to store the results 1, then it then i give this name to this space, it is now clear, the current heap memory there are also two spaces, one called j, the stored content is 0, there is a name j, the stored content is 1

  • Reference types

int[] i={1,2,3,4,5};
int[] j=i;
j[0]=100

Now i ask is how much you value?

Analysis: int [] i = {1,2,3,4,5 }
are familiar with C or C ++ know the name of the array is actually a pointer to the first address of
Java is how to do it, it opens up to two spaces a heap space to store the 1,2,3,4,5, the fact is that five consecutive Aberdeen with space, then a stack space, storage space in the first address of the heap, which is the address space of 1, Java to the stack space has a name called i
Here Insert Picture Description
int [] J = i
the Java and assigned a stack space, giving it a name is called j, the stack space to store the contents of the i inside the store, which is the stack of the first address
Here Insert Picture Description
j [0 ] = 100
Here Insert Picture Description
well, actually the same class as well as the interface analysis

Here's what a few puzzled points:

  1. We started indoctrinated Java is a fully object-oriented, then it's time int i = 0, i is an integer object, but int is a class right? It is easy to presume that that type int, in fact, no, he was just the type, rather than a class, do not believe the students can try i.toString () will not complain, because all Java classes are inherited from Object, Object then there are two ways, one is toString, equals a yes
  2. String types of puzzles
    String is really like, you can use the String ss = "1234" or String ss = new String ( "1234 "), then ss.toString () try, but should be noted that, because the string program design is too important, so Java directly to a good package, so it makes people not know much about him.
    But one thing should be particularly noted that, despite the string is a class, but it's instantiated objects are immutable, should be in accordance with the basic idea to analyze the type of fishes, as to why, ask Java founder.

to sum up

  • We can see the basic data types is relatively small, the time during the operation, are open directly to the heap, then the contents stored in the heap, and reference data types is relatively large, making operation time, are open stack space then, it points to the heap is operated by stack space, as to why, ask yourself Java founder.
Published 16 original articles · won praise 1 · views 1257

Guess you like

Origin blog.csdn.net/cf1169983240/article/details/104373346