PUB ID

Sunday, 17 January 2016

why java is more secure?



1.No use of pointers
         C/C++ language uses pointers, which may cause unauthorized access to memory blocks when other programs get the pointer values. Unlike conventional C/C++ language, Java never uses any kind of pointers. Java has its internal mechanism for memory management. It only gives access to the data to the program if has appropriate verified authorization.

2.Byte code is the thing that makes Java more secure
       Every time when a user compiles the Java program, the Java compiler creates a class file with Bytecode, which are tested by the JVM at the time of program execution for viruses and other malicious files.
1          no readability i.e., contains byte code understandable to only JVM.No virus will infect the byte code. Even if the virus entered into byte code the jvm doesn't understand it, so jvm keeps these instructions as it is. No executable code will be generated to this virus.


Tuesday, 5 January 2016

Difference between checked and unchecked exceptions?


1) Checked Exception The classes that extend Throwable class except RuntimeException and Error are known as checked exceptions e.g.IOException, SQLException etc. Checked exceptions are checked at compile-time.
2) Unchecked Exception The classes that extend RuntimeException are known as unchecked exceptions e.g. ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc. Unchecked exceptions are not checked at compile-time rather they are checked at runtime.
3) Error Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError etc.

What is the Difference Between throw and throws?


throw
throws
Java throw keyword is used to explicitly throw an exception.
Java throws keyword is used to declare an exception.
Checked exception cannot be propagated using throw only.
Checked exception can be propagated with throws.
Throw is followed by an instance.
Throws is followed by class.
Throw is used within the method.
Throws is used with the method signature.
You cannot throw multiple exceptions.
You can declare multiple exceptions 
e.g.
public void method()throws IOException,SQLException



Java throw example

  1. void m(){  
  2. throw new ArithmetiException("sorry");  


Java throw and throws example

  1. void m()throws ArithmeticException{  
  2. throw new ArithmeticException("sorry");  

What is Adapter class in Java?

  • Adapter class is a simple java class that implements an interface with only EMPTY implementation .
  • Instead of implementing interface if we extends Adapter class ,we provide implementation only for require method 
  • An adapter class provides the default implementation of all methods in an EventListener interface. Adapter classes are very useful when you want to process only few of the events that are handled by a particular event listener interface.
     
    Here's a mouse adapter that beeps when the mouse is clicked


    import java.awt.*;
    import java.awt.event.*;
    
    public class MouseBeeper extends MouseAdapter  {
    
      public void mouseClicked(MouseEvent evt) {
        Toolkit.getDefaultToolkit().beep();
      }
    
    }
    
    Without extending the MouseAdapter class, I would have had to write the same class like this
    import java.awt.*;
    import java.awt.event.*;
    
    
    public class MouseBeeper implements MouseListener  {
    
      public void mouseClicked(MouseEvent evt) {
        Toolkit.getDefaultToolkit().beep();
      }
    
      public void mousePressed(MouseEvent evt) {}
      public void mouseReleased(MouseEvent evt) {}
      public void mouseEntered(MouseEvent evt) {}
      public void mouseExited(MouseEvent evt) {}
    
    }
    
    Adapter classes are a minor convenience. You do not need to use the adapter classes if you don't want to.
     Click here for full tutorial of Adapter Classes
     

Saturday, 2 January 2016

What is the difference between print() method ,prinln() method ?

print()
1.print() prints the characters in same line,
2.without passing any argument  you can not use print() method.
 println()
1. println() writes the character in next line,
2.you can use println() without passing any argument

 these two methods are overloaded methods in java.io.PrintStream class