Wednesday, 26 June 2013

Java Basics

Hi friend Good morning! Welcome to our new session. But don't think features of Java post is over. I will update it time to time.


Today we are going know things that are needed to understand our first Java program. A person who already know Java may not need this post.

Generally you are going to write a program to instruct computer to perform your desired task. The program written by you simply contains code written in English. If a language code is written in plain english, easily understandable format you can say that it is an example for high level language. Java is a high level language!

Also remember that the code written by you is known as source code. Every file in computer system contains some extension so that it can be easily identified by OS. For instance the image files in your computer System will have .jpg extension and the audio files generally have .mp3 extension. Like the same way a C program will have .c extension and a Java program contains .java extension.

We are going to write our code in this .java extension file. But here is a catch. If you are going to write your code in english words (of course you have to follow language rules and syntaxes) then how computer is going to understand your commands because it doesn't know other than binary language or machine language.
So you need a mediator who can convert your code in machine understandable format.

Translators are needed to convert your code from one format (high level) to another (machine format). There are two types of translators. They are Compilers and Interpretors. Compiler is going to translate whole code into machine format at once. Whereas interpretor is going to convert it line by line.

In Java compiler is used to translate our code into Byte code. Byte code in Java is having the extension .class. But byte code cannot be executed by our system directly. For this Java provided JVM (Java Virtual Machine). JVM is going to execute the byte code directly and we will get our results. That means JVM is also one type of translator. Of course it has many reponsibilities. But remember JVM executes code line by line, that means it is an interpretor.

We already discussed that Java is platform independent. Here in this post we will see how this is happening. We came to know that after compiling we are getting one file known as .class file. This class file is machine independent. That means if you are going to compile a Java program on Windows XP, you will get one .class file. The same .class file you can copy and place on another system like LINUX. You don't need to chance any thing. The JVM present on linux system can execute this .class file and it will give results on the fly.

Here is a tricky question asked in interviews.

Is JVM platform independent?

Don't get confused. I am asking about JVM and not Java. Actually JVM is machine dependent. For every operating system Oracle provided separate JVM so that it can easily interact with OS. That's why in my first post I told you to download one software which is specific to your need like JDK1.7x86.exe. For linux you have to download separate file. This JVM is provided inside this software. So Oracle provided separate software depending upon your OS. This clearly shows that JVM is platform dependent.


Hope the idea regarding JVM is somewhat clear to you now. Let me know if you are still confusing.

I will deal with program in the next post. 

No comments:

Post a Comment