Wednesday, 24 August 2016

JAVA Features_2

Static Method

  • It is a method which belongs to the class and not to the object(instance)
  • A static method can access only static data. It can not access non-static data (instance variables)
  • A static method can call only other static methods and can not call a non-static method from it

static block


The static block, is a block of statement inside a Java class that will be executed when a class is first loaded in to the JVM.

Static Data Member

Same data is shared between all instances of a class.


Instanceof Operator:

This operator is used only for object reference variables. The operator checks whether the object is of a particular
type(class type or interface type).


  Example:1

String name = “James”;

boolean result = name instanceof String;

// This will return true since name is type of String
Example:2
classVehicle{}
public class CarextendsVehicle{
public static void main(String args[]){
Vehicle a =newCar();
boolean result = a instanceofCar;
System.out.println(result);///True
}
}


Exceptions:


Checked exceptions: A checked exception is an exception that is typically a user error or a problem that

cannot be foreseen by the programmer.

Runtime exceptions: A runtime exception is an exception that occurs that probably could have been avoided
by the programmer.
Errors: These are not exceptions at all, but problems that arise beyond the control of the user or the
programmer.

Why is StringBuffer called mutable?



The String class is considered as immutable, so that once it is created a String object cannot be changed.

If there is a necessity to make a lot of modifications to Strings of characters then StringBuffer should be used.


String represents fixed-length, immutable character sequences. In contrast, StringBuffer represents growable and writeable character sequences. StringBuffer may have characters and substrings inserted in the middle or appended to the end.

Which are the two subclasses under Exception class?



The Exception class has two main subclasses : IOException class and RuntimeException Class.

All exceptions must be a child of Throwable.


Difference between throw and throws?


Throw is used to trigger an exception where as throws is used in declaration of exception.


Without throws, Checked exception cannot be handled where as checked exception can be propagated with throws.


Error Vs Exception  :


1) Recovering from Error is not possible. The only solution to errors is to terminate the execution. Where as you can recover from Exception by using either try-catch blocks or throwing exception back to caller.

2) You will not be able to handle the Errors using try-catch blocks. Even if you handle them using try-catch blocks, your application will not recover if they happen. On the other hand, Exceptions can be handled using try-catch blocks and can make program flow normal if they happen.

3) Exceptions in java are divided into two categories – checked and unchecked. Where as all Errors belongs to only one category i.e unchecked.







No comments:

Post a Comment