Everything Is an Object

Where Storage Lives

Register

  • Exists different from other storage (inside the processor).
  • In java you don't have a direct control, nor do you see any evidence in your programs that registers even exist. While in C an C++, you will be allowed to suggest register allocation to the compiler.

Stack

  • lives in random access memory (RAM), has direct support from the processor via its stack pointer. 
  • Fast and efficient way, second only to registers. Instead of Java object, Java object references are placed on the stack.
  • 声明的引用放在stack里。

The heap

  • Also in RAM area, general-purpose pool of memory where all Java objects live.
  • Unlike stack, compiler doesn't need to know hong long that storage must stay on the heap.
  • It take more time to allocate and clean up heap storage than stack storage.
  • 创建的对象放在heap里。

Constant storage

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

Non-RAM storage

data lives completely outside a program, it can exist while the program is not running.

Primitive Types

The reason for primitive types in Java is that to create an object with new operator (especially a small, simple variable) isn't very efficient, because new places objects on the heap.

For primitive types, Java (falls back on C and C++) variable of primitive type will not hold a reference, it holds the value directly.


PrimitiveSizeWrapper
boolean-Boolean
char16 bitsCharacter
byte8 bitsByte
short16 bitsShort
int32 bitsInteger
long64 bitsLong
float32 bitsFloat
double64 bitsDouble
void-Void

Arrays in Java

A Java array is guaranteed to be initialized and cannot be accessed outside of its range. The range checking comes at the price of having a small amount of memory overhead on each array as well as verifying index at run time.

While using arrays in C and C++ is perilous, because arrays are only blocks of memory. If a program access the array outside of its memory block or uses the memory before initialization, there will be unpredictable results.

Scope of Objects

{
  String string = new String("a string");
} // end of scope

The reference vanishes at the end of the scope while the String object that s pointing to is still occupying memory (but there is no way to access the object after the end of the scope).

Java has a garbage collector, which looks at the objects that were created with new and figures out which ones are not being referenced anymore.

While in C++ you must make sure that the objects stay around for as long as you need them, you must also destroy the objects when you are done with them.

The Static Keyword

When create a class you re describing how objects of that class look like and how they will behave. You don't actually get an object until you create one using new keyword, at that point storage is allocated and methods become available. While there are two situation where this approach is not sufficient.

  • One is 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.

Using the static keyword means that particular field or method is not tied to any particular object instance of that class. There are two way to refer to a static member.

  • name it via an object reference.
  • name it via a class name.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值