You are on page 1of 3

1. What is the most important feature of Java?

Java is a platform independent language.


2. What do you mean by platform independence?
Platform independence means that we can write and compile the java code in one platform (eg
Windows) and can execute the class in any other supported platform eg (Linux,Solaris,etc).
3. What is a JVM?
JVM is Java Virtual Machine which is a run time environment for the compiled java class files.
4. Are JVM's platform independent?
JVM's are not platform independent. JVM's are platform specific run time implementation provided
by the vendor.
5. What is the difference between a JDK and a JVM?
JDK is Java Development Kit which is for development purpose and it includes execution
environment also. But JVM is purely a run time environment and hence you will not be able to
compile your source files using a JVM.
6. What is a pointer and does Java support pointers?
Pointer is a reference handle to a memory location. Improper handling of pointers leads to memory
leaks and reliability issues hence Java doesn't support the usage of pointers.

Java: difference between A x = new A() and A x = new


B() when B extends A [duplicate]
The most important difference is between the static and dynamic types of objects and references
to objects.
Say B extends A and C extends B.
The dynamic type of an object (the type used in the new) is its actual runtime type: it defines the
actual methods that are present for an object.
The static type of an object reference (a variable) is a compile-time type: it defines, or rather
declares, which methods can be called on the object the variable references.
The static type of a variable should always be of the same type or a supertype of the dynamic
type of the object it references.
So in our example, a variable with static type A can reference objects with dynamic types A, B
and C. A variable with static type B can reference objects with dynamic types B and C. A variable
with static type C can only reference objects with dynamic type C.
Finally, calling a method on an object reference is a subtle and complex interaction between
static and dynamic types. (Read the Java Language Spec on method invocation if you don't
believe me.)
If both A and B implement a method f() for example, and the static type is A and the dynamic type
involved is C for a method invocation, then B.f() will be invoked:
B extends A, C extends B
public A.f() {}
public B.f() {}
A x = new C(); // static type A, dynamic type C
x.f(); // B.f() invoked
Simplifying greatly: first the static types of both receiver (type A) and arguments (no args) are
used to decide the best-matching (most specific) method signature for that particular invocation,
and this is done at compile-time. Here, this is clearly A.f().
Then, in a second step at runtime, the dynamic type is used to locate the actual implementation
of our method signature. We start with type C, but we don't find an implementation of f(), so we
move up to B, and there we have a method B.f() that matches the signature of A.f(). So B.f() is
invoked.
In our example we say that method B.f() overrides method A.f(). The mechanism of overriding
methods in a type hierarchy is called subtype polymorphism.
Dynamic binding & Static binding.

How to Use an Anonymous Class in Java

An anonymous class in Java is a class not given a name and is both declared and instantiated in a single
statement. You should consider using an anonymous class whenever you need to create a class that will be
instantiated only once.
Although an anonymous class can be complex, the syntax of anonymous class declarations makes them most
suitable for small classes that have just a few simple methods.
An anonymous class must always implement an interface or extend an abstract class. However, you dont use
the extends or implements keyword to create an anonymous class. Instead, you use the following syntax to
declare and instantiate an anonymous class:
newinterfaceorclassname(){classbody}
Within the class body, you must provide an implementation for each abstract method defined by the interface or
abstract class. Heres an example that implements an interface named runnable, which defines a single
method named run:
runnabler=newrunnable()
{
publicvoidrun()
{
//codefortherunmethodgoeshere
}
};
Here are a few other important facts concerning anonymous classes:

An anonymous class cannot have a constructor. Thus, you cannot pass parameters to an anonymous
class when you instantiate it.

An anonymous class can access any variables visible to the block within which the anonymous class is
declared, including local variables.

An anonymous class can also access methods of the class that contains it.

You might also like