Java中有两种类型,一种是基本类型,另一种是引用类型
基本类型:byte、short、char、int、long、float、double、bool
引用类型:除了以上8中类型,其他都是引用类型例1:
String str = "hello";
str是一个引用,指向了常量池中的"hello"这个对象。内存中的图:
---------- -----------
| 栈空间 | | 常量池 |
| | | |
| | | |
| | | |
| str─┤────├─→"hello"│
└───┘ └──────┘
例2:
String str = new String("hello")
str是个引用类型,我们可以理解为指针。
当这句话执行完毕后,在栈、堆中都有空间被占用,栈中占用的空间存放str这个引用,而这个引用指向堆中被new出来的String对象。先看一看String类的一个构造方法: /** The value is used for character storage. */
private final char value[]; /** The offset is the first index of the storage that is used. */
private final int offset; /** The count is the number of characters in the String. */
private final int count; public String(String original) {
int size = original.count;
char[] originalValue = original.value;
char[] v;
if (originalValue.length > size) {
// The array representing the String is bigger than the new
// String itself. Perhaps this constructor is being called
// in order to trim the baggage, so make a copy of the array.
int off = original.offset;
v = Arrays.copyOfRange(originalValue, off, off+size);
} else {
// The array representing the String is the same
// size as the String, so no point in making a copy.
v = originalValue;
}
this.offset = 0;
this.count = size;
this.value = v;
}
在栈里应该分配一个空间,这个空间是这个构造方法的String original(original的引用)
这个引用就跟第一个例子string original="hello";效果一样。
在String类中offset和count都是值变量,而value可以理解成指针。
看到了?其实new出来的这个对象自己有一个成员char []value来指向这个在常量池中字符串的。
基本类型:byte、short、char、int、long、float、double、bool
引用类型:除了以上8中类型,其他都是引用类型例1:
String str = "hello";
str是一个引用,指向了常量池中的"hello"这个对象。内存中的图:
---------- -----------
| 栈空间 | | 常量池 |
| | | |
| | | |
| | | |
| str─┤────├─→"hello"│
└───┘ └──────┘
例2:
String str = new String("hello")
str是个引用类型,我们可以理解为指针。
当这句话执行完毕后,在栈、堆中都有空间被占用,栈中占用的空间存放str这个引用,而这个引用指向堆中被new出来的String对象。先看一看String类的一个构造方法: /** The value is used for character storage. */
private final char value[]; /** The offset is the first index of the storage that is used. */
private final int offset; /** The count is the number of characters in the String. */
private final int count; public String(String original) {
int size = original.count;
char[] originalValue = original.value;
char[] v;
if (originalValue.length > size) {
// The array representing the String is bigger than the new
// String itself. Perhaps this constructor is being called
// in order to trim the baggage, so make a copy of the array.
int off = original.offset;
v = Arrays.copyOfRange(originalValue, off, off+size);
} else {
// The array representing the String is the same
// size as the String, so no point in making a copy.
v = originalValue;
}
this.offset = 0;
this.count = size;
this.value = v;
}
在栈里应该分配一个空间,这个空间是这个构造方法的String original(original的引用)
这个引用就跟第一个例子string original="hello";效果一样。
在String类中offset和count都是值变量,而value可以理解成指针。
看到了?其实new出来的这个对象自己有一个成员char []value来指向这个在常量池中字符串的。
解决方案 »
- linux swing
- java类实例在堆内存的分配
- 初始化块和构造函数执行顺序的问题
- 大家如何选择自己的JAVA发展路线?
- 一百分相送,加班提问(郁闷中):如何将一个FORM中提交的3个表格分别保存在3个XML文件中,在线等待。。。
- 绑定端口问题
- 急救:applet如何能与别的ip地址进行socket连接(不解决我就死定了)
- GregorianCaledar方法怎么用,在 JC中一运行系统 提示错误为cannot resolve symbol class GregorianCaledar 什么意思??
- 怎样用RMI实现A TIC TAC TOE game??????????
- 请问如何在java程序中关闭其他程序?急!急!!急!!!
- itext插入多张图片
- 关于java绘图 repaint()问题
对于
string s1 = "hello";
string s2 = new string("hello");
所以hbwhwang会说 如果可能访问private final char value[];的情况下,
s1.value == s2.value
因为他们都指向常量池中的同一个对象。
先遇到个问题..
Integer n1=new Integer(50);
Integer n2=new Integer(50);
System.out.println(n1==n2);
System.out.println(!n1==n2);
先一开始把他们当int值去判断了..后来才知道引用类型不是这样的..
请教LZ 这里的n1==n2是判断n1和n2是否指向同一地址吗.?
还有,,是不是每new一次就是给个新的地址?谢谢了.
前面说了,除了那8种类型,其他都是引用类型。
所以n1和n2都是引用类型。只要一new那么就在堆里分配空间了,n1和n2分别指向不同对象。
System.out.println(n1==n2);
这里是判断他们的引用,你可以理解成判断地址。
肯定是不同的,不同对象嘛~每次new实际是new了一个新的对象,在堆中分配对象的空间。希望和大家一起努力!共同进步!
我也再此声明过,我也是初学,只是对这个问题的一个总结而已。如果您觉得哪里不对,请明确指出,以便我们这些菜鸟学习。
在您眼里参数表和常量池我哪里误导别人了,请您马上立即指出,您在这里说这些“废话”有什么用呢?
而且在这里,我是通过我的努力学习,翻阅资料,查看API,询问他人得到的一个结论,我写了这么一大堆,您来一句这个,不仅对我不尊重,
您实际上完全也是在误导他人
第二,这不是争论,我是在和一些想弄明白这些问题的朋友讨论,学习的其中一部分-讨论 是必不可少的,想必您也知道。
权威这个东西谁能说谁有权威?别人都是从会到不会的,精通的人就是有权威了么?出了新技术还不是跟着屁股后面走?
这些都是基础,我希望也渴望把它弄明白。话说的非常直接,希望二位不要介意!
你认为参数那个因为是直接量所以在常量池中,而方法里面又直接指向了这个直接量,所以你说
“其实new出来的这个对象自己有一个成员char []value来指向这个在常量池中字符串的。”这句话可以看出你根本不了解什么叫常量池,也不了解什么样的变量会指向常量池。
任何语言编译后,在方法返回点上面是局部变量表,方法返回点下面是参数表。只有局变量表中的变量才可能指向常量池。
参数表中的参数(严格而讲它不叫变量,只是一个即时操作数)怎么会在常量池中。这个对象自己有一个成员char []value又怎么会在常量池中。
而参数只有在运行期才知道传入的是什么内容,它又如何会在常量池中。
那里一位大师说过
string a = "hello";
string b = new string("hello");
假如说,假如说我们可以访问value的话
那么a.value == b.value;
这个你怎么理解呢?
我说value这个引用指向常量池中字符串是错误的?
构造函数根本是没有任何返回的。
thinking in java中提到:
new表达式确实返回了对象新建对象的引用,但构造器本身并没有任何返回值。