Explain history of java?

History of java:

               Java is a programming language was written by James Gosling along with his team. His team is called as Green team. The green team members are James Gosling, Mike Sheridan, and Patrick Naughton. These people have initiated the Java language project in June 1991. Initially, it is called Green talk and file extension was .gt but later it is called oak language. In 1995 first version was realized called JDK Alpha and Beta. The Oracle Corporation was owned by Sun Micro systems on January 27, 2010.

Java Versions:
1. JDK Alpha and Beta (1995)
2.JDK 1.0 (23rd Jan 1996)
3.JDK 1.1 (19th Feb 1997)
4.J2SE 1.2 (8th Dec 1998)
5.J2SE 1.3 (8th May 2000)
6. J2SE 1.4 (6th Feb 2002)
7. J2SE 5.0 (30th Sep 2004)         
8. Java SE6 (11th Dec 2006)
9. Java SE7 (28th July 2011)      
10. Java SE8 (18th March 2014)

james gosling

                                                                       James Gosling

Interview Questions:
1.What do you know about Java History?
2.Explain about Java Versions?
3.Who is Father of Java?
4. What is Java technology and why we need it?  
5.Initially Java is called as?
6.When Java first version was realised?
7.Java Latest version and its name?
8.When Oracle corporation owned by sun micro system?
9.Explain about Green team?

jvm architecture

JVM (Java Virtual Machine)
       Java Virtual Machine is an abstract machine. It provides the runtime environment and executing java bytecode.  JVM uses different types of memories like class, Heap, Stack, Register, Native Method Stack. JVM is an Interpreter and platform dependent. The various types of class loaders used by JVM,
Bootstrap: Loads JDK internal classes and java.* packages
Extensions: It Loads jar files from JDK extensions directory 
System: Loads classes from system classpath.

Architecture of JVM:
jvm architecture in java with diagram
                                                                JVM architecture 

Class loader:  It is used to load the class files and it is the sub-system of JVM.

Method area: It is stored class structure.

Heap: It is runtime data area. Memory for all class instances (objects) and arrays are allocated.

Java stacks: It stores frames and stores local variables & partial results.

Program counter register:  It contains the address of the JVM currently executed.

Native method stacks:  Natives method stacks  contains all native methods used in the application. The non-java code is called native code.

Execution Engine: It is responsible for executing the program and contains two parts.
 1. Interpreter: It executes byte code when we execute commands.
 2. JIT(Just in time complier): It is converting byte code to executable code providing high performance. The java code will be executed both interpreter and JIT compiler simultaneously it reduces compilation time.


Main operations of JVM:
Loads code
Verifies code                              
Executes code and
Provide runtime environment


Interview question:
What is JVM?
What is mean by platform dependent?
Explain the internal operation of JVM?
Explain various types of class loaders used by JVM?
JVM is Interpreter or complier?

Java oops concepts

Object-Oriented Programming:

                                OOPs is a programming language using  objects and classes instead of actions and rather than logic.
                                Java is an object-oriented programming language.

OOPs concepts in java:


    1.       Object
    2.       Class
    3.       Inheritance
    4.       Encapsulation
    5.       Abstraction
    6.       Interface
    7.       Polymorphism
8.       Package

Object-Oriented Programming Concepts diagram

Object:
                                 An object is a software bundle of related methods and variables. The object is an instance of the class the real-world objects that you find in everyday life.
Ex: Bus,Car,Phone.
More about object

Class:
                                A class is a blueprint it has state and behavior . A class is a logical entity.
State: states means properties of class.
Behavior: Behavior means functionalities of a class.
More about class
Inheritance:
                               Acquiring features from the super-class (parent class) to subclass(child class) is known as inheritance. In  java  inheritance is achieved by using of extends keyword. Inheritance provides Code re-usability.

More about inheritance
Encapsulation:
                                A collection of similar type of data & methods are protective in a single class with required access modifiers, so that can be saved from unauthorized access.
More about encapsulation
Abstraction:                               
                                An abstraction  is a process of hiding internal details and showing only functionality is known as abstraction.
Example: Computer

Interface:
                                An interface is a collection of abstract methods. It is a blueprint of a class. It contains abstract methods and final variables. By using interface 100% abstraction is achieved.

More about interface
Polymorphism:
                                One object existing in many forms is known as polymorphism. In java, there are two types of polymorphisms.

