Everything Is an Object(Thinking In Java读书笔记)

 

  •   我们操作的是对象/类的引用(reference)。
  
  •   存储数据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).
寄存器,数量较少, java 中对程序员透明。( c c ++可以 suggest
  1. 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. 
堆栈 stack 虽然很快,但其存储的变量要求知道生命周期,降低了其灵活性。因此, java 的对象没有存储在堆栈。
  
  1. 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++).
堆栈具有更高的灵活性。
  1. 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.
存放常量,可以放在 ROM 中。
  
  1. 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.
持久层,将类以一定的格式存入硬盘等永久存储。

  • java中的原始类型保存在stack中,效率更高。
          Wrapper 可以使用一个类来表示一个基本类型(存储在heap中).
          eg:
              char c = ‘x’;
          Character ch = new Character(c); 

Primitive type
Size
Minimum
Maximum
Wrapper type
boolean
Boolean
char
16 bits
Unicode 0
Unicode 216- 1
Character
byte
8 bits
-128
+127
Byte
short
16 bits
-215
+215 -1
Short
int
32 bits
-231
+231 -1
Integer
long
64 bits
-263
+263 -1
Long
float
32 bits
IEEE754
IEEE754
Float
double
64 bits
IEEE754
IEEE754
Double
void
Void

          高精度Wrapper类 BigInteger and BigDecimal.  对应int和float。精度高,效率低。


  • 垃圾收集器garbage collector

     

    looks at all the objects that were created with new and figures out which ones are not being referenced anymore. 

  • 类中feild的基本变量会自动初始化,但是对于普通的基本变量声明不会初始化,而是给他一个随机的值。报错。
  • 函数参数的传递其实是传递的reference。
  • 两种情况使用static:
    • One is if you want to have only a single piece of storage for a particular field, regardless of how many objects of that class are created, or even if no objects are created.
    •  The other is if you need a method that isn’t associated with any particular object of this class. That is, you need a method that you can call even if no objects are created. 
  • 包java.lang.*自动import
  • 可以通过注释生成javadoc,效果很不错。
    • 所有生成javadoc的注释以/**开始(普通的以/*开始)
    • 只显示public和protect的域和函数。
    • 可以通过嵌入HTML标签来格式化javadoc

更多内容欢迎访问围观IT





  

 



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值