Thursday 4 September 2014

java basic 1st part

·         Before we introduce the Java platform How about answering a very basic question. What is a PC?
·         Well, a dictionary definition is a, A Computer is an Electronic Device capable of performing computations. It has many peripherals like a monitor and keyword to accept or display data to user and memory to store information
·         But the heart of the computer is its processor, which does all the thinking for the computer.
In fact any device with a processor can be called a computer
·         You may have notice the Keyword Electronic in the entire description. If so. How can it understand and display text, images, and videos.  And other rich media information.
·         Well a computer understands only electronic signals where a , 5 volt current represents a binary 1 and a o volt current represents a binary 0
·         Your PC is continually bombarded with these electronic pulses which it interprets 8 bits of such signals are grouped together and text, numerical, symbols are identified by unique pattern of electric pulses. A symbol # is represented by 11111010.Instruction to ADD two numbers is represented by 10000011
·         This is 8-bit computing. Current day processors are capable of understanding and decoding 64 bits at a time
·         Suppose you are asked to write a program to ADD two numbers. Say add 1 with 2
·         Assembly Language is the most elementary form of software development languages
·         Your code to add two numbers in this language would like
·         Store number 1 at memory location say A
·         Store number 2 at memory location say B
·         Add contents of Location A & B
·         Store Results
·         The way you feed this code to the processor would depend on the PC you are using back in 1950′s when computers were huge and consumed great deal of power
·         You would convert your Assembly languages code into corresponding machine code of 1 and zeros using mapping sheets and punch the machine code into PUNCH cards and feed to the computer
·         With advancement in technology i/o devices were invented you could directly type your program into the PC using a program called Assembler convert it into corresponding machine code and feed to your processor
·         That apart, you will also have to make calls to creating Operating System provided functions to display output of code.
·         With advancement in software development languages This entire assembly code , could shrink into just one line print f 1+2
A software called compiler is used to convert your c language code into assembly code and the assembler converts it into corresponding machine code
·         Though present day compilers come bundled with assembler can directly convert your higher language code into machine code.
·         Now, suppose Windows operating system is running on this Intel processor Combination of Operating System plus the processor is called the platform
·         The most common platform in the world is the Windows and Intel called the Wintel Platform. The other popular platforms are AMD and Linux , Power PC and Mac OS X
·         Now, with change in processor , the assembly instructions will change For example the add instruction in Intel may be called ADDITION for AMD , or Math ADD for Power PC ,and obviously with change in Operating System , the level and nature OS level calls will also change
·         As a developer I want my software program to work on all platforms available, to maximize my revenues. So I would have to buy separate compilers which convert my print f command into the native machine code. But compilers come expensive and there is a chance of capability issues.
·         Enter Java language.
·         The code to display addition of two numbers is System dot out dot println 1 + 2 saved as .java file. Using the java compiler the code is converted into a intermediate code called the bytecode . The output is a .class file
·         This code is not understood by any platform but only a virtual platform called the Java Virtual Machine
·         This Virtual Machine resides in the RAM of your operating system
·         When the Virtual Machine is fed with this bytecode it identifies the platform it is working on and converts the bytecode into the native machine code
·         In fact while working on your PC or browsing a web whenever you see either of these icons be assured the java virtual machine is loaded in your ram
·         But what makes java lucrative is that code once compiled can run not only on all PC platforms
but also mobiles or other electronic gadgets supporting java
·         Hence , java is a language as well as a platform (JVM)
·         Java Virtual Machine:-
·         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.
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).
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
a) Dynamic Linking = Unlike C, linking is done at run-time , every time the program is run in Java.
b)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.

More:-
·         To understand the compiling process in Java ,lets first take a quick look at the linking process in C
·         Suppose in the main , you have called two functions f1&  f2
·         The main function is stored in file a1.c,function f1 is stored in file a2.c,function f2 is stored in file a3.c
·         All these files are fed to the compiler , whose output is the corresponding object files, which is the machine code
·         The linker will club(add) these 3 files together and ,produce a .exe file .During program run  a loader program will load a.exe into the RAM for execution
·         Lets look at the same process for java
·         In your main, you have called two methods f1 & f2.
·         main method is stored in file a1.java , f1 is stored in file a2.java ,f2 is stored in file a3.java
·         The compiler will compile the three files , and produce corresponding .class files which consists of the bytecode , unlike C , No linking is done
·         The JVM (Java Virtual Machine) resides on the RAM
·         During execution, using class loader the class files are brought on the RAM. The bytecode is verified for any for any security breaches using the byte code verifies of the JVM, Next the execution engine will convert the bytecode into native machine code.
·         This is just in time compiling
·         It is one of the main reasons why Java is comparatively slow


STACK AND HEAP AREA:-

The JVM divided the memory into following sections.
1.     Heap
2.     Stack
3.     Code
4.     Static
This division of memory is required for its effective management.

The code section contains your bytecode.
The Stack section of memory contains methods, local variables and reference variables.
The Heap section contains Objects (may also contain reference variables).
The Static section contains Static data/methods.
Of all of the above 4 sections, you need to understand the allocation of memory in Stack & Heap the most, since it will affect your programming efforts
difference between instance variables and local variables
Instance variables are declared inside a class but not inside a method
1
2
3
4
5
class Student{

<strong>int num; // num is  instance variable</strong>

public void showData{}
Local variables are declared inside method including method arguments.
1
2
3
4
5
6
7
public void sum(int a){

int x = int a +  3;

// a , x are local variables</strong>

}


Points to Remember:
·         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.
Point to Ponder: What if Object has a reference as its instance variable?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public static void main(String args[]){

A parent = new A();

//more code

}

class A{

B child = new B();

int e;

//more code

}

class B{

int c;

int d;

//more code

}























 Garbage Collection:-
In the Java programming language, dynamic allocation of objects is achieved using the newoperator. 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.
Assignment: To Learn Garbage Collection Mechanism in Java
Step 1) Copy the following code into a editor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Student{
int a;
int b;

  public void setData(int c,int d){
    a=c;
    b=d;
  }
  public void showData(){
    System.out.println("Value of a = "+a);
    System.out.println("Value of a = "+b);
  }
  public static void main(String args[]){
    Student s1 = new Student();
    Student s2 = new Student();
    s1.setData(1,2);
    s2.setData(3,4);
    s1.showData();
    s2.showData();
    //Student s3;
    //s3=s2;
    //s3.showData();
    //s2=null;
    //s3.showData();
    //s3=null;
    //s3.showData();
  }
}
Step 2) Save, Compile and Run the code. As shown in the diagram , two objects and two reference variables are created.