Package:
                                A package is a namespace it contains a group of similar types of classes, interfaces, sub-classes. To overcome naming space packages are very helpful.
More about packages


Interview Questions:

Explain about oops concepts?
What is inheritance?
What is Encapsulation?
Why we use packages?
What is an object in java?
What is polymorphism?          
What is abstraction and how we can achieve 100% abstraction in java?

java string

Java String

                The collection of characters is known as String. In java String is a class and it is a reference data type.
The strings are immutable i.e. It can’t be changed but a new instance is created every time if we perform concat( ) operation. Once we are allocated memory we can’t change. unchangeable nature  is called immutability.

Example of string:
                String s= “Java”;
                String s2 = new String(“Java World”);

Immutable:
Non-changeable nature is called immutability.

Ex: String s1 = "java";                   
      String s2= "world";
      s1.concat(s2);

             In this case, if we are trying to add new string s2 to this previous string s1.  It is allocating new memory to s1 this nature is called as immutability.

Mutable: The changeable nature is called mutability.We can perform any operation can’t create a new instance every time.
Example: StringBuffer, StringBuilder.

immutability and mutability explanation diagram

String trim()
                Return copy of the string , with leading and trailing whitespace omitted.

The difference between '==' and .equals( ) ?

In java == is ment for reference(address) comparison and return true (or) false.
.equals() is used for content comparison and return true (or) false.

Program:
class Sample{ 
public static void main(String args[]){ 
   String s="java";                               //allocated memory to string
   String s2="java2";                      
   String s3= new String("java world");

  s=s.concat(s2);                              //concatenation  (new instance will be created)

  System.out.println(s);                                  //displaying string
   System.out.println(s3);

}
}


 Interview Questions:
Explain about String?
What is mean by immutable?
What is mutability and example of mutable class?
What is String concatenation?
What is the difference between String and StringBuffer?
 What is the difference between String and StringBuffer and StringBuilder?
What is the difference between == and .equals() method in Java?

java array

Java array
       An array is used to store collection of same data type variables.
Java arrays can hold Homogeneous data only. In arrays by using single reference we can store data. Arrays have fixed size.
In java arrays are index based, the first element stored at 0th index location in the array.



Array declaration:
                To declare an we need reference variable of array, and you must specify the type of array.
                dataType[] arrayRefVar; (or)
                dataType []arrayRefVar; (or)
                dataType arrayRefVar[];
Example:
Int[] anArray;

There are two types of arrays in java
Single Dimensional array
Multidimensional array

Single Dimensional array:
                In single Dimensional array elements are stored from 0th location and size is fixed
Example:
Creating, Initializing, and Accessing an Array:
There are two types to create a single dimensional array
By using new keyword,
class sample{
                public static void main(String args[]){
                int myArray[]= new int[3];   //declaration
                myArray[0]=1;   //initialization
                myArray[1]=2;
                myArray[2]=3;
                for(int i=0;i<myArray.length;i++)    //printing of array
                System.out.println(myArray[i]);
}
}
In another Way
class sample2{
                public static void main(String args[]){
                int myArray[]= {1,2,3};   //declaration, initialization
                for(int i=0;i<myArray.length;i++)           //printing of array
                System.out.println(myArray[i]);
}
}


                 

Multidimensional array:             
                In multidimensional array data is stored in row and column based index.
Example:  matrix form
                Int[][] arr = new int[2][2];
Syntax:
                dataType[][] arrayRefVar; (or)
                dataType []arrayRefVar[]; (or)
                dataType [][]arrayRefVar; (or)
                dataType arrayRefVar[][];
Program:
class Sample3{
                public static void main(String args[]){
                int myArray[][]= new int[3][3];   //declaration 
                myArray[0][0]=1;   //initialization
                myArray[0][1]=2;
                myArray[0][2]=3;
                myArray[1][0]=4;
                myArray[1][1]=5;
                myArray[1][2]=6;
                myArray[2][0]=7;
                myArray[2][1]=8;
                myArray[2][2]=9;
                for(int i=0;i<3;i++)
                {
                                for(int j=0;j<3;j++) {
                System.out.print(myArray[i][j]+" ");   //printing of array
                }              System.out.println();
}
}
}

