Monday, February 3, 2014

Java language

Java is a high-level, general-purpose, object-oriented programming language. The main design goals of the language were robustness, portability, high performance and security. Java is a multithreaded and distributed programming language. It can be used to create console applications, GUI applications, web applications, both on PCs or embedded systems.

Java is a programming language created by Sun Microsystems in 1991. The first publicly available version of Java was released in 1995. Today, the language is developed by Oracle corporation.
Java excels in creating portable mobile applications, programming various appliances and in creating enterprise applications.

Popularity

There are currently several widely used programming languages. Java belongs to the most popular languages today. Several surveys put it into the top three languages in the world.

Java platforms

Java has four programming platforms:
  • Java Platform, Standard Edition (Java SE)
  • Java Platform, Enterprise Edition (Java EE)
  • Java Platform, Micro Edition (Java ME)
  • JavaFX
All Java platforms consist of a Java Virtual Machine (JVM) and an application programming interface (API). The Java Virtual Machine is a program, for a particular hardware and software platform, that runs Java applications. An API is a collection of software components that we can use to create other software components or applications.

Java SE is used for developing desktop applications. Java SE's API provides the core functionality of the Java programming language. It consists of a virtual machine, development tools, deployment technologies, and other class libraries and toolkits used in Java applications. Java EE is built on top of the Java SE platform. The Java EE platform provides an API and runtime environment for developing and running web applications and large-scale, multi-tiered, scalable, reliable, and secure enterprise applications. Java ME is a subset of the Java SE. It provides an API and a small-footprint virtual machine for running Java applications on small devices, like mobile phones. JavaFX is a platform for creating rich internet applications using a lightweight user-interface API. 

In our tutorial, we use the Java SE platform to create simple console applications.

JDK

Strictly speaking, Java SE is a platform specification. Java Platform, Standard Edition Development Kit (JDK) is an official implementation of the Java SE by Oracle. There are also other implementations. For example free and open source OpenJDK or IBM's J9.
$ ls jdk1.7.0_02/
bin        db       jre  LICENSE  README.html  src.zip
COPYRIGHT  include  lib  man      release      THIRDPARTYLICENSEREADME.txt
After we dowload and unpack the Oracles's JDK, we can see the contents of the JDK in the our jdk1.7.0_02 directory. The development tools are located in the bin/ subdirectory. The Java javac compiler and the java application launcher are located in this subdirectory. The jre/ subdirectory contains the JVM, class libraries and other files that help execute Java programs. The lib/ subdirectory has some additional class libraries and support files. The db/ subdirectory contains the Java DB, which is the Oracle's distribution of the Apache Derby database. In the include/ subdirectory we can find header files that support native-code programming. The src.zip file contains source files for all classes that make up the Java core API.

JVM

Java virtual machine (JVM) is a program that can execute Java bytecode. The JVM is included in the JDK. Java source code is written in files with the .java extension. The javac Java compiler will compile the Java source code into the Java bytecode; the compiled files have the .class extension. This bytecode is executed by JVM. The java tool is a launcher for Java applications. Oracle's JVM is called HotSpot. HotSpot is a Java virtual machine for desktops and servers. It has advanced techniques such as just-in-time compilation and adaptive optimization designed to improve performance.

Compiling a simple program

In order to develop Java applications, we need to dowloand a JDK. Oracle's official JDK can be downloaded from this dowload page.
$ mkdir -p com/zetcode
Inside the current working directory, we create a com/zetcode/ subdirectory. Java source files are organized in modules called packages. The packages must match the directory structure.
$ touch com/zetcode/SimpleExample.java
A SimpleExample.java source file is created in the com/zetcode/ subdirectory. Java source files have a .java extension.
package com.zetcode;

public class SimpleExample {

    public static void main(String[] args) {

        System.out.println("This is simple Java example.");
    }
}
This is a source code for a simple Java example. This example prints a message to the console.
package com.zetcode; 
The package name must correspond to the directory structure in which the source file is located.
public class SimpleExample { 
The public class name is required to match the file name.
$ javac com/zetcode/SimpleExample.java 
Using the javac compiler, we compile the source code.
$ ls com/zetcode/
SimpleExample.class  SimpleExample.java
The compiler generetes a Java bytecode, which is executed by the Java Virtual Machine. The bytecode has a .class extension.
$ java com.zetcode.SimpleExample 
This is simple Java example.
With the java application launcher, we execute the program. It starts a Java runtime environment, loading a specified class, and invoking that class's main method. The .class extension is excluded; it is assumed. The program name is a fully qualified name of the program - com.zetcode.SimpleExample. It includes the name of the program and its package.

No comments:

Post a Comment