javascript1

Friday, 7 August 2015

Java Interview Questiojn for interview

Q1 Differentiate final , finalize and finally keyword   in java? 

Or
Why we use finalize mehod in java

Just what is finalize() technique?

Just what is the difference between final keyword , finally keyword and finalize in java ?

why we declare class as a final 

Answer :
              1.     final       is used for declaring constant 
2.     Finally   handles exception in java when a program is executed 
3.     Finalize  job of finalize is automatic garbage collection

Important Tip : Variables specified in an interface by default are  final.we can not extend a final class 


Friday, 13 February 2015

Static Variable , Static Method and Static Block

Static Keyword 


The static keyword is is used for  memory management . java static  keyword can be used  with  a variables , methods , blocks and nested class. When we define a static keyword with method and varriable then it is related to class.
 The static can be:
  1. A class member (varriable)
  2. A method of class
  3. A block
  4. A nested class (class within class)

1) Java static variable
Static varriable are  those varriable which are related to class instead of  objects .
Static varriable  gets the memory once  at time of class loading  not at the time of object creation .
Advantage of static variable
 static is used for efficient utilization of memory.

Understanding problem without static variable


class Employee
{  
     int Eid;  
     String name;  
     String company="google";  

Let us assume there are 10,000 employee`s  in company , now all data members of class  get memory each time when object of class  is created. All employye`s have its unique Eid and name so i  should not be static because it is related to every employee but company name is  common for all employees . If we make company name static than it takes  memory only once


Example of static variable
//Program of static variable  
  
class employee{  
   int eid;  
   String name;  
   static String company ="google";  
     
   employee(int a,String b){  
   eid = a;  
   name = b;  
   }  
 void display ()
{
System.out.println(eid+" "+name+" "+company);}  
  
 public static void main(String args[])
{  
 employee e1 = new employee(1200,"vinay");  
 employee e2 = new employee(1201,"abhishek");  
   
 s1.display();  
 s2.display();  
 }  


class Javacount {  
int i=0;// i  gets the  memory when object of a class is created   
  
Javacount(){  
i++;  
System.out.println(i);  
}  
  
public static void main(String a[]){  
  
Javacount c1=new Javacount ();  
Javacount c2=new Javacount ();  
Javacount c3=new Javacount ();  
  
 }  



Output:1
       1
       1


class Javacount {  
static int i=0;// i  gets the  memory when object of a class is created   
  
Javacount(){  
i++;  
System.out.println(i);  
}  
  
public static void main(String a[]){  
  
Javacount c1=new Javacount ();  
Javacount c2=new Javacount ();  
Javacount c3=new Javacount ();  
  
 }  

Output:1
       2
       3
Where static method can’t be used

1.    static method is not able to acces non static data members and non static method.
2.    In  static method this and super keyword is not used .

1.    class employee{  
2.     int value=40;           // non static data member   
3.       
4.     public static void main(String a[]){  
5.      System.out.println(value);  
6.     }  
7.    }  

Output:Compile Time Error
Java static block
  • static block  is  used for  static data member initialization .
  • static block executed before the main method .
Example of static block
1.    class employee
2.    {  
3.      Static
4.    {
5.    System.out.println("this is static block of employee class");}  
6.      
7.    public static void main(String argument[])
8.    {  
9.       
10. System.out.println("main funtion is invoked ");  
11.  
12.   }  
13. }
 


Method Overloading in Java

method overloading in Java

Method Overloading is used when we have more then one mehod with same name but perform different functionality
Suppose youneed to calculate area of  square  , rectangular and eclipse then you have two option :
1.    create different function for like area1 , area 2, area 3 in order to differentiate the function .
2.    second way use method overloading and use the same method name .

Second way is more efficient because it increase the readability of the program .

 Advantage of method overloading


Method overloading increase the readability of the program

How to do method overloading


1         by changing the number of arguments
2         by changing the data type

1) Method Overloading by   arguments number


class Operation{  
  void sum(int a,int b){System.out.println(a+b);}  
  void sum(int a,int b,int c)
{

System.out.println(a+b+c);

}  
  
  public static void main(String a[]){  
  Calculation obj=new Calculation();  
  obj.sum(10,10,10);  
  obj.sum(20,20);  
  
  }  
}  

2) Method Overloading by   arguments Type


class Operation{  
  void sum(int a,int b){System.out.println(a+b);}  
  void sum(int a,float b)
{

System.out.println(a+b);

}  
  
  public static void main(String a[]){  
  Calculation obj=new Calculation();  
  obj.sum(10,10);  
  obj.sum(20,10.4);  
  
  }  
}  



difference between JDK, JRE and JVM

What is  difference between JDK, JRE and JVM  

If you want to get the detailed knowledge of Java Virtural Machine, move to the next page. Firstly, let's see the basic differences between the JDK, JRE and JVM.

JVM(Java Virtual Machine)

JVM ( Java Virtual Machine) is known as virtual because jvm output is not machine dependent   which makes java as a platform independent. When we compile a java program then  output is in ‘.class’ file instead of .exe file . this class file is known as byte code.
JVM is an enviourment in which java byte code is executed .
JVM , JDK and Jre are not platform independent they are designed specifically for different operating system  but the byte code provided by every jvm is same which is platform indepent .
The JVM  is responsible for following tasks :

Step 1 :It  Loads the code written by user.
Step 2 : it Verifies the code before executing
Step 3 : Executes code and generate byte code

Step 4 : Provides a  runtime environment for byte code






JRE(Java RunTime Environment)

JRE is known as  Java Runtime Environment which provide enviourment for execution . jre  is the implementation of JVM. JRE contains set of libraries files which is used by JVM at runtime .

JDK (Java Development Kit)

Java Developer Kit is set of  tools which is required to develop the Java programs  and  JRE is required to  execute java  programs. Jdk contain java compile ,  Java application launcher and  Appletviewer, etc…
Java compiler convert the java code into byte code .

Jdk contain JVM +JRE and other tools .

Thursday, 12 February 2015

Simple and easy to learn


Java is a very simple language because it`s syntax is same just like as c and c++
Pointer is not used in java which makes java very simple
In java automatic garbage collection so that there is no need to remove object which are not in used .
     

Object-oriented programming language


Java is Object-oriented  programming language . Object-oriented  means  we can create our application in different modules and then combine each other.  Different module is developed using classes .
Basic concept of OOPs are:
1   1 .    Object
2.    Class
3.    Inheritance
4.    Polymorphism
5.    Abstraction
6.    Encapsulation

Platform Independent

Platform Independent  means application developed in java operate on every platform like on window  , linux , mac etc .
Java is  Platform Independent  because it works on the concept of byte code which is not specific to any operating system .  byte code is interpreted by jvm .