Basic Understanding of Java Programming Language

Java programming language was invented for two fundamental reasons:

  • to adapt the changing environments and used and
  • to implement improvements in the art of programming

Firstly it was called OAK and was designed by James Gosling, Patrick Naughton, Chris Warth, Ed Frank and Mike Sheridan at Sun Microsystems in 1991. Java is not the “Internet version of C++”. Java was not designed to replace C++, but to solve a different set of problems. Java is now used to create web pages with dynamic and interactive  content, to develop large-scale enterprise applications, to enhance the functionality of world wide web servers, to provide apps for consumer devices and so on.

Java is purely object-oriented programming language. To understand more about Object Oriented Programming Structures, see our previous article on Object Oriented Programming – Theory and Examples.

Features of Java Programming Language

These unique features of Java made it popular than C++.

Java is simple, object-oriented, distributed, robust, secure, architecture-neutral, portable, high-performance, multithreaded and dynamic language.

Java Logo

Java Logo

Some unique features of java that made it more popular than C++ are:

  1. Simplicity: Java is designed to be easy for the professional programmer to learn and use. Simple syntax are used in java which make it easier to write bug free code than in C++.
  2. Object-oriented: A clean, usable, pragmatic approach to objects, not restricted by the need for compatibility with other languages.Java defines data as objects with methods that support the objects. Every concept except some basic types like number, boolean, are implemented as objects.
  3. Robust: Java restricts the programmer to find the mistakes early, performs compile-time(strong typing) and runtime(exception handling) checks, manages memory automatically. It is easier to write, bug free code than in C++. Example, in C++, programmer should handle memory manipulations, but java itself handles memory allocation/de-allocation via Automatic Garbage Collection. Hence there is no chance of error due to improper memory usage in Java.
  4. Multithreaded: Java supports multi-threaded programming for writing program that perform concurrent computations. Multithreading enables an application to be more interactive, responsive and real-time.
  5. Architectural-neutral and portable: Java virtual machine provides a platform independent environment for the execution of java bytecode. When a java program(.java file) is compiled it generates an architecture-neutral object file capable of executing on different platforms which support JVM. The java compiler does this by generating byte code instructions which have nothing to do with particular computer architecture. But the bytecode instructions are interpreted on any machine and easily translated into native machine code on the fly. Java’s portability feature supports write once and run everywhere principle.
  6. Interpreted and high-performance: Java programs are compiled into an intermediate representation bytecode:
    1.  can be later interpreted by any JVM
    2.  can also be translated into the native machine code for efficiency.
      The Java interpreter can execute java byte codes directly on any machine to which the interpreter has been ported.
  7. Distributed: Java handles TCP/IP protocols, accessing a resource through its URL much like accessing a local file.
  8. Dynamic: Java supports substantial amounts of run-time type information to verify and resolve access to objects at run-time. Libraries can freely add new methods and instance variables. This feature is important when code needs to be added in running program.
  9. Secure: Programs are confined to the java execution environment and cannot access other parts of the computer. Java enables construction of virus-free, tamper-free systems.

Structure of a Java Program

Here is a code which will print out “Hello World” on the screen

/**
*This is a sample java program
*
*Save this file as Welcome.java
*
*/

class Welcome {
//  A java program will start from here.
	public static void main(String args[]){
		System.out.println("  Welcome to The Computer Students!!! ");
	}
}

Dissecting the Main Method:

public static void main(String[] args) {
   // Method body
}

public, static, void – are Java modifiers.
These three words are what we would call Java keywords, meaning that these words are already pre-defined in Java and you cannot use them as variable names or method names (you can use them as part of the full name, however). The three words here describe what type of method main is, the answer to which is, public, static, and void.

public – visible to other classes. Java programs usually incorporate multiple class files, so if you want to refer to code in the class from other classes, you make it public. (The opposite is private).

static – means that this method belongs to the Class, not an instance of the class. Recall that classes are blueprints for creating objects. The main method is the first piece of code that runs. Since we have no way of creating theHelloWorld object to call its main method, it has to be static. If that doesn’t make sense, don’t worry too much about it. We will discuss it in detail later.

void – whenever we call a method (call means to ask for it to run), we can ask to receive a value back. Void means that no value will be returned. For example, if we have a method that adds two values together, then we would not write void, but int, so that the method can return an int value representing the sum.

Compiling and Running Java Program

After writing java programs, we need to compile and run the java source code to get the output. To compile java program you should first open the command prompt or terminal and then type the command javac <program_name>.java.

javac Welcome.java for the above example.

Note: Terminal should locate to the path where java files are saved. Like in figure below, the source code was in Desktop of Luzan’s computer so he had to set terminal location to desktop. Using cd command. See for more commands for windows command prompt.

The javac compiler will create a class file called Welcome.class that contains only bytecodes. These bytecodes have to be interpreted by a Java Virtual Machine(JVM) that will convert the bytecodes into machine codes. Once we successfully compiled the program, we need to run the program in order to get the output. So this can be done by the java interpreter called java. In the command line type as shown here.

c:\>java Welcome for windows

$java Welcome for Linux

Then you’ll see output like this

Compiling and running Java Programs

Compiling and running Java Programs

Syntactic characteristics of java program

On the most basic level, Java programs consist of whitespaces, identifiers, comments, literals, separators, keywords and operators.

Whitespaces in java

A whitespace is a space, tab or new line. Java is a form-free language that does not require special indentation. A program could be written like this:

class MyProgram{ 
	public static void main(String[] args){ 
		System.out.println(“Hello World!”);
	}
}   

It could also be written like this:

class MyProgram        { 
	public static void main(String[] args){ 
		System.out.println      (“Hello World!”);
	}
}   

 

Identifiers in java

In Java identifiers are:

  1. a) used for class names, method names, variable names,
  2. b) any sequence of letters, digits, underscore or dollar($) characters which do not begin with a digit.

Java is case sensitive, so value, Value and VALUE are all different. In the given program italic words are all identifiers.

class MyProgram{ 
	public static void main(String[] args){ 
		System.out.println(“Hello World!”);
	}
}   

Comments in java

The bold characters in the following listing are comments.

/** 
 * The HelloWorldApp class implements an application that
 * simply displays "Hello World!" to the standard output.
 */
class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!"); //Display the string.
    }
}

The Java language supports three kinds of comments:
/* text */ 
The compiler ignores everything from /* to */.

/** documentation */
This indicates a documentation comment (doc comment, for short). The compiler ignores this kind of comment, just like it ignores comments that use /* and */. The JDK javadoc tool uses doc comments when preparing automatically generated documentation. For more information on javadoc, see the Java tool documentation.

// text
The compiler ignores everything from // to the end of the line.

Literals in java

A literal is a constant value of certain type. It can be used anywhere, where values of this type is allowed.

Examples:

  1. 100
  2. 98.6
  3. ‘X’
  4. “test”
class MyProgram{ 
	public static void main(String[] args){ 
		System.out.println(“My first Java Program.”);
	}
}   

Download PDF file on Basic understanding of java programming language with source code and examples.

This article gives basic understanding of Java Programming Language.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>