Search This Blog

Is finalize( ) similar to a destructor?

I know that C++ defines things called destructors, which are automatically executed when an object is destroyed. Is finalize( ) similar to a destructor?

Java does not have destructors. Although it is true that the finalize( ) method approximates the function of a destructor, it is not the same. For example, a C++ destructor is always called just before an object goes out of scope, but you can’t know when finalize( ) will be called for any specific object. Frankly, because of Java’s use of garbage colle ction,there is little need for a destructor.

Java :Question with Answer

You say that once created, String objects are immutable. I understand that, from a
practical point of view, this is not a serious restriction, but what if I want to create a string that can be changed?


You’re in luck. Java offers a class called StringBuffer, which creates string objects
that can be changed. For example, in addition to the charAt( ) method, which obtains
the character at a specific location, StringBuffer defines setCharAt( ), which sets a
character within the string. However, for most purposes you will want to use String,
not StringBuffer.

Why does String define the equals( ) method? Can’t I just use ==?

The equals( ) method compares the character sequences of two String objects for
equality. Applying the == to two String references simply determines whether the two
references refer to the same object.

When should I make an instance variable private?

There are no hard and fast rules, but here are two general principles. If an instance
variable is to be used only by methods defined within its class, then it should be made private. If an instance variable must be within certain bounds, then it should be private and made available only through accessor methods. This way, you can prevent invalid values from being assigned.

The javadoc Tags

Tag Meaning
@author Identifies the author of a class.
@deprecated Specifies that a class or member is deprecated.
{@docRoot} Specifies the path to the root directory of the current documentation. (Added by Java
2, version 1.3.)
@exception Identifies an exception thrown by a method.
{@inheritDoc} Inherits a comment from the immediate superclass. (Added by Java 2, version 1.4, butnot currently implemented.)
{@link} Inserts an in-line link to another topic.
{@linkplain} Inserts an in-line link to another topic, but the link is displayed in a plain-text font.(Added by Java 2, version 1.4.)
@param Documents a method’s parameter.
@return Documents a method’s return value.
@see Specifies a link to another topic.
@serial Documents a default serializable field.
@serialData Documents the data written by the writeObject( ) or writeExternal( ) methods.
@serialField Documents an ObjectStreamField component.
@since States the release when a specific change was introduced.
@throws Same as @exception.
{@value} Displays the value of a constant, which must be a static field. (Added by Java 2,version 1.4.)
@version Specifies the version of a class.

Java : Fundamental Question and Answer

To address the issues of portability and security, why was it necessary to create a
new computer language such as Java; couldn’t a language like C++ be adapted? In
other words, couldn’t a C++ compiler that outputs bytecode be created?


While it would be possible for a C++ compiler to generate bytecode rather than
executable code, C++ has features that discourage its use for the creation of
applets—the most important feature being C++’s support for pointers. A pointer is the
address of some object stored in memory. Using a pointer, it would be possible to access resources outside the program itself, resulting in a security breach. Java does not support pointers, thus eliminating this problem

Java : Fundamental Question and Answer

Does the use of a code block introduce any run-time inefficiencies? In other words,
does Java actually execute the { and }?



No. Code blocks do not add any overhead whatsoever. In fact, because of their ability
to simplify the coding of certain algorithms, their use generally increases speed and
efficiency. Also, the { and } exist only in your program’s source code. Java does not,per se, execute the { or }.

Java : Fundamental Question and Answer

You state that object-oriented programming is an effective way to manage large programs. However, it seems that it might add substantial overhead to relatively
small ones. Since you say that all Java programs are, to some extent, object-oriented,does this impose a penalty for smaller programs?



No. As you will see, for small programs, Java’s object-oriented features are nearly
transparent. Although it is true that Java follows a strict object model, you have
wide latitude as to the degree to which you employ it. For smaller programs, their
“object-orientedness” is barely perceptible. As your programs grow, you will integrate
more object-oriented features effortlessly