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?





method overloading in java

method overloading in java

Method overloading:

                In a class, many methods have the same name but different parameters, it is known as method overloading. In a class, two methods have the same name but a different signature that indicates method is overloaded.
The method overloading increases the readability of the program and we can overload the main () in java. Method overloading is not possible by changing the return type of the method. Method overloading is possible within the class only.There are two ways to overload the method
·         By changing parameters
·         By changing the data type.

Syntax:
                public <data type> <method name>(parameter1,parameter2,…..)
                public <data type> <method name>(parameter1 , parameter2,…)

Example:
                public void difference(int i, int j)
                public void difference (int i, int j, int k)
                public void difference (double i, double j)

Sample java program:
                class Sample{
public void difference(int i, int j){
       System.out.println(i-j); }
public void difference (int i, int j, int k) {
        System.out.println(i-j-k); }
public void difference (double i, double j) {
        System.out.println(i-j); }
public static void main(String args[]){
Sample s= new Sample();
s. difference(10,8);
s. difference(15,3,2);
s. difference(10.0,5.0);
}
}


Interview Questions:               
Define method overloading?
What is the use of method overloading?
What is the difference between method overloading and method overriding?
Can we overload main () method?
How many we can overload a method?               
 Differences between method overriding and method overloading
polymorphism in java

polymorphism in java

Polymorphism:
                                The polymorphism in java is an oops concept one method performing different operations is known polymorphism. In polymorphism same method performing different operations.

There are two types of polymorphisms in java
1.       Compile time polymorphism (static polymorphism)
2.       Run time polymorphism (Dynamic  polymorphism)

Compile time polymorphism:
                Compile time polymorphism is nothing but the method overloading in java. A class has more than one method with the same name but performing different operations and it has different arguments. The compile time polymorphism is also known static polymorphism.

Run time polymorphism:
                The reference of the super class can hold an object of the superclass or an object of its subclasses of the superclass. In java runtime polymorphism is also known as dynamic polymorphism.
In method overriding both the classes ( parent, child classes|) have same methods,  in this case, compile time JVM doesn’t figure out which method to call, but it decides at runtime.
Example: Method Overriding

Interview Questions:

What do you know about polymorphism ?
Explain about polymorphism ?
What is Compile time polymorphism?
What is runtime polymorphism ?
What are the difference between  compile time polymorphism and runtime polymorphism ? 

wrapper class in java examples and sample program


Wrapper class in Java

            If we use primitive data types in java programs that is not a fully object oriented, so introduced wrapper classes in java. The wrapper class mechanism is converting primitive type to object and object type to primitive type. The java.lang package is a wrapper class in java.
The eight wrapper classes are,

Boolean                        ------            boolean
Character                     -------           char
Byte                             -------           byte
Short                            -------           short
Integer                         -------           int
Long                            -------           long
Float                            -------           float
Double                         -------           double

Auto-boxing:
            If we converting primitive data type to object is called as auto-boxing.
Example:
            int x=5;
            Integer ob = new Integer(x);

Auto-unboxing:
            Converting reference to primitive data type is known as auto-unboxing.
Example:
            Integer ob = new Integer(x);
            int x=5;

Sample program:

public class BoxingVariables {

     public static void main(String []args){
            int x=5;
            Integer ob = new Integer(x);        //converting int to Integer
        System.out.println(ob);
       
            Integer a=new Integer(15);   
    int i=a.intValue();                 //converting Integer to int 
    System.out.println(+i);

     }
}


Interview Questions:

Explain about Wrapper classes in Java?
What is Auto-boxing in java?
What is Auto-unboxing in java?
Write a sample program to convert the reference to an object?
 List out all the wrapper classes in Java?
Why we are using Wrapper classes?
In which package wrapper classes are present? 

basic data types in java examples and programs


Basic Data Types In Java:


            There are two different types of data types used in java that are

Primitive type
Non-primitive type


Primitive type:

byte:
The byte data type is useful for saving memory in large arrays. It saves memory the range of byte data type is -128 to +127.It is an 8- bit signed two’s complement integer. The byte default value is 0.
Example: byte a=20;

short:
The short is as similar as byte but is  16-bit signed two’s complement integer. The range of short data type is -32768 to +32767. The short data type default value is 0.
Example: short s=1000;

char:
The char data type is a single 16-bit Unicode character. The range of char data type is '\u0000' to '\uffff'. The Char datatypes are used to store any character.
Example: char c ='J';

boolean:
The boolean data type used to represent one bit of information. This data type is used to check true or false conditions. The Default value is false.
Example: boolean isPrime = true;

int:
 It is a default data type for integral values and default value is 0. The range of an int is -2,147,483,648 to 2,147,483,647.It is a 32-bit signed two’s complement integer.
Example: int a=100;

Long:
The Long data type is a 64-bit signed two's complement integer. The range of Long data type is -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. This data type is used to wide ranger int values. The default value of Long data type is 0L.
Example: long a = 10000L;

float:
 To allocate decimal type numeric values to variables it is useful. The float data type is a single-precision 32-bit IEEE 754 floating point.  The default value of float data type is 0.0f.
Example: float f = 23.5;

double:
The double data type is double precision 64-bit IEEE 754 floating point. This data types generally used as the default data type for decimal values. The Default value of double data type is 0.0d.
Example: double d1 = 12.6;

Non-primitive data type:
            The Non-Primitive data types reference variables are creates using defined constructor of the classes. The non-primitive data types also called as reference data types and its default value is null.
          Example: String str =”Java”;

Type-casting:
            Type casting is a process of Converting one data type to anther data type is known as type casting.
Example:

int x = 20;
Byte b = (byte)x;

 




final keyword

final keyword

Final keyword in Java:

                Final is a keyword (or) reserved word in java it is used to restrict the user. We can apply to variables, methods, class. Once we are applied the final keyword  to variable’s we can’t change the value of a variable.
The final keyword not applicable to the constructor.


Final methods:
                Final keyword  also applicable to methods we can make any method as a final method. If a method is declared with final keyword we can’t override that method. The subclass calls the parent class method but we can’t override the method.
We can use final keyword to main() method in java.

Syntax:
                class Sample{
                final void method_name(){
                //
                }
                }

Program:
                class ABC{
                final void display(){     //final method
                System.out.println(“parent class method”);              
                }
                }
                class EFG extends ABC{
                void display(){
                System.out.println(“child  class method”);
                }
                public static void main(String args[]){
                EFG obj                = new EFG();
                obj.display();
                }
                }

Final variables:
                If we use the final keyword to variables we can’t change the value of a variable. The final variables are initialized value at variable declaration time. We can apply the static keyword  to final variables.  Mostly final variables are used in interfaces and final variables are constants.

Example:
final int a=100;

Program:
public class Sample{
static final int a=100;            //final variable
     public final static void main(String []args){
        System.out.println("Hello World");
       System.out.println("Final variable Is"+a);
     }
}


Final class:
                If a class declared with final keyword we can’t extend  it.

Program:
final class ABC{ }            //final class
                class EFG extends ABC{
                void display(){
                System.out.println(“child  class method”);
                }
                public static void main(String args[]){
                EFG obj                = new EFG();
                obj.display();
                }
                }
Compile time error will occur.



Kategori

Kategori