RSS


[ Pobierz całość w formacie PDF ]
.Where storage livesIt s useful to visualize some aspects of how things are laid out while theprogram is running, in particular how memory is arranged.There are sixdifferent places to store data:1.Registers.This is the fastest storage because it exists in a placedifferent from that of other storage: inside the processor.However,the number of registers is severely limited, so registers areallocated by the compiler according to its needs.You don t havedirect control, nor do you see any evidence in your programs thatregisters even exist.2.The stack.This lives in the general RAM (random-accessmemory) area, but has direct support from the processor via itsstack pointer.The stack pointer is moved down to create newChapter 2: Everything is an Object 103 memory and moved up to release that memory.This is anextremely fast and efficient way to allocate storage, second only toregisters.The Java compiler must know, while it is creating theprogram, the exact size and lifetime of all the data that is stored onthe stack, because it must generate the code to move the stackpointer up and down.This constraint places limits on the flexibilityof your programs, so while some Java storage exists on the stackin particular, object references Java objects themselves are notplaced on the stack.3.The heap.This is a general-purpose pool of memory (also in theRAM area) where all Java objects live.The nice thing about theheap is that, unlike the stack, the compiler doesn t need to knowhow much storage it needs to allocate from the heap or how longthat storage must stay on the heap.Thus, there s a great deal offlexibility in using storage on the heap.Whenever you need tocreate 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.Of course there s a price you pay for this flexibility: it takes moretime to allocate heap storage than it does to allocate stack storage(that is, if you even could create objects on the stack in Java, as youcan in C++).4.Static storage. Static is used here in the sense of  in a fixedlocation (although it s also in RAM).Static storage contains datathat is available for the entire time a program is running.You canuse the static keyword to specify that a particular element of anobject is static, but Java objects themselves are never placed instatic storage.5.Constant storage.Constant values are often placed directly inthe program code, which is safe since they can never change.Sometimes constants are cordoned off by themselves so that theycan be optionally placed in read-only memory (ROM).6.Non-RAM storage.If data lives completely outside a program itcan exist while the program is not running, outside the control ofthe program.The two primary examples of this are streamedobjects, in which objects are turned into streams of bytes, generally104 Thinking in Java www.BruceEckel.com to be sent to another machine, and persistent objects, in which theobjects are placed on disk so they will hold their state even whenthe program is terminated.The trick with these types of storage isturning the objects into something that can exist on the othermedium, and yet can be resurrected into a regular RAM-basedobject when necessary.Java provides support for lightweightpersistence, and future versions of Java might provide morecomplete solutions for persistence.Special case: primitive typesThere is a group of types that gets special treatment; you can think ofthese as  primitive types that you use quite often in your programming.The reason for the special treatment is that to create an object with newespecially a small, simple variable isn t very efficient because new placesobjects on the heap.For these types Java falls back on the approach takenby C and C++.That is, instead of creating the variable using new, an automatic variable is created that is not a reference.The variable holdsthe value, and it s placed on the stack so it s much more efficient [ Pobierz całość w formacie PDF ]
  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • nvs.xlx.pl