Monday, September 5, 2011

points to remember about Java

Hi all


points to remember about Java
1) Java is a programming language as well as a Platform.
2) Machine code is the sequences of binary 1 & 0's that the processor understands.

JVM

1) Editor – To type your program into , a notepad could be used for this
2) Compiler – To convert your high language program  into native machine code
3) Linker – To combine different program files  reference in your main program together.
4) Loader – To load the files from your secondary storage device like Hard Disk , Flash Drive , CD into RAM for execution. The loading is automatically done when your execute your code.
5) Execution – Actual execution of the code which is handled by your OS & processor.

why is Java both interpreted and complied language ?
Programming languages are classifies as
Higher Level Language Ex. C++ , Java
Middle Level Languages Ex. C
Low Level Language  Ex Assembly
& finally the lowest level as the Machine Language.

A compiler is a program which converts a program from one level of language to another. Example conversion of C++ program into machine code.
The java compiler is a convert’s high level java code into bytecode (which is also a type of machine code).
A interpreter is a program which converts a program at one level to another programming language at  the same level. Example conversion of Java program into C++
 In Java , the Just In Time Code generator converts the bytecode into the native machine code which are at the same programming levels.
 Hence java is both compiled as well as interpreted language.

why is Java slow ?
The two main reasons behind the slowness of Java are

 1) Dynamic Linking = Unlike C, linking is done at run-time , every time the program is run in Java.
 2) Run-time Interpreter =  The conversion of byte code into native machine code is done at run-time in Java which furthers slows down the speed.

Unstructured Programming Languages:  The most primitive of all programming langauges having sequentially flow of control.  Code is repeated through out the program

Structured Programming Languages: Has non-sequentially flow of control. Use of functions allows for re-use of code.

Object Oriented Programming: Combines Data & Action Together.

Java Abstraction:

Abstraction in the process of selecting important data sets for an Object in your software , and leaving out the insignificant ones.
Once you have modeled your object using Abstraction , the same set of data could be used in different applications.

Java Encapsulation:

With Encapsualtion you can hide (restrict access) to critical data members in your code , which improves security
Encapsualtion combines data and actions together (just like a capsule).

Inheritance & Ploymorphism :

Inheritance: In simple words , Inheritance is way to define new a class , using classes which have already been defined.

Ploymorphism: Is the ability of methods to behave differently , based on the object calling it.

Integer data types
byte (1 byte)
short (2 bytes)
int (4 bytes)
long (8 bytes)

What is the difference between a  Class &  Object ?
A class is a blueprint or prototype that defines the variables and the methods (functions) common to all objects of a certain kind.

An object is a specimen of a class. Software objects are often used to model real-world objects you find in everyday life.

Command Line Arguments
During program execution, information passed following a programs name in the command line is called Command Line Arguments.

1)Command Line Arguments can be used to specify configuration information while launching your application.
2)There is no restriction on the number of command line arguments.You can specify any number of arguments
3)Information is passed as Strings.
4)They are captured into the String argument of your main method

class Demo{
     public static void main(String b[]){
         System.out.println("Argument one = "+b[0]);
         System.out.println("Argument two = "+b[1]);
    }
}

Run the code as javac Demo java programming

"this" keyword
1) "this" is a reference to the current object, whose method is being called upon.
2) You can use "this" keyword to avoid naming conflicts in the method/constructor of your instance/object.


public class ThisDemo {
int a;
int b;

public void setData(int a ,int b){
         a = a;  // this.a = a;
 b = b;       // this.b = b;
}
public void showData(){
  System.out.println("Value of A ="+a);
  System.out.println("Value of B ="+b);
}
public static void main(String args[]){
ThisDemo obj = new ThisDemo();
  obj.setData(2,3);
  obj.showData();
}
}
Run the above program Result will come 0 , 0
remove the a =a; & b=b; then add this.a = a; & this.b = b;
Run the above program Result will come 2, 3

Garbage Collection
In the Java programming language, dynamic allocation of objects is achieved using the new operator. An object once created uses some memory and the memory remains allocated till there are references for the use of the object. When there are no references for an object, it is assumed to be no longer needed and the memory occupied by the object can be reclaimed.

There is no explicit need to destroy an object as java handles the de-allocation automatically. The technique that accomplishes this is known as Garbage Collection.
Programs that do not de-allocate memory can eventually crash when there is no memory left in the system to allocate. These programs are said to have memory leaks

