Stack heap dif

Stack heap dif
Stack.  用来在ram中存放数据的地方 This   lives   in   the   general   RAM   (random- access   memory)   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   compiler   must   know,   while   it   is   creating   the   program,   the   exact   size   and   lifetime   of   all   the   data   that   is   stored   on   the   stack,   because   it   must   generate   the   code   to   move   the   stack   pointer   up   and   down.   This   constraint   places   limits   on   the   flexibility   of   your   programs,   so   while   some   Java   storage   exists   on   the   stack   ?in   particular,   object   handles   ?Java   objects   are   not   placed   on   the   stack.    
   
  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   much   storage   it   needs   to   allocate   from   the   heap   or   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   to   create   an   object,   you   simply   write   the   code   to   create   it   using   new   and   the   storage   is   allocated   on   the   heap   when   that   code   is   executed.   And   of   course   there's   a   price   you   pay   for   this   flexibility:   it   takes   more   time   to   allocate   heap   storage.  
. 栈(stack)与堆(heap)都是Java用来在Ram中存放数据的地方。
2. 栈的优势是,存取速度比堆要快,仅次于直接位于CPU中的寄存器。但缺点是,存在栈中的数据大小与生存期必须是确定的,缺乏灵活性。另外,栈数据可以共享,详见第3点。堆的优势是可以动态地分配内存大小,生存期也不必事先告诉编译器,Java的垃圾收集器会自动收走这些不再使用的数据。但缺点是,由于要在运行时动态分配内存,存取速度较慢。
3. Java中的数据类型有两种。基本类型(primitive types), 共有8种,即int, short, long, byte, float, double, boolean, char。存在于栈中。另一种是包装类数据,如Integer, String, Double等将相应的基本数据类型包装起来的类。这些类数据全部存在于堆中.

大盘预测

你可能感兴趣的:(Stack heap dif)