Java Tutorial, Java, Core Java, Core Java Tutorial, Learn Core Java, Introduction to java
New Document
Java Tutorial - Introduction to Java
Java is an object-oriented programming language with a built-in application programming interface (API) that can handle graphics and user interfaces and that can be used to create applications or applets. Because of its rich set of API's, similar to Macintosh and Windows, and its platform independence, Java can also be thought of as a platform in itself. Java also has standard libraries for doing mathematics.
Much of the syntax of Java is the same as C and C++. One major difference is that Java does not have pointers. However, the biggest difference is that you must write object oriented code in Java. Procedural pieces of code can only be embedded in objects. In the following we assume that the reader has some familiarity with a programming language. In particular, some familiarity with the syntax of C/C++ is useful.
In Java we distinguish between applications, which are programs that perform the same functions as those written in other programming languages, and applets, which are programs that can be embedded in a Web page and accessed over the Internet. Our initial focus will be on writing applications. When a program is compiled, a byte code is produced that can be read and executed by any platform that can run Java.
Java is a platform independent language
Java is most widely used application programming language because of its platform independence also called as "write once run anywhere" feature which allows java programs to run any kind of platform, operating system or machine. To understand how Java achieves platform independence it's important to understand how java works and how java codes gets compiled and executed.
First Step - Writing Java code
For writing java code you can use any text editor including notepad, word pad or edit plus. While writing your java program make sure it must have one public class and one public static void main method which allow it to run as java program when invoked in Java Virtual Machine. After completing coding just saves your file with .java extension and you are ready for next step of compiling your Java program.
/* Hello World Java Program */

public class Helloworld {
     public static void main(String[] args) {
        System.out.println("Hello World!");

Hello World!

Compiling Java code
For compiling java code you must need a java compiler which comes along with Java Development Kit also called as JDK. You can download and install JDK from Sun Microsystems now Oracle's site. Once you are ready with your java compiler you can compile your java code by invoking "javac" command from command line and it will compile your java source file into class file which has .class extension. This Class file is actually java's way to achieve platform independence. This class file contains JVM specific byte code instead of machine code unlike the C or C++ which makes it possible to run java program in any platform.
Running Java application
Now we are ready to run our java application and in the process we will understand important link in java platform independence. We know that after compilation java compiler creates .class file which contains JVM specific byte codes and to execute those byte code we need a piece of software called Java Virtual Machine or JVM. JVM is also known as Java Runtime or JRE. Byte codes of java programs executed inside Java Virtual Machine which is platform dependence, you here it correctly JVM are indeed platform dependent and it has different binary for different platform which you can down load from Sun's site. JVM also comes with JDK or you can download it separately.
It's important to know that JVM or Java Virtual machine doesn't have any information regarding the programming language. It knows only binary format of the byte code and ensures that the class file generated by java compiler is as per Java Byte Code specification thus eliminating dangers of running a malicious and manipulated java byte codes. We can also generate byte code that adheres to this format in any programming language which makes it easy to write java compiler and java virtual machine in ay programming language.
Since every Java program runs within the boundaries defined by Java Virtual machine or JVM it provides inherent Security. The code run inside the JVM can not go beyond the security constraints defined by the Java Virtual Machine. This is why java application are considered as secure applications over internet and Java applets are most widely used programming platform for internet. Apart from Security Java Virtual machine also provides memory management via Garbage collection which enables java programmer or java developer to focus more on business logic rather than worrying about system aspects like allocating and reclaiming memory.
In conclusion with the use of class file, byte code and Java virtual machine, Java achieves its platform independence and makes development easy for products suppose to run in different platforms.

Back to Top