Java

"49" questions available for Java.


Q. Why the signature of main method is public static void main(String[] args) ?

public specifies that main method is accessible outside the class and package, or simply publicaly accessible. It is static so that its access is not bounded by the class object. void main(String[] args) is the mehod signature however the String type array can be passed in case you need some dynamic runtime user input to initialize the program.

Q. Can we overload or override the main method in the program ?

No!!
You can not override the main method but you can overload it . What we mean here can be undrstood by following program.


class TheMain{
    public static void main(String s, int i){
  //statements
  ..
  }
    
    public static void main(String[] args){
      // statements
     ...
    }
}

class TheSubMain extends TheMain{
    public static void main(String[] args){
      //statements
    ....
    }
}
In the program we have created two main methods in the class TheMain these are different by signature. The overloading is effcted by the number and type of argument so it would not matter as long as the number of type of arguments are different. And in the Subclass the TheSubMain you can see that we have created main method so this is not overriding this is a completely different main method. you must remember that you can not override the static method because they belong completely to the class for which it is defined.

Q. What is the difference between final, finally and finalize ?

final, is a keyword used to make a method or variable constant or to prevent class inheritance. When we use final with a variable it makes it constant. When we use it with class definition it would not allow to extend that class.
finally, is the keyword used with the try-catch block, the code placed in the finally block execute itself in any case, that means it doesnot matter if there is an exception or program ran properly the finally will be executed in both the cases.
finalize, is the method used with the garbage collection. The garbage collection is taken care by the JVM itself but in case you want to help JVM in the work you can do so using finalize method. This method execute the code just before the program is terminated.

Q. What is serialization ?

Serialization is a feature provided by java by which we can save the instance of program in the file for later use. To achieve this we need serializable interface to be implemented in the class.

Q. What are native methods ?

These are methods or say function defined in the more lower level language like c, c++ etc. Native methods may be used to create drivers for some device and then after compiling they can be easily used in java.

Q. What is the difference between == and equals() ?

== always just compares two references (for non-primitives, that is) - i.e. it tests whether the two operands refer to the same object but equals() method compares the state of the objects (e.g. if all attributes are equal). You can even override the equals() method to define yourself when an object is equal to another.


String x = "hello";
String y = new String(new char[] { 'h', 'e', 'l', 'l', 'o' });

System.out.println(x == y); // return false
System.out.println(x.equals(y)); // returns true

Q. What is key difference between String, StringBuffer and StringBuilder classes?

String is immutable, everytime we perform some operation like string concatination a new object is created and the older one doesnot get destroyed unless garbage is collected.
StringBuffer is good enough to take care of the memory efficiency and is thread safe.
StrinBuilder is just like the StringBuffer but is not thread safe so it shall not be used where threading manipulate the values.


Start a discussion...or just leave a comment.