Everything Is an Object

来源:互联网 发布:知秋歌曲 编辑:程序博客网 时间:2024/05/22 12:48

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.


PrimitiveSizeWrapperboolean-Booleanchar16 bitsCharacterbyte8 bitsByteshort16 bitsShortint32 bitsIntegerlong64 bitsLongfloat32 bitsFloatdouble64 bitsDoublevoid-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.

0 0