Tuesday, January 23, 2007

what are Local Variables ?

Variables declared within a method. That means the variable is not just initialized within the method, but also declared within the method. As local variables starts its life inside the method, it's also destroyed when the method has completed. Local variables are always on stack and not on heap.


SOURCE : www.referjava.com

What is Array?

Arrays are objects that store multiple variables of the same type or variables that are all subclasses of the same type.

SOURCE : www.referjava.com

What are Constructors?

A constructor is always invoked when a new object is created. Every class has atleast one constructor. Constructor must have the same name as the class name. Constructors don't have a return type. The default constructor is a no-arg constructor.


SOURCE : www.referjava.com

How to request for Garbage Collection?

You can request for garbage collection with System.gc();


SOURCE : www.referjava.com

What is an Exception?

An abnormal condition that occurs during normal program flow.

There are two types of exception:
1. checked
2. unchecked.

SOURCE : www.referjava.com

Difference between String and StringBuffer ?

String objects are immutable and String reference variables are not.
StringBuffer objects are mutable.


SOURCE : www.referjava.com

What is a Thread?

A Thread is a line of execution. It is an instance of class java.lang.Thread.
There is one thread per call stack.

SOURCE : www.referjava.com

How can you define a Thread?

You can define and instantiate a Thread in two ways:

Extend the java.lang.Thread class
Implement the Runnable interface.

SOURCE : www.referjava.com

States of Thread

A thread has five states:

1. New Runnable
2. Running
3. Waiting/Blocked/sleeping
4. Dead.

SOURCE : www.referjava.com

Interfaces in Collection

1. List
2. Set
3. Map
4. Queue
5. SortedSet
6. SortedMap.

SOURCE : www.referjava.com

What is Object ?

Object is instance of class, object has its own state and has access to all of the behaviour defined by the Class.
Object or instance is an execution copy of a class.

SOURCE : www.referjava.com

What is class?

A template that describes the kinds of state and behaviour that objects of its type support.

Class is a structure that defines data and methods to work on that data.

SOURCE : www.referjava.com

Constructors available in thread class

1.Thread()
2.Thread(Runnable target)
3.Thread(Runnable target, String name)
4.Thread(String name).

SOURCE : www.referjava.com

What is Struts ?

it is an open source framework. It follows the MVC design pattern

Struts is a frame work , has its own library(api) which makes easy to develope loosly coupled web applications using jsp and servlet.

SOURCE : www.referjava.com

Abstract class ?

An abstract class can never be instantiated. Its sole purpose is to be extended. There should be atleast one abstract method in an abstract class.

abstract methods end in a semi colon. They only declare the methods but contain no body for the method. A concrete subclass of an abstract class must implement all the methods in the subclass.

. abstract class must bs declared as absrtact using "abstract" key word.
. abstact class can't be instantiated.
. may not contain abstract method ie its not necessary to have abstarct method in abstract class.
. class which extends abstract class must implement all the abstract methods in the abstract class else should be declared as abstract class.

SOURCE : www.referjava.com

Garbage Collection

In Java, garbage collection provides automated memory management. Its purpose is to delete objects that can't be reached.

we can make an object eligible for garbage collection ..and we can request garbage collector usaing System.gc() or Runtime.gc() methods ......... but we can never force garbage collection.

SOURCE : www.referjava.com

What are the components of Event Delegation Model ?

The components are:
1. Event Classes
2. Event Listeners
3. Implicit event handlers
4. Adapters

SOURCE : www.referjava.com

What are Event Classes?

The EventObject class is the top most class in the hierarchy in java.util package. It contains the methods: getSource() - returns the object that initiated the event getId() - returns the nature of the event.

Event Listeners

An object that delegates the task of handling an event to an event listener. When an event occurs an event object of the appropriate type is created.


ActionListener
MouseListener
MouseMotionListener
ComponentListener
ContainerListener
ItemListener
KeyListener
WindowListener

SOURCE : www.referjava.com

difference between a constructor and a method?

A constructor is a member function of a class that is used to create objects of that class. It has the same name as the class itself, has no return type, and is invoked using the new operator. A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator.

SOURCE : www.referjava.com

What is final?

A final class can't be extended ie., final class may not be subclassed. A final method can't be overridden when its class is inherited. You can't change value of a final variable (is a constant).

SOURCE : www.referjava.com

Can an application have multiple classes having main method?

Yes it is possible. While starting the application we mention the class name to be run. The JVM will look for the Main method only in the class whose name you have mentioned. Hence there is not conflict amongst the multiple classes having main method.

SOURCE : www.referjava.com

