Many characteristics of Java are derived from c++, which in turn are derived from C. If you are familiar with C programming, you will find that, Java's syntax is similar to that of C. In fact, this module will reuse some of the examples available in our C module. If you are wondering what is the need for Java, while the 'C' language has been extremely powerful and a popular one. Well, let us try to answer that question first.

Platform independent - Java is independent of platform/CPU. Java source code once compiled generates byte code, which is portable and can be executed on variety of Operating systems/architectures.

Figure: Running JVM (Java Virtual Machine) on Various Devices

JVM - Java virtual machine, which is available in all the platforms that supports Java, converts the byte code into something that the platform understands. Thus, when we develop an application in Java, we don't have to worry about the platform on which it would be run, as JVM would automatically take care of the platform dependencies. Along with platform independent, features like portability, Object oriented programming language, Multi-threaded and few other features made Java what it is today.

C is a procedural programming language. In procedural programming, a program contains a series of procedure calls (aka function calls in C programming terminology). When the complexity and the size of the program grows, procedural programming has its own problems. This does not mean, complex applications cannot be developed in C language, but it comes with its own limitations and complexities. On the other hand Java is an Object Oriented Programming (OOP) language; OOP overcomes some of the limitations that are seen with procedural programming. Thus, support for object oriented programming, portability and a rich set of functionality offered by Java, is what makes Java a very popular language.

There are various features of OOP: (1) Abstraction, (2) Polymorphism, (3) Inheritance, and (4) Encapsulation. We will talk about each of these features in detail, in the coming sections. We will be using Eclipse through out this module, to execute the example programs.

First program in Java

  * My first program in Java.
  * Name this source file as "".
 class MyFirstProgram {
     // Program's in Java starts with a call to main().
     public static void main(String args[]) {
         System.out.println("My first Java program.");

Before we run the above program, let us see what are the various components of the program. Compile/Run this program and here is the output:

Figure: Example of a Simple Java Program

 Various Types of Comments in Java.
 /* Multi line comments */    - As you see in the above example. All the text between /* and */ would be ignored
 			        by the compiler.

 // Single line comments      - This is used to comment a single in line in the source file.

 /** documentation comment /  - There is another kind of comment we haven't used yet, called documentation comment.

You might have observed, in the above example, both the class and the source file has the same name. This is not a coincidence, Java requires the name of the source file to match the class name. We have used the keyword "Class" in the above example, we will see what a "class" is in the next section. A "keyword" is something that cannot be the name of a variable/class/method etc. "class" is a keyword, this means you cannot have a variable/method with the name "class". There are quite a few key words defined in Java, we will see them as and when we use them.

comments powered by Disqus