Q What are the differences between the == operator and the equals() method?

Well, first off, == is a fundamental operator in the language. The result type of the expression is a boolean. For comparing boolean types, it compares the operands for the same truth value. For comparing reference types, it compares the operands for the same reference value (i.e., refer to the same object or are both null). For numeric types, it compares the operands for the same integer value or equivalent floating point values.
In contrast, equals() is an instance method which is fundamentally defined by the java.lang.Object class. This method, by convention, indicates whether the receiver object is "equal to" the passed in object. The base implementation of this method in the Object class checks for reference equality. Other classes, including those you write, may override this method to perform more specialized equivalence testing.

Q How can I force garbage collection to take place?

Strictly speaking, you can't force it.
You can, however, call System.gc(), which is a "hint" to the runtime engine that now might be a good time to run the GC. Some implementations take that hint to heart and some don't.

Q When should I use an interface instead of an abstract class?

There are two primary axes of "inheritance" in object-oriented languages like Java.
"Implementation" inheritance is where the sub-class inherits the actual code implementation from the parent.
"Interface" inheritance is where the "sub-class" adheres to the public interface of the "parent".

Java actually mixes the two notions together a bit. Java interfaces are nice and clean -- when you "implement" an interface, you are stipulating that your class adheres to the "contract" of the interface that you specified. Java class inheritance isn't so clean -- when you sub-class in Java you are getting both the code inheritance but you are also stipulating that your sub-class adheres to the "contract" of the interface of the parent class.

Abstract classes in Java are just like regular Java classes but with the added constraint that you cannot instantiate them directly. In terms of that added constraint, they are basically classes which don't actually implement all of the code specified by their "contract".

So, it's generally considered good OO practice to specify the "contract" which you want to adhere to via Java interfaces. Then use normal Java class inheritance primarily for code reuse purposes. Use abstract Java classes when you want to provide some standard base code but want/need to force the user's of your class to complete the implementation (i.e., you create a skeleton implementation and the sub-classes must flesh it out).

Q What are the differences between instance and class variables?

Instance variables have separate values for each instance of a class. Class variables maintain a single shared value for all instances of the class, even if no instance object of that class exists.

You would use the static keyword to change an instance variable into a class variable.
Both instance and class variables are declared at the class level, not within methods:


public class MyTest {

static private int count; // class variable

String name; // instance variable

private void printName() {

int nameCount; // local variable

}
}
Note also that classes are loaded by classloaders therefore class variables are unique on a per-classloader basis.



Q What is an overloaded method?

Overloaded methods are multiple methods in the same class that share the same name but have different parameter lists. Overloaded methods cannot have the same parameter lists with different return types.


public class Person {

public void SetName () {
// do something here
}

// Overloaded method
public void SetName (String name){
// Do something here
}
}

Del.icio.us Digg! My StumbleUpon Page