Monday 16 March 2015

Q1 What Is Java Why Java ?




Java is a programming language , it is a development environment.

·       Programming Language.
·       Development Environment.

Some of the key features that makes java powerful and robust are:

·       It has built in support for : multithreading ,Socket communication  & Memory Management (garbage collection ). 
·       It is Object Oriented.
     ·       It supports :

  1.    web based applications (applets, servlets, jsp..).
  2.    Distributed applications (Socketds, Rmi, ejb,….).
  3.    Network Protocols (http, Jrmpets..)with the help of    extensive standard API’s(Application Program                       Interface).           
                              
Features To Remember :


  1. Simple.
  2. Object-Oriented.
  3. Platform independent.
  4. Secured.
  5. Robust.
  6. Architecture neutral.
  7. Portable.
  8. Dynamic.
  9. Interpreted.
  10. High Performance.
  11. Multithreaded.
  12. Distributed.

Q 02) - What Is The Difference Between c++ and Java ?




 

  •  Java does not support the concept of Pointers. Pointers are inherently tricky and troublesome to use.
  • Java does not support multiple inheritance because it causes more problem s than it solves. Instead Java supports multiple interface inheritance.
  •  Java does not use structures and unions because the traditional data structures are implemented as an object oriented framework( Java Collection Framework ).
  •  All the codes are encapsulated with in a class there fore java does not have a global variables or functions.
  • C++ requires explicit memory management, while java includes automatic garbage collection.

Q 03) Difference Between Java Platform And Other Platforms ?



           A java platform is a software only platform. It runs on other hardware based platforms like Unix, Linux, windows nt ,….etc.

Java platform has two components  :
  •        JVM ( Java Virtual Machine ) : It is a software that cam be ported  onto various hardware platforms. And converts Byte codes into  machine level language.   
  •       Java API  : Application Program Interface.

Q 04) What Is Jvm Explain With a Diagram ?





Java Virtual Machine (JVM) :

     A JVM converts bytecode into machine level language.
or

    A jvm is a runtime environment used to run bytecode generated form the source code without taking the help of the operating system.



1)    Class loader : Loads the class files
2)    Class Method Area : All the methods and method data are stored.
3)    HEAP: All the OBJECTS are stored here.
4)    STACK: Java stack stores frames, it holds local variables and partial results and plays a part in method invocation and return values.
5)    PC (Program Counter )Register: Contains address of JVM instructions currently being executed.
6)    Native Method Stack : All native methods used in application.
7)    Execution Engine : 
·       A Virtual processor
·       Interpreter ( Reads bytecode streams then executed instruction )
·       JIT Complier (Just In Time)

JIT Compiler : A JIT (Just In Time) Compiler is used to improve the performance.

A JIT – Compiler compiles  parts of byte code that have similar functionality at the same time, and hence reduces the time needed.

Q 05) Garbage Collection Converted ?




 Garbage collection :

  Freeing up the required memory to make a program more “ Robust” .
or
   Destroy Useless objects.


   Garbage collection is an assistance provided in java and by using this assistance , the chances of failing a java program with memory problems is very very low.

Q 06) Class Loaders ?





A class loader is a component of jvm which checks if we are following all the rules at compile time or not. There after it converts byte codes and loads the “ .class files “ .
There are two types of Class loaders:

1. Boot Strap or System Class Loader :

Boot strap is responsible to load the java API classes  written  by the sun developers. It is responsible to load Jdk’s internal classes
                     Eg : java.* packages
System Class loader can load a class from local path only.

2. User defined Class Loader: 

                    If we want to load the application from remote machine.  If we need to                     define user defined class loader.





Q 07) Object And Class ?




 An “Object is an Instance of a Class”.

Or

An Object represents real world entities with certain characteristics such as

·         STATE          : Represents Data (Value) of an object.
           BEHAVIOR : Represents the functionality of an object . e.g. : Deposit , withdraw,…
·         IDENTITY   : Every object is designated with a unique ID by the JVM. And this ID can only be                                  recognized by JVM.






3 Ways of Creating an Object   :

1)    MyDog mDog = new MyDog ();

Terminology :

MyDog                           mDog          =       new        MyDog ();
(Object Reference )    ( Reference Variable  ) ( Keyword)   ( Constructor )
or
2)    String        myString      =       “ Welcome to JavaNoobs !!!”  ;

     or

3)    String      myString      =      “ I see I remember !!! ”;

Class :

A Class is a blue print of object.

or

        We can say that a class is a software blueprint of an object used to instantiate many individual objects .
A class consists of Attributes and methods.






Q 08) Constructors And Methods ?




CONSTRUCTOR :A Constructor is a set of instructions designed to initialize an instance of a newly created object.

Two Rules defined for the Constructors :
·         A constructor should have the same name a Class.
·         Constructors must not return a value.


There are two types of constructors :

1.       Default Constructor:         
     
·         Default constructor refers to a constructor that is automatically created by compiler in the absence of explicit constructors.
·         You can also call a constructor without parameters as default constructor because all of its class instance variables are set to default values.

2.       Parameterized Constructor:
               ·         Parameterized constructors are required to pass parameters on creation of objects.

·         If we define only parameterized constructors, then we cannot create an object with default constructor. This is because compiler will not create default constructor. You need to create default constructor explicitly.
  
METHOD :

public     void             add     (int a, int b)     {  }
A Java method is a collection of statements that are grouped together to perform an operation. access 
  return type   name   parameters   Body 


Example programs of Constructors :