In Java,Garbage collection happens automatically during the lifetime of a java program, eliminating the need to de-allocate memory and avoiding memory leaks.

In C language, it is the programmer’s responsibility to de-allocate memory allocated dynamically using free() function.

Note : All objects are created in Heap Section of memory. More on this in a later tutorial.

1) If you want to make your object eligible for Garbage Collection , assign its reference variable to null.
2) Primitive types are not objects. They cannot be assigned null.

Stack and Heap
The JVM divided the memory into following sections. This division of memory is required for its effective management.

1)Heap    - The Heap section contains Objects (may also contain reference variables).
2)Stack   - The Stack section of memory contains methods, local variables and reference variables.
3)Code    - The code section contains your bytecode.
4)Static  - The Static section contains Static data/methods.

Note:
When a method is called , a frame is created on the top of stack.
Once a method has completed execution , flow of control returns to the calling method and its corresponding stack frame is flushed.
Local variables are created in the stack
Instance variables are created in the heap & are part of the object they belong to.
Reference variables are created in the stack.

difference between instance variables and local variables

Instance variables are declared inside a class but not inside a method
  class Student{
    int num; // num is  instance variable
    public void showData{}

Local variables are declared inside a method including method arguments.
   public void sum(int a){
       int x = int a +  3;
      // a , x are local variables
   }

Inheritance
The parent class is termed super class and the inherited class is the sub class.
The keyword extends is used by the sub class to inherit the features of super class.
Inheritance is important since it leads to reusability of code

class Doctor
// Instance Variables and Methods for the Doctor Class
}
class Surgeon extends Doctor{
// Inherits instance variables & methods of the doctor class
//may have variables and methods of its own.
}

method overriding
Redefining a super class method in a sub class is called method overriding

Rules for Method Overriding
The method signature i.e. method name, parameter list and return type have to match exactly.
The overridden method can widen the accessibility but not narrow it, i.e. if it is private in the base class, the child class can make it public but not vice versa.

Doctor doctorObj = new Doctor()
doctorObj.treatPatient()
// treatPatient method in class Doctor will be executed
Surgeon surgeonObj = new Surgeon();
surgeonObj.treatPatient()
// treatPatient  method in class Surgeon  will be executed

dynamic polymorphism.
A reference variable of the  super class can refer to a sub class object
Doctor obj = new Surgeon();

Consider the statement
obj.treatPatient();
Here the reference variable "obj" is of the parent class , but the object it is poiting to is of the child class (as show in diagram).
obj.treatPatient() will execute treatPatient() method of the sub-class - Surgeon
If a base class reference is used to call a method, the method to be invoked is decided by the JVM, depending on the object the reference is pointing to
For example, even though obj is a reference to Doctor, it calls the method of Surgeon, as it points to a Surgeon object
This is decided during run-time and hence termed dynamic or run-time polymorphism

super
What if the treatPatient method in the Surgeon class wants to do the functionality defined in Doctor class and then perform its own specific functionality?
In this case keyword super can be used to access methods of the parent class from the child class.
The treatPatient method in the Surgeon class could be written as:

treatPatient(){
super.treatPatient();
//add code specific to Surgeon
}

What is the difference method overloading & method overriding ?
Method overloading:
Method overloading is in the same class , where more than one method have the same name but different signatures
Ex
void sum (int a , int b);
void sum (int a , int b, int c);
void sum (float a, double b);

Method overriding : 
Method overriding is when one of the methods in the super class is redefined in the sub-class. In this case the signature of the method remains the same.
Ex
class X{
    public int sum(){
        // some code
    }
}
class Y extends X{
     public int sum(){
        //overridden method
       //signature is same
    }
}

What is the difference between static & Dynamic polymorphism ?
Static Polymorphism :  It relates to method overloading .Errors ,if any, are resolved at compile time. Since the code is not executed during execution , the name static.
Ex:
void sum (int a , int b);
void sum (float a, double b);
int sum (int a, int b); //compiler gives error.

Dynamic Polymorphism: It relates to method overriding.
In case a reference variable is calling an overridden method, the method to be invoked is determined by the object ,your reference variable is pointing to. This is can be only determined at run -time when code in under execution , hence the name dynamic.





No comments:

Post a Comment