The stack and the heap

Java stores stuff in two separate pools of memory: the stack and the heap. The heap stores all objects, including all arrays, and all class variables (i.e. those declared "static"). The stack stores all local variables, including all parameters(primitive variables and reference variables).

CS 61B handout 9

1

每次 new 一个字符串就是产生一个新的对象,即便两个字符串的内容相同,使用 "==" 比较时也为 "false" ,如果只需比较内容是否相同,应使用 "equals()" 方法

String是一个特殊的包装类数据。 可以用: String str = new String("abc"); String str = "abc"; 两种的形式来创建,第一种是用new()来新建对象的,它会在存放于堆heap中。每调用一次就会创建一个新的对象。 而第二种是先在栈中创建一个对String类的对象引用变量str,然后查找栈中有没有存放"abc",如果没有,则将"abc"存放进栈stack,并令str指 向”abc”,如果已经有”abc” 则直接令str指向“abc”。

results matching ""

    No results matching ""