Tutorials

Monday, March 14, 2011

Why Java for web applications?(Must read for java programmers)

What exactly a an web application is?

A web application is an application which can be accessed over a network like internet or intranet.

So,think of some common web applications......

yes,it may be anything like any content which we access from a server through our client applications i.e the web browser.Consider the a social networking application.Here you are accessing some contents over the internet.

So now as you are clear about web applications let us think up of some languages using which we can impliment them.Those are C,C++,PHP,Java etc.So let us find out the causes, why Java dominates other languages when it comes to web application.

What are the special features of java?

1.Platform Indeependent(Architecture neutral)

2.Object Oriented

3.Security

3.Robust

4.Multithreading Support

Before discussing  about the above points let us have a look at the lifecycle of a java program.

Java-lifecycle2

or in details

Csprogram1

  • A java program is written using any  text editor like notepad or some IDEs(Integreted Developing Environments) like Eclipse or Netbeans etc. and are saved as .java file.(program.java)
  • Then the program is compiled using the java compiler and converted to .class(program.class)file.And which is nothing but the bytecode of the java program.The .class file will run in any machine indeependent of it's architecture.(Magic of byte code is discussed shortly)
  • The class file(program.class) is now interpreted by the JVM(Java Virtual Machine) of the partcular platform which is a part of JRE(Java Runtime Environment) software.(The architecture of the JVM is discussed shortly)
  • So,we can conclude that the JVM is platform dependent but the bytecode is platform indeependent.

Now,what is the difference between JDK and JRE?

Ans:

JDK(Java Developement Kit) is a bundle of software that you can use to develop Java based software. JDK provides developers with tools like compiler ( javac), document generator ( javadoc) etc…

JRE is an implimentation of the JVM which is used to run a java program.Typically, each JDK contains one (or more) JRE’s along with the various developement tools like like the Java source compilers, bundling and deployment tools, debuggers, development libraries, etc.

So let us understand what is the meaning of Platform indeependent.

software or other technology that "doesn't care" what kind of computer the end user has. Platform-independent applications may run on any kind of computer, or may not rely on the enduser's system for anything other than.

Analyze the above definition with reference to Internet?

Internet has got a large number of users having different set of platforms like operating systems(mac,windows,UNIX,Solaris),web browser(Firefox,Internet Explorer,Netscape,Saffari,Opera,Chrome,Epic,Thunderbard etc) etc.So different platforms obey diffrent set of rules for executing a program.That is why the same c program running on windows shows different behaviour in UNIX.So we have to write different set of codes for both, which increases the programming effort.That is infact more harsh and painful while developing web applications because we can not afford to write different codes for different set of user architectures.Is it possible?So languages C,C++,Fortan failed in this aspect.With reference to the diagram below let's understand the difference between a C program and a java program.

9iv6a
So here comes the utility of java's platform indeependent behaviour.Java generates a .class file i.e the bytecode which is the intermediate form of the code.The bytecode for all java programs are same in all platforms and can be interpreted by all JVMs related to all platforms.So the bytecode can be run in any machine where that platform compatible JVM is installed.So to run a java program you need to have a JVM only and the effort of writting codes is reduced because the same source code can be executed in all Platforms.But the same is not possible using C or C++.But the interpretation takes a bit more time then compilation.In recent versions of Java Just in time compilation(JIT) is added to compensate the time upto some extents.

For internet users just we need to send the compiled class files and it can be executed easily.Now let's have a look at the architecture of a JVM and how it incorporates these things.

Fig5-1
We will not go into deep of each part.

Each time a Java Application is executed then an instance of JVM ,responsible for its running,is created.A JVM instance is described in terms of subsystems, memory areas, data types, and instructions.There can be many instances of JVM created,which can hadle more then one java programes at a time.This concept is used in Multithreading.Each instance of the Java virtual machine has one method area and one heap.

When a Java virtual machine runs a program, it needs memory to store many things, including bytecodes and other information it extracts from loaded class files, objects the program instantiates, parameters to methods, return values, local variables, and intermediate results of computations. The Java virtual machine organizes the memory it needs to execute a program into several runtime data areas.

Object Oriented and Security

For web applications one of the most important requirement is security.A code in java is more secure than a code in C or C++.why?All of you must have heard about the fast,furious and dangerous pointers in C and C++.Using pointers a programmer can have access to any part of the system memory which is not secure, as it can also look up at your personal informations like credit card number,bank acc no etc.More ever you can not trust a program downloaded over internet because it may be a harmful one.But java has overcame this problem quite nicely and intelligently.There is no concept of pointers in Java.(that is why it is more object oriented then C++)But do not think this as a weakness of java because it has implimented some skills which will completely compensate pointer.It is designed in such a way that no java program can have access to the restricted ares.

Robust

In C and C++ the programmer must manually allocate and free dynamic memory.This may sometime leade to problems as  programmers sometime forget to free the memory or sometimes frees  up the required memory.Java virtually eliminates the problem by managing memory allocation and deallocation for you.(Garbage collection for unused objects)

 

These are the features of java which makes it the king of programming languages for web applications.Infact C# is mostly derived from Java.So,as a good programmer you should atleast know these things.

 

 

Have a nice programming experience and keep commenting.

 

 

 

No comments:

Post a Comment