Java Language FAQs

What is JDK 1.8? Is it same as Java SE 8.0/1.8?

JDK 1.8 or Java SE 1.8 or 8.0 all mean the same. They all refer to 1.8 version of Java Standard Edition.

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. The following lists features found in C++ and not found Java.

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. 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. Assuming srikanth.jar is in c:\java folder, the following command incldues srikanth.jar in classpath so that it can be used by Java.
set classpath=.;c:\java\srikanth.jar

How can we create an 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 whose objects 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
{
  private int rollno;
  private String name;

  // methods
}
During serialization all instance variables are serialized. If you want to leave out any instance variable from serialization then declare it with keyword transient. If you do not want any
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 contain 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, but abstract class can contain instance variables.

However, interface and abstract classes can not have objects. They can only have object references which point to object of implementing class of interface or sub class of abstract 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 Language { JAVA,C, VB, PYTHON}

for (Language lang : Language.values())
{
    System.out.println(lang);
}

How do I compute a Date that is 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 directory is set, 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.abs method then you can then use abs() without qualifications as shown below.
import static java.lang.Math.random;  // need fully qualified class name
import static java.lang.System.out;
class Test {
   public static void main(String args[]) {
       out.println( random() );  // calls static method
   }
}

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 annotation (from JDK 5.0) can be added to new method to indicate that you want to override a method in superclass. In case your method in subclass doesn't override a method in superclass, it is considered as a compilation error.

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

Which kind of error can stop jvm in java?

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.
    v.add( 10);  // O.K. from Java 5.0 onwards as boxing is applied.

How do I convert an int to String?

You can use String.valueOf(int) method to convert int to String.