1 .Java 程序运行时内存的逻辑分配
method area, stack ,heap ,native method stack
In method area------ there are information about class which loaded.
In heap------there are array and obj created by new operation
In stack------local variance area ,operater number stack
In native method stack 内存------

2. Java 栈(JVM stack)
Jvm stack 是程序运行时的单位
just like this

In java,每个thread 都有自己的jvm stack,因为 jvm stack 存储着thread的执行逻辑。
3. let us talk about Java Heap.
Firstly,Java Heap divided to different generations.just like this:
Then we can see when we create a String obj,how it stored in java heap:just like this:

extends:String str=new String("123");
if there is no 123 in constant var in pool ,it will create in pool firstly, then create it in heap,just like this:

please think when string is local variance,what happens?
4:we can talk about 值传递和引用传递 now
摘自知乎:链接:
https://www.zhihu.com/question/31203609/answer/50992895
in order to know what is 值传递 and what is 引用传递,we make sense the difference between 基本类型 和 引用类型

其中a是基本类型,值就保存在变量a中,而string是引用类型,变量中保持的是实际对象的地址。一般称这种变量为引用。
how the “=” work
if
int num = 10;
String str = "hello";

now we do this
num = 20;
str = "java";
it works just like this:

from above two pictures,the conclusion is:
对于基本类型 num ,赋值运算符会直接改变变量的值,原来的值被覆盖掉。
对于引用类型 str,赋值运算符会改变引用中所保存的地址,原来的地址被覆盖掉。但是原来的对象不会被改变(重要)。
调用方法时发生了什么???
------参数传递基本上就是赋值操作------
example 1 :基本类型
void foo(int value) {
value = 100;
}
foo(num); // num 没有被改变,因为传递只是num的值。
example2 :引用类型,但该引用类型没有提供改变自身的方法。如String
void foo(String text) {
text = "windows";
}
foo(str); // str 也没有被改变
example 3:引用类型,该类型提供了改变自身的方法。
StringBuilder sb = new StringBuilder("iphone");
void foo(StringBuilder builder) {
builder.append("4");
}
foo(sb); // sb 被改变了,变成了"iphone4"。
example4:同example3,提供改变自身方法的引用类型,但是不使用,使用赋值运算。
StringBuilder sb = new StringBuilder("iphone");
void foo(StringBuilder builder) {
builder = new StringBuilder("ipad");
}
foo(sb); // sb 没有被改变,还是 "iphone"。
网友评论