Thursday, October 19, 2017

Binding(Early and Late binding)





No.

Static Binding (Early)

Dynamic Binding(Late)

1)
It is a binding that happens at compile time. It is a binding that happens at run time.
2)
Actual object is not used for binding.
Actual object is used for binding.
3)
It is also called early binding because binding happens during compilation.
It is also called late binding because binding happens at run time.
4)
Method overloading is the best example of static binding.

Method overriding is the best example of dynamic binding.
5)
Private, static and final methods show static binding. Because, they can not be overridden.
Other than private, static and final methods show dynamic binding. Because, they can be overridden.




Example of static binding

  1. class Dog{  
  2.  private void eat(){System.out.println("dog is eating...");}  
  3.   
  4.  public static void main(String args[]){  
  5.   Dog d1=new Dog();  
  6.   d1.eat();  
  7.  }  
  8. }  

Example of dynamic binding

  1. class Animal{  
  2.  void eat(){System.out.println("animal is eating...");}  
  3. }  
  4.   
  5. class Dog extends Animal{  
  6.  void eat(){System.out.println("dog is eating...");}  
  7.   
  8.  public static void main(String args[]){  
  9.   Animal a=new Dog();  
  10.   a.eat();  
  11.  }  







Wednesday, October 18, 2017

Throwable Throws Throw




Throwable class is the superclass of all errors and exceptions in the Java language. Only objects that are instances of this class (or one of its subclasses) are thrown by the Java Virtual Machine or can be thrown by the Java throw statement.





No.

Throw

Throws

1)
Java throw keyword is used to explicitly throw an exception. (Or more specifically, the Throwable).
The throw keyword is followed by a reference to a Throwable (usually an exception).
Used when writing methods, to declare that the method in question throws the specified (checked) exception.
As opposed to checked exceptions, runtime exceptions (NullPointerExceptions etc) may be thrown without having the method declare throws NullPointerException.
2)
Checked exception cannot be propagated using throw only.
Checked exception can be propagated with throws.
3)
Throw is followed by an instance.
Throws is followed by class.
4)
Throw is used within the method.
Throws is used with the method signature.
5)
You cannot throw multiple exceptions.
You can declare multiple exceptions e.g.
public void method()throws IOException,SQLException.












Wednesday, October 11, 2017

How to find out the length of the string without using length function

There are several methods.., one method is by using toCharArray() method


package com.simplePrograms;

public class Length {

    public static void main(String[] args) {
       
        String blah = "HellO";
        int count = 0;
        for (char c : blah.toCharArray()) {
            count++;
        }
        System.out.println("blah's length: " + count);
    }

}

Difference between Final, Finally, Finalize



Final

Finally

Finalize


Final is a keyword.

Finally is a Block

Finalize is a method.


Final is used to apply restrictions on class, method and variable. Final class can't be inherited, final method can't be overridden and final variable value can't be changed.

Finally is used to place important code, it will be executed whether exception is handled or not.

Finalize is used to perform clean up processing just before object is garbage collected.
 

 Examples::


Java final example
class FinalExample{  
public static void main(String[] args){  
final int x=100;  
x=200;//Compile Time Error  
}}  


Java finally example
class FinallyExample{  
public static void main(String[] args){  
try{  
int x=300;  
}catch(Exception e){System.out.println(e);}  
finally{System.out.println("finally block is executed");}  
}}  


Java finalize example
class FinalizeExample{  
public void finalize(){System.out.println("finalize called");}  
public static void main(String[] args){  
FinalizeExample f1=new FinalizeExample();  
FinalizeExample f2=new FinalizeExample();  
f1=null;  
f2=null;  
System.gc();  
}}



ref: www.javapoint.com