11.1 Java

Java is a high-level, third generation programming language, like C, C++, Fortran, Smalltalk, Perl and many others. You can use Java to write computer applications that crunch numbers, process words, play games, store data or do any of the thousands of other things that computer software can do.

Compared to other programming languages, Java is most similar to C. However, although Java shares much of C's syntax, it is not C. Knowing how to program in C or, better yet, C++,will certainly help you to learn Java more quickly, but you do not need to know C to learn Java. Unlike C++ Java is not a superset of C. A Java compiler would not compile C code, and most large C programs need to be changed substantially before they can become Java programs.

What's most special about Java compared to other programming languages is that it lets you write special programs called applets that can be downloaded from the Internet and played safely within a web browser. Traditional computer programs have far too much access to your system to be downloaded and executed directly. Although you generally trust the maintainers of various ftp archives and bulletin boards to do basic virus checking and not to post destructive software, a lot still slips through the cracks. Even more dangerous software would be distributed if any web page you visited could run programs on your system. You have no way of checking these programs for bugs or for malicious behaviour before downloading and running them.

Java solves this problem by severely restricting what an applet can do. A Java applet cannot write to your hard disk without your permission. It cannot write to arbitrary addresses in memory and thereby introduce a virus into your computer. It should not crash your system.

11.1.1 Brief History

Java is a programming language created by James Gosling from Sun Microsystems in 1991. The first publically available version of Java (Java 1.0) was released in 1995. Over the time several versions of Java were released, which enhanced the language and its libraries. The current version of Java is Java 1.6, also known as Java 6.0.

From the Java programming language, the Java platform evolved. The Java platform allows some code written in a language other than the Java programming language and still runs on the Java Virtual Machine (JVM).

11.1.2 Overview

The Java programming language is defined by the Java compiler, the Java virtual machine and the Java class libraries. The JVM is a software implementation of a virtual computer that executes programs like a real machine.

The Java compiler translates a source Java code into an intermediate form called byte-codes. The JVM interprets these byte-codes and executes the program. It is written specifically for specific operating system and processor architecture.

The Java run-time environment (JRE) consists of the JVM and the Java class libraries.

11.1.3 Characteristics of Java

The Java language aims at providing a platform for writing a program once and then executes this program on multiple operating systems. To this end, Java has the following properties:

Platform independent: Java programs use the Java virtual machine as an abstraction and do not access the operating system directly. This makes Java programs highly portable. A Java program which is standard complaint and follows certain rules can run unmodified on several platforms, e.g. Windows or Linux.

Object-oriented programming language: Except the primitive data types like integer or float, all elements in Java are objects.

Strongly typed programming language: Java is strongly typed, which means that the types of the variables used must be pre-defined and conversion to other objects is relatively strict, i.e. must be done in most cases by the programmer. There is almost no automatic type conversion as in C.

Interpreted and compiled language: Java source code is translated into byte-code which does not depend on the target platform. This byte-code will be interpreted by the JVM. The JVM contains a compiler, called Hotspot-compiler, which translates critical byte-code into native machine code.

Automatic memory management : Java manages the memory allocation and deallocation for creating new objects. The program does not have direct access to the memory. A garbage collector within the run-time system automatically recovers memory assigned to object to which no active pointer exists. The Java garbage collector runs in the background, keeping track of which objects the application no longer needs and reclaiming memory from them. This approach to memory handling is called implicit memory management because it does not require you to write any memory-handling code. Garbage collection is one of the essential features of Java platform.

The Java syntax is similar to C++. Java is case sensitive, e.g. the variables myValue and myvalue will be treated as different variables.

11.1.4 Development with Java

The programmer writes Java source code using a text editor which supports plain text. The editor may be a part of an IDE (Integrated Development Environment) which is normally used by a programmer for program development.

When the source code is ready, the programmer (or the IDE) calls the Java compiler (javac). The Java compiler creates platform-independent byte-codes. This byte-code is stored in “.class” files.

Byte-code can be executed by the JRE. The Java run-time environment is a program which executes the byte-code on the operating system. The JRE translates the byte-code into native code and executes it, e.g. the native code for Linux is different from the native code for Windows.

By default, the compiler puts each class file in the same directory as its source file. You can specify a separate destination directory with appropriate command option.

Classpath: The classpath defines where the compiler and interpreter look for .class files to load. The classpath in Java defines which Java class is available for your Java program. For example, if you want to use an external Java library you have to add the path to this library to your classpath to use it in your program.

11.1.5 The First Java Program

It has become a tradition to illustrate a computer programming language by writing a “Hello World” program and we follow that tradition here.

public class HelloWorld {
  public static void main(String[] args) { 
    System.out.println(″Hello World″);
  }
}

11.1.6 Type Conversion

As mentioned above Java is a strongly typed language and if you need to change the type of variables Java generally requires an explicit conversion. The following examples illustrate such conversions.

Conversion to String

The following examples show conversion from other types to Strings:

// Convert from int to String
String s1 = String.valueOf(10);    // ″10″
// Convert from double to String
String.valueOf(Math.PI);	   // ″3.141592653589793″
// Convert from Boolean to String
String s3 = String.valueOf(1 < 2); // ″true″
// Convert from date to String
String s4 = String.valueOf(new Date()); // ″Fri Apr 22 11:28:08 IST 2011″

The following examples show conversion from String to Number:

// Conversion from String to int
int i = Integer.parseInt(String);
// Conversion from float to int
float f = Float.parseFloat(String);
// Conversion from double to int
double d = Double.parseDouble(String);

We have already discussed the nature of JVM in Chapter 8 which may be consulted at this stage.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.137.38