Saturday, February 17, 2007

When converting older Java software written for JDK1.0 to run on a newer JVM, are there any issues I should be aware of?

While there have been significant improvements to the Java platform over the years in the terms of graphics, networking, performance, and other API enhancements, older software can't take advantage of these so many developers find themselves faced with the task of upgrading software. Or, as often happens, a machine running an earlier JVM for some critical application is upgraded, and a newer release is installed. How can you guarantee that there won't be any problems with your older software? Or is it safe to assume that all versions of Java are guaranteed to be backwards compatible?

It's never safe to make any assumption when it comes to JVMs. There are just too many vendors, and too many versions to keep track of. Beyond vendor incompatibilities though, there are some issues to be careful of when moving from JDK1.02 to JDK1.1, and from JDK1.1 to Java 2.

Firstly, and this is the most important, be mindful of deprecated methods. A method that has been marked as deprecated means that Sun no longer recommends using it and that an alternate mechanism should be used if at all possible. Please see an earlier FAQ on this topic located here. Remember also that if you want your new programs to run on older JDK1.0 machines, you must continue to use the deprecated methods and not newer ones that JDK1.0 is not aware of.

Secondly, there are many minor changes to the JDK tools, and JVMs, that will cause problems with older software. Generally this happens because of minor flaws in the original source code of the application, and as the JVM has improved it has become more strict. As a general rule, you shouldn't have much of a problem, but in the documentation that ships for JDK1.2 there are quite a few areas identified where problems can arise with older code
Sun documentation on compatibility problems
For JDK1.1, please see http://java.sun.com/products/jdk/1.1/compatible/index.html

For JDK 1.2, which identifies numerous problems which Java 2 has with older code, please see
http://java.sun.com/products/jdk/1.2/compatibility.html



www.referjava.com

How do I run the garbage collector to free memory and delete unused objects?

First, you should be aware that the garbage collector works automatically, whether you request it to or not. Secondly, you cannot force the garbage collector to run.

That said, you can request the garbage collector to perform work, by invoking the System.gc() method. Once working, you may experience a short pause while the garbage collector does its work. That means you should do it at an appropriate time, such as before a big task or when the GUI is idle and waiting for input.


www.referjava.com

What is a Sun Certified Java Architect? How can I become one?

Developers who have achieved a Sun Certified Java Programmer, or Sun Certified Java Developer title may want to consider becoming a certified architect. Software architecture encompasses a broad range of technologies and techniques such as :-

object oriented analysis and design (OOAD)
Unified Modeling Language (UML)
Enterprise JavaBeans (EJBs)
large scale system design
Java 2 Enterprise Edition technologies such as CORBA/RMI
internationalization and security

www.referjava.com

What does a void return value mean? Which class is being returned?

You've probably heard of this method :-

public static void main(String args[])
before. It's invoked by the JVM when starting a Java application. Have you ever wondered why it didn't have a return statement at the end of the method? After all, don't all methods return a value?

It's possible, and actually quite common, to not return a value. If you're not going to return a value, however, you must mark your method as void. This tells the compiler there's no need to enforce a return value.


www.referjava.com

How I can I return a null value from an object constructor?

The simple answer is : no, you can't.

The explanation why is simple. You don't return any value whatsoever from an object constructor. The object has already been created - in the constructor you're just initializing the object's state. So there isn't any way to return a null value.

However, if you want to throw a spanner in the works, and stop someone using your object (which is usually the intent of returning a null value, or to indicate an error), why not throw a NullPointerException ?

public MyClass()
{
// Something might go wrong, so throw a null pointer exception
throw new NullPointerException() ;
}


www.referjava.com

How I can find the class of an object?

That's easy. Suppose you've got some objects stored in a collection, like a Vector or a List of some sort. You might want to check to see if an individual object belongs to a particular class. The instanceof operator is used in this case.

For example:-

if (obj instanceof MyClass)
{
MyClass.doSomething();
}
else
{
// handle object differently .......
}


www.referjava.com

How can I append an existing file in Java?

Appending data to a file is a fairly common task. You'll be surprised to know that there was no support for file appending in JDK1.02, and developers supporting that platform are forced to re-write the entire file to a temporary file, and then overwrite the original. As most users support either JDK1.1 or the Java 2 platform, you'll probably be able to use the following FileOutputStream constructor to append data:

