Fri, May 31, 2019 4:51:37 PM

Everything Is an Object 43�<br/>
3. The heap. This is a general-purpose pool of memory (also in the RAM area) where all <br/>
Java objects live. The nice thing about the heap is that, unlike the stack, the compiler <br/>
doesn�t need to know how long that storage must stay on the heap. Thus, there�s a <br/>
great deal of flexibility in using storage on the heap. Whenever you need an object, you <br/>
simply write the code to create it by using new, and the storage is allocated on the <br/>
heap when that code is executed. Of course there�s a price you pay for this flexibility: It <br/>
may take more time to allocate and clean up heap storage than stack storage (if you <br/>
even could create objects on the stack in Java, as you can in C++). <br/>
<br/>
4. Constant storage. Constant values are often placed directly in the program code, <br/>
which is safe since they can never change. Sometimes constants are cordoned off by <br/>
themselves so that they can be optionally placed in read-only memory (ROM), in <br/>
embedded systems.2 <br/>
<br/>
5. Non-RAM storage. If data lives completely outside a program, it can exist while the <br/>
program is not running, outside the control of the program. The two primary <br/>
examples of this are streamed objects, in which objects are turned into streams of <br/>
bytes, generally to be sent to another machine, and persistent objects, in which the <br/>
objects are placed on disk so they will hold their state even when the program is <br/>
terminated. The trick with these types of storage is turning the objects into something <br/>
that can exist on the other medium, and yet can be resurrected into a regular RAM-<br/>
based object when necessary. Java provides support for lightweight persistence, and <br/>
mechanisms such as JDBC and Hibernate provide more sophisticated support for <br/>
storing and retrieving object information in databases. <br/>
Special case: primitive types <br/>
One group of types, which you�ll use quite often in your programming, gets special treatment. <br/>
You can think of these as �primitive� types. The reason for the special treatment is that to <br/>
create an object with new�especially a small, simple variable�isn�t very efficient, because <br/>
new places objects on the heap. For these types Java falls back on the approach taken by C <br/>
and C++. That is, instead of creating the variable by using new, an �automatic� variable is <br/>
created that is not a reference. The variable holds the value directly, and it�s placed on the <br/>
stack, so it�s much more efficient. <br/>
Java determines the size of each primitive type. These sizes don�t change from one machine <br/>
architecture to another as they do in most languages. This size invariance is one reason Java <br/>
programs are more portable than programs in most other languages. <br/>
Primitive <br/>
type <br/>
Size Minimum Maximum Wrapper type <br/>
boolean � � � Boolean <br/>
char 16 bits Unicode 0 Unicode 216- 1 Character <br/>
byte 8 bits -128 +127 Byte <br/>
short 16 bits -215 +215-1 Short <br/>
int 32 bits -231 +231-1 Integer <br/>
long 64 bits -263 +263-1 Long <br/>
float 32 bits IEEE754 IEEE754 Float <br/>
double 64 bits IEEE754 IEEE754 Double <br/>
void � � � Void <br/>
������������������������������������������������������������<br/>
2 An example of this is the string pool. All literal strings and string-valued constant expressions are interned automatically <br/>
and put into special static storage. <br/>
All numeric types are signed, so don�t look for unsigned types. <br/>
The size of the boolean type is not explicitly specified; it is only defined to be able to take <br/>
the literal values true or false. <br/>
The �wrapper� classes for the primitive data types allow you to make a non-primitive object <br/>
on the heap to represent that primitive type. For example: <br/>
char c = �x�; <br/>
Character ch = new Character(c); <br/>
Or you could also use: <br/>
Character ch = new Character(�x�); <br/>
Java SE5 autoboxing will automatically convert from a primitive to a wrapper type: <br/>
Character ch = �x�; <br/>
and back: <br/>
char c = ch; <br/>
The reasons for wrapping primitives will be shown in a later chapter. <br/>
High-precision numbers <br/>
Java includes two classes for performing high-precision arithmetic: BigInteger and <br/>
BigDecimal. Although these approximately fit into the same category as the �wrapper� <br/>
classes, neither one has a primitive analogue. <br/>
Both classes have methods that provide analogues for the operations that you perform on <br/>
primitive types. That is, you can do anything with a BigInteger or BigDecimal that you <br/>
can with an int or float, it�s just that you must use method calls instead of operators. Also, <br/>
since there�s more involved, the operations will be slower. You�re exchanging speed for <br/>
accuracy. <br/>
BigInteger supports arbitrary-precision integers. This means that you can accurately <br/>
represent integral values of any size without losing any information during operations. <br/>
BigDecimal is for arbitrary-precision fixed-point numbers; you can use these for accurate <br/>
monetary calculations, for example. <br/>
Consult the JDK documentation for details about the constructors and methods you can call <br/>
for these two classes. <br/>
Arrays in Java <br/>
Virtually all programming languages support some kind of arrays. Using arrays in C and C++ <br/>
is perilous because those arrays are only blocks of memory. If a program accesses the array <br/>
outside of its memory block or uses the memory before initialization (common programming <br/>
errors), there will be unpredictable results. <br/>
One of the primary goals of Java is safety, so many of the problems that plague programmers <br/>
in C and C++ are not repeated in Java. A Java array is guaranteed to be initialized and cannot <br/>
44 Venkata Pilaka Venkata Pilaka <br/>

Comments

Popular posts from this blog

termux vnc viewer setup

../Settings.jpg

me.html