Java Language FAQs

What is JDK 1.5? Is it same as J2SE 5.0/1.5? Also I heard about Tiger. What is Tiger?
JDK 1.5 or J2SE 1.5 or 5.0 or Tiger all mean the same. They all refer to the latest version of Java.

Tiger is the code name of Java 5.0 during its development. Then Sun gave the name J2SE (Java2 Standard Edition) 1.5. But compared with J2SE 1.4, J2SE 1.5 has some very important changes to Java language. So Sun preferred to call it as J2SE 5.0 to signify major changes to language.

The new name gave by Sun is Java SE 5.0 (they are dropping 2 from name as it refers to Java2 and now Java2 is not appopriate any more).

What is the difference between Java and C++?
Java and C++ are two different object oriented programming languages. Both of them are widely used.

C++ is a powerful and flexible language used mainly in system programming.

Java is a roubust and platform independent language which is used to develop business and internet applications.

A lot of features of C++ are not found in Java and vice-versa.

C++ features not in Java

Java features not in C++

What is a JAR file?
JAR file is similar to a .LIB file in C and C++.
JAR (Java Archive) file contains a collection of packages and classes. It is used to distribute packages and classes.
Jar file is created using JAR.EXE, which is found in BIN directory of JDK. For example the following command creates a jar file with the name srikanth.jar and contains all .class files in the current directory.
jar cvf srikanth.jar .
To use classes of a jar file one has to keep jar file in CLASSPATH.

How can we create a executable jar file?
An executable jar file is the one with a manifest file specifying which class is to start when user executes it (double clicks it in explorer) or invokes with -jar flag of java ( java -jar test.jar).

Assume we have First.class and Second.class to be placed in a jar file test.jar. We want First.class to start execution when user runs test.jar.

Create a manifest file with the name test.mf as follows:

Main-Class: First
Note, we should not mention the .class extesion for the file in Manifest file.

Create test.jar file as follows:

jar cfm test.jar test.mf  First.class  Second.class
You can now either double click .jar file in explorer to start running First.class of test.jar or you can run it with java as follows:
java -jar test.jar

What is Serializable interface? How do we use?
Serializable interface is an empty interface present in java.io package. It is implemented by classes that are to be serialized. Serialization is the process of converting an object into a stream of bytes.

An object must be serializable if it is to be written to disk or transfered in distributed applications.

The following example shows class Student implementing this interface to indicate to Java that its object can be serialized by Java.


public class  Student implements java.io.Serializable
{
  // body of class
}

What is the base/super class for all classes in Java?
Every class that is not extending another class is implicitly extending Object class in Java. So java.lang.Object class is the super most class of all classes in Java.

What is the difference between interface and abstract class
An interface contains only declaration of methods and can not have any body for methods.
Whereas an abstract class can contains abstract methods without any body but it can also have non-abstract methods with body (which is not possible with interfaces).

Interfaces cannot contain instance variables
Abstract class can contains instance variables.

However, interface and abstract classes can not have objects. They can only have object references which point to object of implementing class or sub class.

What is a null interface in JAVA? What does it mean? Are there any null interfaces in JAVA?
A null interface is an interface without any methods. Null interfaces are used to inform Java regarding what it can do with a class. For example, Serializable interface informs Java that objects of the implementing class can be serialized.

The following are important null interfaces in Java.

How do I access environment variables?
Starting with JDK 1.5, the getenv() method of System class returns an environment variable setting.

How do you clear a system property?
Starting with JDK 1.5, you can use the clearProperty method of System class.

How do I loop through a set of enum values in 1.5+?
public enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES }

for (Suit suit : Suit.values())
{
    System.out.println(suit);
}

How do I compute a Date 24 hours from now?
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_YEAR, 1);
Date d = cal.getTime();
How do I change the starting directory for running a command with ProcessBuilder?
Use the directory() method of ProcessBuilder to change the starting / working directory. Once setting the directory, start() the sub-process.
   ProcessBuilder processBuilder = new ProcessBuilder(
                                       command, arg1, arg2);
   processBuilder.directory("DirectoryLocation");
   Process p = processBuilder.start();

What's the difference between Runtime.exec() and ProcessBuilder?
JDK 5.0 added a new class, ProcessBuilder to do the equivalent of the exec() method of Runtime. In addition to running commands in forked off subprocesses, you can adjust environment variables and change the starting working directory with ProcessBuilder.

What's a static import?
It lets you avoid qualifying static members with class names. If you import static java.lang.Math.PI; you can then use PI without qualifications.

Sometimes when I try to override a method, I get the signature wrong. The program still compiles, but the code never runs. How can I make sure my method actually overrides the method of the superclass?
The @Override

Can Java use attributes?
Yes. In Java, however, these are called annotations. They were added in version 5.0.

What is "Tiger"?
"Tiger" was the code name for Java 2 Platform Standard Edition (J2SE) 5.0 that released on September 29th, 2004.

This major release included over a dozen JSRs and nearly 100 other significant updates from the JCP. You should note that this can also be considered developer release 1.5.0.

This release included features such as the addition of generics, an enhanced for loop, enumerated types, static imports, automatic boxing of primitive types and much more.

Tiger is followed by the "Mustang", which will be J2SE 6.0 (1.6.0), which in turn will be followed by "Dolphin", which will be J2SE 7.0 (1.7.0).

What is "Mustang"?
Mustang is the code name for J2SE 6.0. (1.6.0)

The Mustang release of J2SE is expected to include a number of key changes to J2SE including changes to the release model of the JDK, XML and Web Services, Transparency, Ease-of-Development improvements, better compatibility, an updated to the API for accessing JDBC, and the addition of a scripting engine. There will a refined GUI as well as graphics performance improvements.

Which kind off error can stop jvm in java?
Actually, the JVM will exit if unchecked-exceptions (for example RuntimeException etc) are not caught.

How can i find out which JAR file or Directory from where my class was loaded?
class Test {
  public static void main (String args[]) {
	System.out.println("Loaded Test from:" + Test.class.getProtectionDomain().getCodeSource().getLocation());
  }
}
Why the following code doesn't work?
float f;

   f = 10.50;
The above code doesn't work because, by default all floating point constants in Java are treated as double, the float variable f cannot take a double value. The remedy is to convert it using type casting ( (float)10.50) or declare variable f as double.

Can I use standard data types like int, float and char with Vector class?
No. You cannot use them as Vector deals with objects. Standard data types are not objects. However, you can use wrapper classes to convert standard data types to object so that they can be used with Vector.
    Vector v = new Vector();

    // v.add(10);  -- cannot be done
    v.add ( new Integer(10));   // int 10 is converted to object of type Integer class.

How do i convert an int to String?
You can use String.valueOf(int) method to convert int to String.