CS 24L Object-Oriented Programming
Some Important Java Facts
a. Case Sensitivity
Like its C++ counterpart, Java is case sensitive. For example, astring
and aString are considered to be two distinct
identifiers. For those with C++ experienced this should not cause any problems,
however, some tips that can ease the situation are provided below.
-
All Java keywords are in lower case. For example, while, for
and class.
-
All the classes provided with the Java SDK begin with an uppercase letter.
b. Naming Conventions
The Java SDK class libraries generally follow the naming convention given
below.
-
All method names begin with a lowercase letter. For example, setX().
-
All field names begin with a lowercase letter. Underscores can also be
used. For example, new_x.
-
Constants are in uppercase letters and can also use underscores. For example,
MAX_VALUE.
-
All class names start with an uppercase letter. For example, String.
By following a particular naming convention, the readability of the code
is improved
c. Garbage Collection
To create an instance of a class, the new
operator is used. However, there is no need for a delete
operator since Java automatically removes objects that are not being referenced.
This is known as Garbage Collection. This has the following advantages
and disadvantages.
Advantages
-
Reduces the possibility of memory leaks since memory is freed as needed.
A memory leak occurs when unused memory is not released, resulting in the
eventual consumption of all of the available memory.
-
An object that is being referenced cannot be deleted. This sort of deletion
occurs mainly with pointers where it is possible to delete an object that
is still being referenced by a pointer (resulting in memory corruption).
Disadvantages
-
For some implementations of the JVM, garbage collection is considered one
of the greatest bottlenecks in the speed of execution. The main cause is
that each time the garbage collector is activated, some aspects of the
interpreter may be suspended.
-
Makes the low-level aspect of systems programming very difficult since
control of the memory allocation/de-allocation process is removed.
d. No Pointers
Java has no pointers but instead uses references. A reference provides
access only to the functionality of the object. The programmer does not
have direct access to memory removing the dangers of pointer manipulation.
For example:
-
Pointers remove the concept of private data since they can provide direct
access to that data.
-
In some cases, integer values can be converted to pointer addresses which
can result in incorrect addresses being formed and hence memory corruption.
-
Arrays can be directly accessed by pointers. This can result in the program
writing past the end of the array.
Interestingly, the lack of pointers can be a hindrance in specific cases.
For example, imagine trying to create a memory manager for an operating
system without pointers.
e. No Global Variables
In Java, the global name space is the class hierarchy and so, one cannot
create a variable outside of a class. This removes the possibility of side
effects occurring on a system-wide basis due to some change in the state
of a global variable. For example, it is extremely difficult (if not impossible)
to ensure that a global variable is changed in a consistent and controlled
manner. Java does allow a modified form of the global variable called static
variables.
Copyright © 1999-2003, Colin Depradine. All
rights reserved.