What are Checked and UnChecked Exception?

A checked exception is some subclass of Exception (or Exception itself), excluding class RuntimeException and its subclasses. Making an exception checked forces client programmers to deal with the possibility that the exception will be thrown. eg, IOException thrown by java.io.FileInputStream's read() method· Unchecked exceptions are RuntimeException and any of its subclasses. Class Error and its subclasses also are unchecked. With an unchecked exception, however, the compiler doesn't force client programmers either to catch the exception or declare it in a throws clause. In fact, client programmers may not even know that the exception could be thrown. eg, StringIndexOutOfBoundsException thrown by String's charAt() method· Checked exceptions must be caught at compile time. Runtime exceptions do not need to be. Errors often cannot be.

SOURCE : www.referjava.com

inner class doubt

First, "A a1 = new A();" will create an Object of class A, setting the private String name of this Object to A0. Then "a1.m1();" is executed - and so "new A().new B();" will be executed. A new A-object is created, setting its name to A1 and a B (tied to this A) is created with an innerCounter of 0 (incrementing the static innerCounter to 1). Then this A's name and this Bs name are printed, giving A1B0. Next, "a1.m2();" is executed - so "new A.B();" is executed. A new B will be created (tied to our "old" object we crerated first, referenced by a1). This B's name will be B1, incrementing the innerCounter to 2. Our A, referenced by a1, still has "his" name A0, so A0B1 will be printed. Last, "a1.m3();" will be executed, and so "new B();". Again, a new B will be created, tied to our first A-object, referenced by a1. So A0B2 is printed.First, "A a1 = new A();" will create an Object of class A, setting the private String name of this Object to A0. Then "a1.m1();" is executed - and so "new A().new B();" will be executed. A new A-object is created, setting its name to A1 and a B (tied to this A) is created with an innerCounter of 0 (incrementing the static innerCounter to 1). Then this A's name and this Bs name are printed, giving A1B0. Next, "a1.m2();" is executed - so "new A.B();" is executed. A new B will be created (tied to our "old" object we crerated first, referenced by a1). This B's name will be B1, incrementing the innerCounter to 2. Our A, referenced by a1, still has "his" name A0, so A0B1 will be printed. Last, "a1.m3();" will be executed, and so "new B();". Again, a new B will be created, tied to our first A-object, referenced by a1. So A0B2 is printed.

SOURCE : www.referjava.com

What is serialization?

Serialization is a mechanism by which you can save the state of an object by converting it to a byte stream.

SOURCE : www.referjava.com

difference between protected and friendly access specifier

Protected data and methods are accessible to all the classes within the same package and to all the subclasses of any package. Now if this holds true, why the following prog. failed to compile.


// file A.javapackage p1;
public class A{
protected int i = 10;
public int getI() {
return i;
}
}
// file B.java package p2;
import p1.*;
public class B extends p1.A{
public void process(A a)
{
a.i = a.i * 2;
}
public static void main(String[] args) {
A a = new B();
B b = new B();
b.process(a);
System.out.println( a.getI() );
}
}

SOURCE : www.referjava.com

What is Overriding?

In declaration we just mention the type of the variable and it's name. We do not initialize it. But defining means declaration + initialization. e.g String s; is just a declaration while String s = new String ("abcd"); Or String s = "abcd"; are both definitions.

SOURCE : www.referjava.com

difference between declaring a variable & defining variable

In declaration we just mention the type of the variable and it's name. We do not initialize it. But defining means declaration + initialization. e.g String s; is just a declaration while String s = new String ("abcd"); Or String s = "abcd"; are both definitions.

SOURCE : www.referjava.com

Can a top level class be private or protected?

No. A top level class can not be private or protected. It can have either "public" or no modifier. If it does not have a modifier it is supposed to have a default access.If a top level class is declared as private the compiler will complain that the "modifier private is not allowed here". This means that a top level class can not be private. Same is the case with protected.

SOURCE : www.referjava.com

What is serialization?

Serialization is a mechanism by which you can save the state of an object by converting it to a byte stream.

SOURCE : www.referjava.com

Example for Constructor

class Parent {
String s="Parent" ;
public Parent(){
method(); }
public void method()
{
System.out.println(s);
}
}
class Child extends Parent {
String s = "Child";
public Child(){
}
public void method(){
System.out.println(s);
}
}
public class Doubt {
public static void main(String[] args)
{
Child child = new Child();
}
}

SOURCE : www.referjava.com

Who Found Java?

First James Goslig found OAK, it was redefined from c++ in 1991. Then it was Renamed as JAVA in 1995.

SOURCE : www.referjava.com