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

be accessed outside of its range. The range checking comes at the price of having a small <br/>
amount of memory overhead on each array as well as verifying the index at run time, but the <br/>
assumption is that the safety and increased productivity are worth the expense (and Java can <br/>
sometimes optimize these operations). <br/>
When you create an array of objects, you are really creating an array of references, and each <br/>
of those references is automatically initialized to a special value with its own keyword: null. <br/>
When Java sees null, it recognizes that the reference in question isn�t pointing to an object. <br/>
You must assign an object to each reference before you use it, and if you try to use a reference <br/>
that�s still null, the problem will be reported at run time. Thus, typical array errors are <br/>
prevented in Java. <br/>
You can also create an array of primitives. Again, the compiler guarantees initialization <br/>
because it zeroes the memory for that array. <br/>
Arrays will be covered in detail in later chapters. <br/>
You never need to <br/>
destroy an object <br/>
In most programming languages, the concept of the lifetime of a variable occupies a <br/>
significant portion of the programming effort. How long does the variable last? If you are <br/>
supposed to destroy it, when should you? Confusion over variable lifetimes can lead to a lot <br/>
of bugs, and this section shows how Java greatly simplifies the issue by doing all the cleanup <br/>
work for you. <br/>
Scoping <br/>
Most procedural languages have the concept of scope. This determines both the visibility and <br/>
lifetime of the names defined within that scope. In C, C++, and Java, scope is determined by <br/>
the placement of curly braces {}. So for example: <br/>
{ <br/>
int x = 12; <br/>
// Only x available <br/>
{ <br/>
int q = 96; <br/>
// Both x & q available <br/>
} <br/>
// Only x available <br/>
// q is "out of scope" <br/>
} <br/>
A variable defined within a scope is available only to the end of that scope. <br/>
Any text after a �//� to the end of a line is a comment. <br/>
Indentation makes Java code easier to read. Since Java is a free-form language, the extra <br/>
spaces, tabs, and carriage returns do not affect the resulting program. <br/>
You cannot do the following, even though it is legal in C and C++: <br/>
{ <br/>
int x = 12; <br/>
{ <br/>
Everything Is an Object 45�<br/>
int x = 96; // Illegal <br/>
} <br/>
} <br/>
The compiler will announce that the variable x has already been defined. Thus the C and C++ <br/>
ability to �hide� a variable in a larger scope is not allowed, because the Java designers <br/>
thought that it led to confusing programs. <br/>
Scope of objects <br/>
Java objects do not have the same lifetimes as primitives. When you create a Java object <br/>
using new, it hangs around past the end of the scope. Thus if you use: <br/>
{ <br/>
String s = new String("a string"); <br/>
} // End of scope <br/>
the reference s vanishes at the end of the scope. However, the String object that s was <br/>
pointing to is still occupying memory. In this bit of code, there is no way to access the object <br/>
after the end of the scope, because the only reference to it is out of scope. In later chapters <br/>
you�ll see how the reference to the object can be passed around and duplicated during the <br/>
course of a program. <br/>
It turns out that because objects created with new stay around for as long as you want them, <br/>
a whole slew of C++ programming problems simply vanish in Java. In C++ you must not only <br/>
make sure that the objects stay around for as long as you need them, you must also destroy <br/>
the objects when you�re done with them. <br/>
That brings up an interesting question. If Java leaves the objects lying around, what keeps <br/>
them from filling up memory and halting your program? This is exactly the kind of problem <br/>
that would occur in C++. This is where a bit of magic happens. Java has a garbage collector, <br/>
which looks at all the objects that were created with new and figures out which ones are not <br/>
being referenced anymore. Then it releases the memory for those objects, so the memory can <br/>
be used for new objects. This means that you never need to worry about reclaiming memory <br/>
yourself. You simply create objects, and when you no longer need them, they will go away by <br/>
themselves. This eliminates a certain class of programming problem: the so-called �memory <br/>
leak,� in which a programmer forgets to release memory. <br/>
Creating new data types: class <br/>
If everything is an object, what determines how a particular class of object looks and <br/>
behaves? Put another way, what establishes the type of an object? You might expect there to <br/>
be a keyword called �type,� and that certainly would have made sense. Historically, however, <br/>
most objectoriented languages have used the keyword class to mean �I�m about to tell you <br/>
what a new type of object looks like.� The class keyword (which is so common that it will not <br/>
usually be boldfaced throughout this book) is followed by the name of the new type. For <br/>
example: <br/>
class ATypeName { /* Class body goes here */ } <br/>
This introduces a new type, although the class body consists only of a comment (the stars and <br/>
slashes and what is inside, which will be discussed later in this chapter), so there is not too <br/>
much that you can do with it. However, you can create an object of this type using new: <br/>
ATypeName a = new ATypeName(); <br/>
46 Venkata Pilaka Venkata Pilaka <br/>

Comments

Popular posts from this blog

termux vnc viewer setup

../Settings.jpg

me.html