1)  Default Constructor :
package org.javanoobs.dconstructor;

public class DefaultConstructor {

     // This is the Constructor
     public DefaultConstructor() {

           System.out.println(" I am inside the default constructor !!! ");
     }

     public static void main(String args[]) {

           // Object created..
           DefaultConstructor dCon = new DefaultConstructor();
     }

}

OUTPUT :
I am inside the default constructor !!!

2)  Parameterized Constructor :

package org.javanoobs.pconstructor;

public class Test2 {

     int a, b; // instance variables.
     static int c;

     // this is the instance block.
     {
           System.out.println(" Inside Instance Block ");
           System.out.println(" C =  " + c);
           System.out.println(" A =  " + a);

     }

     static {

           System.out.println("Inside the static block !!!");
           System.out.println(" C =  " + c);

     }

     public void disp() {

           int d = 1; // Local varables, initialize to 0 or 1 or will result in an
                           // error.
           d += a + b + c;

           System.out.println("d after addition : " + d);
           System.out.println(" Disp method called...");
     }

     public static void main(String args[]) {

           Test2 t; // refference variable .
           t = new Test2(); // t is the object .
           t.disp();
     }

}




package org.javanoobs.pconstructor;

public class ParameterizedConstructor {

     private String name;

     public ParameterizedConstructor(String str) {
           this.name = str;
           System.out.println("I am Inside Parametrized Constructor !!!");
           System.out.println("The parameter Value is : " + str);
     }

     public static void main(String args[]) {

           ParameterizedConstructor pCon = new ParameterizedConstructor(" Java Noobs !!! ");
     }
}

OUTPUT :

I am Inside Parametrized Constructor !!!
The parameter Value is :  Java Noobs !!! 




Q 09) What Are Mutable And Immutable Objects ?




Mutable Objects : 
          
          When you have the reference to an object and if the reference of the object can be altered it is known as Mutable Object.
A Mutable object is a kind of object whose state can be modified after it is created.

Eg : String Buffer.

Immutable Objects : 

           When you have the reference to an object  and if the reference of that object if it cannot be altered it is known as Immutable Object.
An Immutable object is a kind of object whose state Cannot be modified after it is created.
          Eg: StringByteCharacterDoubleFloatIntegerLong,

                Short, and  Boolean are immutable classes in Java

Q 10) Heap And Stack ?


Java Heap Memory:

    Heap memory is used by java runtime to allocate memory to Objects and JRE classes.

     Whenever we create any object, it’s always created in  the Heap space.

      Garbage Collection runs on the heap memory to free  the memory used by objects that doesn’t have any  reference.

       Any object created in the heap space has global access  and can be referenced from anywhere of the  application.

Java Stack Memory:

     Java Stack memory is used for execution of a thread. They contain method specific values that are short-lived and references to other objects in the heap that are getting referred from the method.
     Stack memory is always referenced in LIFO (Last-In-First-Out) order.
     Whenever a method is invoked, a new block is created in the stack memory for the method to hold local primitive values and reference to other objects in the method.
     As soon as method ends, the block becomes unused and become available for next method.

     Stack memory size is very less compared to Heap memory.



Q 11) Primitive And Reference Datatypes ?





Primitive Data
Types
Reference  Data
Types
Default values of primitive datatypes depends on the primitve datatype.
Jvm initializes refference variables as “NULL” and will also initialize array to “NULL”.
Eg:
Int  = 0
Float = 0.0
Char = null


Q 12) Local And Instance Variables ?







Local Variable
Instance Variable
01)
  Local variables are the variables which are declared “ inside a Method or a Block “ .
 Instance variables are the variables which are “ declared directly in a Class “.
02)
  Local variables will not be initialized by the JVM, we will have to initialize it.
 Instance variables will be initialized by the JVM, with their default values.
03)
 Memory will be allocated for local variables when the corresponding block or a method is executed.
Memory will be allocated for the instance variables when an object is being created.


 




Example Programs of Instance And Local Variable :



Program 1 :

package org.javanoobs.variables;

public class Test {

     static int a,b; // static variable s
    
     public Test ( int a1, int b1){
          
           a = a1;
           b = b1;
     }
    
     public void disp (){
          
           System.out.println( " a = " +a);
           System.out.println( " b = " +b);
     }
    
    
     public static void main(String args[]){
          
           Test t1,t2;
          
           t1 = new Test(10,20);
           t2 = new Test (30,40);
          
           t1.disp();
           t2.disp();
     }
    
}

OUTPUT :
 a = 30
 b = 40
 a = 30
 b = 40

Program 2 :

package org.javanoobs.variables;

public class Test2 {

     int a,b// Instance variables
     static int c; //static variable.
    
     // Instance block..
     {
           System.out.println(" Inside Instance Block !!! ");
           System.out.println(" C " +c);
           System.out.println(" a " +a);
}

     static {
          
           System.out.println(" Static block is called !! ");
           System.out.println(" C " +c);
}
    
     // Create a method...
    
     public void disp(){
          
           int d = 1 ; // Local variable and
          
           d = a+b+c;
           System.out.println( " d after addition  " +d);
           System.out.println(" Disp method called... ");
     }
    
     public static void main(String args[]){
          
           Test2 t; // This is the reference variable.
          
           t = new Test2();
          
           t.disp();
          
     }
    
}

OUTPUT :

Static block is called !!
 C 0
 Inside Instance Block !!!
 C 0
 a 0
 d after addition  0
 Disp method called...