3、“封装器”(wrapper)类。事实上,Integer、Double等包装类和String有着同样的特性:不变类。 String str = "abc"的内部工作机制很有代表性,这里我以Boolean为例,说明同样的问题。 不变类的属性一般定义为final,一旦构造完毕就不能再改变了。 于是private final boolean value; 而且由于Boolean对象只有有限的两种状态:true和false,将这两个Boolean对象定义为命名常量: public static final Boolean TRUE = new Boolean(true); public static final Boolean FALSE = new Boolean(false); 这两个命名常量和字符串常量一样,在常数池中分配空间。 你注意到,Boolean.TRUE是一个引用,Boolean.FALSE是一个引用,而"abc"也是一个引用!(虽然我们经常把它混用地说成字符串对象。)
由于Boolean.TRUE是类变量(static)将静态地分配内存,所以需要很多Boolean对象时,并不需要用new表达式创建各个实例,完全可以共享这两个静态变量。其JDK中源代码是: public static Boolean valueOf(boolean b) { return (b ? TRUE : FALSE); } 基本数据(Primitive)类型的自动装箱(autoboxing)、拆箱(unboxing)是JSE 5.0提供的新功能。自动装箱与拆箱的功能事实上是编译器帮了你一点忙, Boolean b1 = 5>3; 等价于Boolean b1 = Boolean.valueOf(5>3); //优于Boolean b1 = new Boolean (5>3);
再举一例: public class A{ String x ="abc"; } public class B{ String x ="abc"; } public class User{ static void foo(){ A a = new A(); B b = new B(); System.out.println(a.x==b.x); } } 这里,System.out.println(a.x==b.x);也输出true。
String str1 = new String("abc"); 如何工作? 和 Boolean b4 = new Boolean(isTrue);////不宜使用 一样,被我称为“傻瓜的代码”,由于new而在heap中创建一个String对象,并将其引用赋值给str1, 因此System.out.println(str1=="abc");输出false。
如果问你:String x ="abc";创建了几个对象? 准确的答案是:0或者1个。如果存在"abc",则变量x持有"abc"这个引用,而不创建任何对象。 如果问你:String str1 = new String("abc"); 创建了几个对象? 准确的答案是:1或者2个。(至少1个在heap中)
2、 public static final Boolean TRUE = new Boolean(true); public static final Boolean FALSE = new Boolean(false); 这两个命名常量在逻辑上应该有存在空间的,至于它变成了CONSTANT_Integer 还是别的什么我不关心, 请问它在哪里存放?给一个语义上的解释。 ------ 关于引用:
1、在不同的Java虚拟机中,引用可能是作为指针实现的,其优点是速度快;可能是作为句柄实现的,其优点是容易在内存中调整对象的位置以减少内存碎片。据介绍Sun公司的一些Java虚拟机中,“指向对象的引用”是作为“指向句柄的指针”实现的。这里,引用是一个指向句柄的指针,而句柄本身由两个指针构成。 2、语义上,引用(reference)是引用变量的值、是new表达式的值、是对象的唯一识别号。 3、Each string literal is a reference (§4.3) to an instance (§4.3.1, §12.5) of class String (§4.3.3). JLS。 所以,"abc"是一个引用,是一种精确的说法(虽然我们经常说混用地它是一个String对象)
In previous versions of the Java virtual machine, such as the Classic VM, indirect handles are used to represent object references. While this makes relocating objects easier during garbage collection, it represents a significant performance bottleneck, because accesses to the instance variables of Java programming language objects require two levels of indirection.
In the Java HotSpot VM, no handles are used by Java code. Object references are implemented as direct pointers. This provides C-speed access to instance variables. When an object is relocated during memory reclamation, the garbage collector is responsible for finding and updating all references to the object in place. Two-Word Object Headers
The Java HotSpot VM uses a two machine-word object header, as opposed to three words in the Classic VM. Since the average Java object size is small, this has a significant impact on space consumption -- saving approximately eight percent in heap size for typical applications. The first header word contains information such as the identity hash code and GC status information. The second is a reference to the object's class. Only arrays have a third header field, for the array size. Reflective Data are Represented as Objects
Classes, methods, and other internal reflective data are represented directly as objects on the heap (although those objects may not be directly accessible to Java technology-based programs). This not only simplifies the VM internal object model, but also allows classes to be collected by the same garbage collector used for other Java programming language objects. Native Thread Support, Including Preemption and Multiprocessing
Per-thread method activation stacks are represented using the host operating system's stack and thread model. Both Java programming language methods and native methods share the same stack, allowing fast calls between the C and Java programming languages. Fully preemptive Java programming language threads are supported using the host operating system's thread scheduling mechanism.
A major advantage of using native OS threads and scheduling is the ability to take advantage of native OS multiprocessing support transparently. Because the Java HotSpot VM is designed to be insensitive to race conditions caused by preemption and/or multiprocessing while executing Java programming language code, the Java programming language threads will automatically take advantage of whatever scheduling and processor allocation policies the native OS provides.
------
------ 出发点是好的就行~~ 技术水平不够,看不懂各位的分析,帮忙顶顶帖吧 ------ Constants (class variables declared final) are not treated in the same way as non-final class variables. Every type that uses a final class variable gets a copy of the constant value in its own constant pool. As part of the constant pool, final class variables are stored in the method area--just like non-final class variables. But whereas non-final class variables are stored as part of the data for the type that declares them, final class variables are stored as part of the data for any type that uses them. This special treatment of constants is explained in more detail in Chapter 6, "The Java Class File."
2. 至于数值存放在哪里?只要记住“所有局部变量存放在栈中,所有对象存堆中”就可以了,具体实例可以据此引申出来。 a 局部变量如果是基本类型,则存栈中;如果是对象类型,实际上操作的是一个引用,引用存栈中,对象存堆中。 b 对象存堆中的含义就是对象的所有字段存堆中,所以对象的字段不管是基本数据类型和引用一律在堆中,引用指向的对象自然也在堆中。
------ i hava no idea about it ------ 恩,内容不错,值得学习交流 ------ 进来学习的 ------ 已经改成 烂文 了,呵呵! ------ 说的好 支持你 ------ 这些文章说的不都是一个意思么,发那么多篇干嘛 ------ "abc"是引用,不太好理解~ ------ ... ------ ldc "Hello World" ; push string "Hello World" onto stack ldc 10 ; push the integer 10 ldc 1.54 ; push the single-precision float 1.54
ldc <value> <value> is an int, a float, or a quoted (i.e. literal) string. ------ 好乱好乱啊。。。遇到一些String的问题,就一直看内存的问题,,,看到很多观点,都不知道哪个正确 ------ 新手 看不懂啊 ------
有没有人可以解释一下 引用 变量 和对象之间的关系啊
有点糊涂了!! ------ 学习学习!不是很会 ------
------ 随便看看,呵呵 ------ 继续顶 ------
------ 路过
有事看不完了
留个记号 ------ 我还以为这个帖子都沉了呢 ------ 迷糊学习中 ------ mark ------ 我都不懂喃~~~~你们好厉害!! ------ mark ------ kankanakna ------ 欣赏 ------