Typical Java interview questions

来源:互联网 发布:有关大数据的毕业设计 编辑:程序博客网 时间:2024/06/05 18:23

1. What is the difference between parameter and arguments?

int add(int a, int b) // here a and b are parameters

{

return a+b;

}

void main(){

int x=3;

int y=4;

int z = add(x,y);//here x and y are arguments.

}

So, when we define a method, variables passed in the method are called parameters. While when we use the method, the value passed to those variables are called arguments.


2. What is final, finalize() and finally?

final is a keyword in Java. It can be used for class, variable and method. A final class can't be subclassed. A final method can't be overriden. A final variable cant change from its initialized value. So in my point of view, final equals being unchangeable.

finalize() method is used just before an object is destroyed.

finally is used after try/catch.


3. What is abstract?

An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.

An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:

abstract void moveTo(double deltaX, double deltaY);

If a class includes abstract methods, the class itself must be declaredabstract, as in:

public abstract class GraphicObject {   // declare fields   // declare non-abstract methods   abstract void draw();}

When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, the subclass must also be declaredabstract.


原创粉丝点击