Another Way
class Sample4{ 
public static void main(String args[]){ 

int myArray[][]={{1,2,3},{4,5,6},{7,8,9}};  //declaring and initializing

for(int i=0;i<3;i++){ 
 for(int j=0;j<3;j++){ 
   System.out.print(myArray[i][j]+" ");  //printing

 } 
 System.out.println(); 

}}

Jagged array:
                The Jagged array is like multidimensional arrays. In the jagged array, we can allocate dimensions separately.
Example:
0
1 2 3
4 5
6 7 8 9

int cse[][]= new int[4][];   ([] optional)

cse[0]=new int[1];
cse[1]=new int[3];
cse[2]=new int[2];
cse[3]=new int[4];             

Advantages of arrays:
                If we know size better to go arrays only because fast access and random access.
                Code optimization, by using single reference we can store data.
Disadvantages of java arrays:
                Java arrays are fixed size once we are allocated size we can’t change it.
Example: If we are declared array size is  10 but we are stored 5 values in this case memory is not reusable.

threads in java

Java threads:


Life cycle of thread:
                There are four states in thread lifecycle new (start) ,runnable , non-runnable (wait) and terminate. In Java life cycle of a thread is controlled by JVM. The thread states are
                1. New (start)
                2. Runnable
                3. Run
                4. Non-runnable (wait)
                5. Terminate



New:
                If you create an instance of Thread class and before start() thread it is in the new state.
Runnable:
                After stating thread it is in a runnable state, the thread scheduler has not selected to running state.
Run:
                If thread scheduler is selected then thread comes in to running state.
Wait (non-runnable):
            If thread is alive, but not in running state is called waiting state.
Terminate:
            If the thread is in a dead state is called thread is terminated.

Creating thread:
            In two ways we can create a thread
1.    By extending Thread class
2.    By implementing Runnable interface
Thread class:
            The thread class has many method and constructors to perform operations on thread. In java Thread class extends Object class and implements Runnable interface and Thread class present in java.lang.Thread package.

Declaration:
            public class Thread extends Object
 implements Runnable

Thread class constructors:
            Thread()
            Thread(Runnable r)
            Thread(Runnable r, String name)
            Thread(String name)

Java program on Thread class
            public class Single extends Thread{
          public void run(){
          System.out.println("single thread is started");
          }
          public static void main(String args[]){
          Single s = new Single();
          s.start();
          }
}

Runnable interface:
            The runnable interface can be implemented by any class and it have only one method that is run() method.

public void run(): This method is used to preform action in thread.

Java Program:
public class Sample2 implements Runnable{
                public void run(){
                System.out.println("Runnable interface is implemented");
                }
                public static void main(String args[]){
                Sample2 s = new Sample2();
                Thread t1 =new Thread(s); 
                t1.start();
                }
       }

Difference between implementing Runnable and extending Thread class:
1.Interfaces are loosely coupled and classes are tightly coupled.
2. Runnable interface have only one method that is Run() but in Thread class have many methods
3.Java doesn’t support multiple inheritances if we extend Thread class we can’t extend another class but there is no problem with an interface as we want we can implement interfaces into a single class.
4.Reusability is high in the interface compare with classes.
                                                                                                                  
Type of threads in java:
                A thread is a lightweight process, a program under execution is known as thread. The Threads are sharing address space value and it has own stack value. Mostly thread based architectures are using now.  The threads based architectures are very fast.
                In java, there are two types of threads that are,
                1. Single thread
                2. Multithreading
Single threading:
                Single threading is the processing of one command at a time and it executes one task at a time.

Multithreading:
                In Multithreading context switching is used to the multi-processing system. Multithreading is a process of executing multiple threads (commands) simultaneously.
Example:
Cinema hall one person is collecting tickets and another person is showing seat.
Advantages of Multithreading:
1.       You can perform multiple operations at a time.
2.       Exception handling is essay because Threads are independent. If an exception occurred in the thread it doesn’t effect another thread.


Interview Questions:
1)  What is Thread in Java?
2)  What is the difference between Thread and Process in Java?
3)  How do you implement Thread in Java?
4)  Explain different types thread in java with an example?
5)  Explain the life cycle of the thread?
6)  What are the differences between implementing runnable interface and Thread class?
7)  Explain about multithreading?
8)  What are the advantages of multithreading?





Kategori

Kategori