Java中数据的存储方式

以下内容摘自《Thinking in Java》第四版根据自己的理解进行了简单翻译。希望通过这样能够对Java的数据存储有更清楚的认识。


It's useful to visualize some aspects of how things are laid out while the program is running—in particular how memory is arranged. There are five different places to store data:

可视化程序运行过程中的一些方面,尤其是如何排列内存,是很有用的。存储数据有五个不同的地方:


1、Registers. This is the fastest storage because it exists in a place different from that of other storage: inside the processor. However, the number of registers is severely limited, so registers are allocated as they are needed. You don't have direct control, nor do you see any evidence in your programs that registers even exist (C & C++, on the other hand, allow you to suggest register allocation to the compiler).

1、寄存器。它是最快的存储区,因为它不同于处理器中的其它存储区。然而寄存器的数量非常有限,因此只在需要的时候才会被分配。你没有直接控制权,也不能在你的程序中看到任何寄存器存在的证据(C和C++,另一方面,允许你建议编译器分配寄存器)。


2、The stack. This lives in the general random-access memory (RAM) area, but has direct support from the processor via its stack pointer. The stack pointer is moved down to create new memory and moved up to release that memory. This is an extremely fast and efficient way to allocate storage, second only to registers. The Java system must know, while it is creating the program, the exact lifetime of all the items that are stored on the stack. This constraint places limits on the flexibility of your programs, so while some Java storage exists on the stack—in particular, object references—Java objects themselves are not placed on the stack.

2、。它存在于一般的随机存取存储器(RAM)区域,但是通过它的栈指针对处理器有直接支持。栈指针下移创建内存空间,上移则释放内存空间。这是一种非常快速、高效的分配存储区的方式,仅次于寄存器。创建程序时,Java系统必须知道所有存储在栈中的东西的确切寿命。这种约束限制了你的程序的灵活性,因此一些Java数据存储在栈中,特别是对象的引用,而Java对象本身并不存放在栈中


3、The heap. This is a general-purpose pool of memory (also in the RAM area) where all Java objects live. The nice thing about the heap is that, unlike the stack, the compiler doesn't need to know how long that storage must stay on the heap. Thus, there's a great deal of flexibility in using storage on the heap. Whenever you need an object, you simply write the code to create it by using new, and the storage is allocated on the heap when that code is executed. Of course there's a price you pay for this flexibility: It may take more time to allocate and clean up heap storage than stack storage (if you even could create objects on the stack in Java, as you can in C++).

3、。这是一个通用的内存池(也在RAM区域),所有Java对象都在其中。 堆的好处是,与栈不同的是,编译器不需要知道存储器必须在堆上停留多长时间。 因此,在堆上使用存储有很大的灵活性。 无论何时需要一个对象,您只需编写使用new来创建它的代码,并在执行该代码时在堆上分配存储。当然,您需要为灵活性付出一定的代价:与堆栈存储相比,分配和清理堆存储可能需要更多时间(如果您甚至可以在Java中的堆栈上创建对象,就像在C ++中一样)。


4、Constant storage. Constant values are often placed directly in the program code, which is safe since they can never change. Sometimes constants are cordoned off by themselves so that they can be optionally placed in read-only memory (ROM), in embedded systems.

4、常量存储。常量经常被直接放在程序代码中,这是安全的,因为它们永远不会改变。有时常量被自身封锁,以便它们可以被选择性的放在嵌入式系统的只读存储器(ROM)中。


5、Non-RAM storage. If data lives completely outside a program, it can exist while the program is not running, outside the control of the program. The two primary examples of this are streamed objects, in which objects are turned into streams of bytes, generally to be sent to another machine, and persistent objects, in which the objects are placed on disk so they will hold their state even when the program is terminated. The trick with these types of storage is turning the objects into something that can exist on the other medium, and yet can be resurrected into a regular RAM-based object when necessary. Java provides support for lightweight persistence, and mechanisms such as JDBC and Hibernate provide more sophisticated support for storing and retrieving object information in databases.

5、非RAM存储。如果数据完全处于程序之外,程序不运行就可以在程序的控制之外存在。主要的两个例子是:流对象,其中的对象被转换成了字节流,通常被发送到另一台机器;持久化对象,其中的对象被放置在磁盘上,因此当程序终止时它们将保持其状态。这些类型存储的技巧是将对象转换为可以存在于其他介质上的对象,并且在必要时可以复制到基于常规RAM的对象中。​Java提供对轻量级持久化的支持,以及诸如JDBC、Hibernate之类的机制提供的为数据库中存储和检索对象信息提供了更复杂的支持。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值