Java Interview Questions

Always update...

JVM is plantform dependent?

Is the JVM (Java Virtual Machine) platform dependent or platform independent? What is the advantage of using the JVM, and having Java be a translated language?

JVM translates bytecode into machine language Every Java program is first compiled into an intermediate language called Java bytecode. The JVM is used primarily for 2 things: the first is to translate the bytecode into the machine language for a particular computer, and the second thing is to actually execute the corresponding machine-language instructions as well. The JVM and bytecode combined give Java its status as a "portable" language – this is because Java bytecode can be transferred from one machine to another.

Machine language is OS dependent Since the JVM must translate the bytecode into machine language, and since the machine language depends on the operating system being used, it is clear that the JVM is platform (operating system) dependent – in other words, the JVM is not platform independent.

The JVM is not platform independent The key here is that the JVM depends on the operating system – so if you are running Mac OS X you will have a different JVM than if you are running Windows or some other operating system.

Overloading & Overriding

In Java, what’s the difference between method overloading and method overriding?

Overloading: Method overloading in Java occurs when two or more methods in the same class have the exact same name but different parameters (remember that method parameters accept values passed into the method). However, method overloading is a compile-time phenomenon. Can be overloading:

1.) The number of parameters is different for the methods.
2.) The parameter types are different (like
changing a parameter that was a float to an int).

Not overloading

1. Just changing the return type of the method. (Compiler Error)
2. Changing just the name of the method parameters, but
not changing the parameter types.

Overriding: [根本也记不住,其实我的方法是小朋友骑在爸爸肩膀上,他们主体是一样的,不会变的,即方法参数返回值不变,但内容变了。(:зゝ∠)] Overriding means that a method inherited from a parent class will be changed. But, when overriding a method everything remains exactly the same except the method definition – basically what the method does is changed slightly to fit in with the needs of the child class. But, the method name, the number and types of parameters, and the return type will all remain the same. Method overriding is a run-time phenomenon that is the driving force behind polymorphism.

Private Constructor

What’s the point of having a private constructor?

Defining a constructor with the private modifier says that only the native class (as in the class in which the private constructor is defined) is allowed to create an instance of the class, and no other caller is permitted to do so.

There are two possible reasons why one would want to use a private constructor – the first is that you don’t want any objects of your class to be created at all, and the second is that you only want objects to be created internally – as in only created in your class.

A singleton is a design pattern that allows only one instance of your class to be created, and this can be accomplished by using a private constructor.

An object and a class

In Java, what’s the difference between an object and a class?

Shortly: An object is an instance of a class. Objects have a lifespan but classes do not.

Java platform

What is the main difference between Java platform and other platforms?

The Java platform differs from most other platforms in the sense that it's a software-based platform that runs on top of other hardware-based platforms.It has two components:

  • Runtime Environment
  • API(Application Programming Interface)

write once and run anywhere

What gives Java its 'write once and run anywhere' nature?

The bytecode. Java is compiled to be a byte code which is the intermediate language between source code and machine code. This byte code is not platform specific and hence can be fed to any platform.

Is Empty .java file name a valid source file name?

Yes, save your java file by .java only, compile it by javac .java and run by java yourclassname Let's take a simple example:

//save by .java only
class A{
public static void main(String args[]){
System.out.println("Hello java");
//compile by javac .java
//run by java A

compile it by javac .java

run it by java A

If I don't provide any arguments on the command line, then the String array of Main method will be empty or null?

It is empty. But not null.

What if I write static public void instead of public static void?

Program compiles and runs properly.

What is the default value of the local variables?

The local variables are not initialized to any default value, neither primitives nor object references.

What is difference between object oriented programming language and object based programming language?

Object based programming languages follow all the features of OOPs except Inheritance. Examples of object based programming languages are JavaScript, VBScript etc.

What will be the initial value of an object reference which is defined as an instance variable?

The object references are all initialized to null in Java.

Constructor in Java

Constructor in java is a special type of method that is used to initialize the object.

Java constructor is invoked at the time of object creation. It constructs the values i.e. provides data for the object that is why it is known as constructor.

Rules for creating java constructor

There are basically two rules defined for the constructor.

Constructor name must be same as its class name Constructor must have no explicit return type

Types of java constructors

There are two types of constructors:

Default constructor (no-arg constructor) Parameterized constructor