public FileOutputStream(String name,
boolean append)
throws FileNotFoundException
Parameters:
name - the system-dependent file name
append - if true, then bytes will be written to the end of the file rather than the beginning
For example, to append the file 'autoexec.bat' to add a new path statement on a Wintel machine, you could do the following:

FileOutputStream appendedFile = new FileOutputStream
("c:\\autoexec.bat", true);


www.referjava.com

How do I kill a thread? The stop() method is deprecated in JDK1.2, so how do I shut it down?

The API documentation for JDK1.2 discusses an alternate mechanism for stopping threads, by having them continually poll a boolean flag to see if they should terminate of their own accord. This is an option, but if you have threads that become deadlocked or stall waiting for I/O, sometimes you'll have to kill them the hard way.

Why shouldn't you normally use the Thread.stop() method? Well, it is deprecated as of JDK1.2 because it can potentially leave the system in an unsafe state. If a thread
had a lock on an object (within a synchronized block), it might not release the object lock, causing problems at a later time. Note that this problem can affect older JVM implementations as well, JDK1.02 and JDK1.1 are not immune.

So while you shouldn't use Thread.stop() unless absolutely necessary, it is an acceptable way to kill a thread if it becomes stalled and you really need to shut it down fast.


www.referjava.com

What is an abstract class, and when should it be used?

Abstract classes are classes that contain one or more abstract methods. An abstract method is a method that is declared, but contains no implementation. Abstract classes may not be instantiated, and require subclasses to provide implementations for the abstract methods. Let's look at an example of an abstract class, and an abstract method.

Suppose we were modeling the behavior of animals, by creating a class hierachy that started with a base class called Animal. Animals are capable of doing different things like flying, digging and walking, but there are some common operations as well like eating and sleeping. Some common operations are performed by all animals, but in a different way as well. When an operation is performed in a different way, it is a good candidate for an abstract method (forcing subclasses to provide a custom implementation). Let's look at a very primitive Animal base class, which defines an abstract method for making a sound (such as a dog barking, a cow mooing, or a pig oinking).

public abstract Animal
{
public void eat(Food food)
{
// do something with food....
}

public void sleep(int hours)
{
try
{
// 1000 milliseconds * 60 seconds * 60 minutes * hours
Thread.sleep ( 1000 * 60 * 60 * hours);
}
catch (InterruptedException ie) { /* ignore */ }
}

public abstract void makeNoise();
}
Note that the abstract keyword is used to denote both an abstract method, and an abstract class. Now, any animal that wants to be instantiated (like a dog or cow) must implement the makeNoise method - otherwise it is impossible to create an instance of that class. Let's look at a Dog and Cow subclass that extends the Animal class.

public Dog extends Animal
{
public void makeNoise() { System.out.println ("Bark! Bark!"); }
}

public Cow extends Animal
{
public void makeNoise() { System.out.println ("Moo! Moo!"); }
}
Now you may be wondering why not declare an abstract class as an interface, and have the Dog and Cow implement the interface. Sure you could - but you'd also need to implement the eat and sleep methods. By using abstract classes, you can inherit the implementation of other (non-abstract) methods. You can't do that with interfaces - an interface cannot provide any method implementations.


www.referjava.com

What is a "magic number" in Java, and why does it sometimes go bad (referring to a bad magic number error when loading applets) ?

The class definition files (*.class) for Java applets are loaded over the network. Sometimes during the transmission of files, the connection may be aborted, or may be scrambled, causing class loading to fail. Sometimes when copying files over to a web server, they may become garbled or a disk error might occur. For this reason, special care is taken by the JVM and the class loader, to verify that classes are intact. One of the precautions is that every class definition contains at the beginning the magic number, a sequence of four bytes that identify a file as a Java class definition file.

For those curious to know what the magic number is, it is the hexadecimal number CAFEBABE, which is used by the class loader to see if a file is really a class definition file. Please don't ask me why it spells out cafebabe - my guess it was an attempt at humor.


www.referjava.com

I'm trying to compile a Java source file, and get the error message "bad command or filename". What did I do wrong?

This is a VERY frequently asked question. You need to add a path statement in your autoexec.bat file on windows to allow the javac.exe program to be found.

For example, if you installed java to the c:\java\ directory, you'd add the following to autoexec.bat

set path=%path%;c:\java\bin\

Hint: change the path to your installation dir (e.g. c:\jdk1.1.7\bin)
Remember to rerun the autoexec.bat file or reboot before trying to compile again, or the path setting will not be acted upon.


www.referjava.com