Java Language Issues

W
Shared by: honeytech
Categories
Tags
-
Stats
views:
314
posted:
11/12/2007
language:
English
pages:
0
Document Sample
scope of work template
							Java Language Issues
6.1: What does it mean that a class or member is final?
A final class can no longer be subclassed. Mostly this is done for security reasons with basic classes like String and Integer. It also allows the compiler to make some optimizations, and makes thread safety a little easier to achieve. Methods may be declared final as well. This means they may not be overridden in a subclass. Fields can be declared final, too. However, this has a completely different meaning. A final field cannot be changed after it's initialized, and it must include an initializer statement where it's declared. For example,
public final double c = 2.998;

It's also possible to make a static field final to get the effect of C++'s const statement or some uses of C's #define, e.g.
public static final double c = 2.998;

6.2: What does it mean that a method or field is "static"?
Static variables and methods are instantiated only once per class. In other words they are class variables, not instance variables. If you change the value of a static variable in a particular object, the value of that variable changes for all instances of that class. Static methods can be referenced with the name of the class rather than the name of a particular object of the class (though that works too). That's how library methods like System.out.println() work. out is a static field in the java.lang.System class.

6.3: What does it mean that a method or class is abstract?
An abstract class cannot be instantiated. Only its subclasses can be instantiated. You indicate that a class is abstract with the abstract keyword like this:
public abstract class Container extends Component {

Abstract classes may contain abstract methods. A method declared abstract is not actually implemented in the current class. It exists only to be overridden in subclasses. It has no body. For example,
public abstract float price();

Abstract methods may only be included in abstract classes. However, an abstract class is not required to have any abstract methods, though most of them do. Each subclass of an abstract class must override the abstract methods of its superclasses or itself be declared abstract. For more details, see section 8.1.2.1 of the Java Language Specification.

6.4: What's an interface?
An interface is an idea taken from Objective C. It describes the public methods that a class implements and their calling conventions without saying anything about how those methods are implemented. It is the responsibility of each class that implements an interface to provide code to handle the cases where the methods of the interface are called. For example suppose you're writing an inventory database. The inventory may include many different items of many different types and classes. However each item in the warehouse needs to be able to tell you its price. Normally you would implement this by having each class extend a common superclass. However that's not always convenient. Instead you can declare an interface called Price with a price() method like this:
public interface Price { public float price(); }

Any class which implements the Price interface must contain a method with the signature public float price(). The code of the price() method is included separately in each separate class which implements Price, not in the Price interface itself. Different classes in your warehouse can each implement the Price interface like this:
public class Monopoly extends BoardGame implements Price { // other methods public float price() { return 14.95; } }

When other code is passed an object, it can test whether the object implements Price with the instanceof operator. For example,
if (o instanceof Price) System.out.println("Subtotal is " + o.price());

In fact, interfaces can be used to tag objects. The java.rmi.Remote interface declares no methods. Its sole purpose is to indicate that an object is a remote object. In general, subinterfaces of java.rmi.Remote will declare remote methods, however. For example,
public interface Hello extends java.rmi.Remote { public String sayHello(); } public class HelloImpl extends UnicastRemoteServer implements Hello { public String sayHello() { return "Hello"; } }

For more information about the java.rmi package, see http://chatsubo.javasoft.com/current/rmi/index.html or Chapter 14 of my book, Java Network Programming, from O'Reilly & Associates.

6.5: Why doesn't Java include insert your favorite feature here?
The Java language has been extensively debated and argued about within Sun. Almost every language construct of existing languages has already been considered for inclusion in Java. While there may still be room for addition, it is very unlikely that your pet feature will be added to the language spec if it isn't already there. In a couple of years parameterized types (i.e. templates) may be added to the language. Otherwise the spec is pretty much frozen except for minor changes and bug fixes. Extensions are planned for the class library though. In particular Sun is working on extensions for 3D, multimedia, telephony, and improved graphics.

6.6: Is Java CORBA compliant?
Not yet. However work is underway for a Java ORB and IDL. http://www.javasoft.com/products/jdk/idl/index.html.

6.7: Can I cast an int to an Integer? a float to a Float?
No, you cannot promote a base data type like int or float to an object such as an Integer or a Float. However the proper way to do this isn't very hard. Instead do
int x = 5; myInteger = new Integer(x);

6.8: How do I version a class?

There is no support for versioning classes in Java 1.0. However in Java 1.1 the serialver tool provides a serialVersionUID for one or more classes you can add to your class as a field. This is used in object serialization.

6.9: Why isn't there operator overloading?
Because C++ has proven by example that operator overloading makes code almost impossible to maintain. In fact there very nearly wasn't even method overloading in Java, but it was thought that this was too useful for some very basic methods like print(). Note that some of the classes like DataOutputStream have unoverloaded methods like writeInt() and writeByte().

6.10: Does Java have pointers?
No, no, a thousand times no. Java does not have pointers, no way, no how, the daily email I get from people who think differently not withstanding. Java does have references. A reference is an abstract identifier for an object. It is not a pointer. A reference tags a particular object with a name in the Java virtual machine so that the programmer may refer to it. How exactly the virtual machine implements references at the level of machine code is VM-dependent and completely hidden from the programmer in any case. Most VMs including Sun's use handles, not pointers. A handle is a pointer to a pointer. At the level of machine code in the CPU a reference is an address in memory where the address of the object is stored. This way the objects can be moved around in memory and only the master pointer needs to be updated rather than all references to the object. This is completely hidden from the Java programmer, though. Only the implementer of the virtual machine needs to worry about it. Indeed, this is not the only way references can be implemented. Microsoft's VM actually does use pointers rather than handles. Other schemes are possible.

6.11: Does Java pass method arguments by value or by reference?
Java passes all arguments by value, not by reference. However this is one of the few places where the distinction between an object and a reference to an object becomes important. Object and array variables in Java are really references to the object or array. This can make it look like an object is passed by reference if you only modify the fields of the object or array, but do not change the reference itself. For example, consider this program:
import java.awt.Point; class changePoint { public static void main(String args[]) { Point p1 = new Point(0, 0); changePoint(p1); System.out.println(p1);

} static void changePoint(Point p) { p.x = 38; p.y = 97; } }

It prints:
java.awt.Point[x=38,y=97]

Therefore the point has been changed. However the reference, which is what was really passed, has not been changed. To see that consider the following program.
import java.awt.Point; class dontChangePoint { public static void main(String args[]) { Point p1 = new Point(0, 0); dontChangePoint(p1); System.out.println(p1); } static void dontChangePoint(Point p) { p = new Point(38, 97); } }

It prints:
java.awt.Point[x=0,y=0]

What happened in this example was that a copy of the reference p1 was passed to the dontChangePoint() method. A new Point object was then assigned to that copy. However this did not change the old reference in the main method. In the previous example the reference p in the changePoint() method and p1 in the main() method both referred to the same object. In this example p and p1 refer to different objects after the new Point is assigned to p.

6.12: Are there parameterized types (templates)?
Not in Java 1.0 or 1.1. However this is being seriously considered for future versions.

6.13: How does garbage collection work?
Current implementations of Java use a mark and sweep garbage collector. Reference counting is not used. Thus circular linked lists do not lead to memory leaks. It is

theoretically possible that future versions of Java will use some other garbage collection algorithm.


						
Related docs