Java Programs






LECTURE NO: 01
Introduction of java language
 
 The first section of a Java program identifies the environment information. To do this, it specifies the classes or packages that will be referred to in the program. This information is specified with the help of the ‘import’ statement. A program may have more than one import statement. An example of an import statement is given below:
Java is a high-level, third generation programming language, like C, Fortran, Smalltalk, Perl, and many others.
 
                            
                    Brief History of Java Language
 
In January of 1991, James Gosling, Mike Sheradin, Patrick Naughton and several other individuals met to discuss the ideas for the Stealth Project.
in the consumer electronics market.
The vision of the project was to develop "smart" consumer electronic devices that could all be centrally controlled and programmed from a handheld-remote-control-like device.
Members of the Stealth Project, which later became known as the Green Project, divided the tasks amongst themselves.
Mike Sheradin was to focus on business development,
Patrick Naughton was to begin work on the graphics system, and
James Gosling was to identify the proper programming language for the project.
Gosling began with C++, but soon after was convinced that C++ was inadequate for this particular project.
His extensions and modifications to C++ were the first steps towards the development of an independent language that would fit the project objectives.
He named the language "Oak" while staring at an oak tree outside his office window!
 The name "Oak" was later dismissed due to a patent search which determined that the name was copyrighted and used for another programming language, so another name had to be chosen“, and was later(in 1995) renamed Java, from a list of random words.
 
                    James Gosling
 
James Gosling is generally credited as the inventor of the Java programming language
He was the first designer of Java and implemented its original compiler and virtual machine
He is also known as the Father of Java
Between 1984 and 2010, Gosling was with Sun Microsystems.
On April 2, 2010, Gosling left Sun Microsystems which had recently been acquired by the Oracle Corporation.
 
 
      Purpose of creating Java Language
 
In June 1991 James Gosling, Mike Sheridan, and Patrick Naughton initiated the Java language project
Through C and C++ were available to work with these languages, the compiler was targeted for a particular CPU.
Complier are expensive and lots of time to create. Hence, it was possible to have compliers for every CPU. An easy and cost efficient solution is needed. This software had to be small, fast, efficient and platform-independent i.e: a code that could run on different CPUs, under different environments. This led to the creation of Java. 
Java language was initially called Oak after an oak tree that stood outside Gosling's office; and was later(in 1995) renamed Java, from a list of random words.
It was not intended to be an internet programming language. However, the advent of the World Wide Web created a demand for software that was portable across all platforms.  Java was then adapted as an Internet Programming Language.
 
                    JDK Versions
 
JDK 1.02 (1995), Java was first publicly released.
JDK 1.1 (1996)
Java 2 SDK v 1.2 (a.k.a JDK 1.2, 1998)
Java 2 SDK v 1.3 (a.k.a JDK 1.3, 2000)
In 2002, JDK 1.4 (codenameMerlin was released, the most widely used version.
In 2004, JDK 5.0 (codenameTiger) was released, the latest version.
                    JDK Editions
 
Java Standard Edition (J2SE)
¡J2SE can be used to develop client-side standalone applications or applets.
J2EE can be used to develop server-side applications such as Java servlets and Java ServerPages.
Java Micro Edition (J2ME).
¡J2ME can be used to develop applications for mobile devices such as cell phones.
                 LECTURE NO: 02
Java Enterprise Edition (J2EE)
Java Program Structure

According to Gosling, "the goal was ... to build a system that would let us do a large, distributed, heterogeneous network of consumer electronic devices all talking to each other." With this goal in mind, the stealth group began work.
The goal of the Stealth Project was to do research in the area of application of computers
Unliked C++ Java is not a superset of C. A Java compiler won't compile C code, and most large C programs need to be changed substantially before they can become Java programs.
Java is built upon C and C++. It derives its syntax from C. its object oriented features are influenced by C++.
It is a language for professional programmers.
Java is a programming language that was introduced by Sun Microsystems in June 1995.

import java.awt.*;

This statement imports the ‘awt package’, which is used to create GUI objects. Here java is the name of the folder, which contains the package ‘awt’. The ‘*’ symbol denotes that all the classes under this package are included.

In Java, all code, including variable and methods should be included with in a class. A single program may have several classes. These classes may extend other classes. A semicolon terminates program statements. The program may also include comments. The complier ignores the comments.

The First Java Program

Let us start our first java program. We shall start with a simple application. The program is given below to display a message:

// This is a simple program called “First.java”

class First
{
    public static void main(String args[])
    {
        System.out.println(“My first java program”);
     }
}

the file name plays a very important role in java. The java compiler inside on a .java extension. In java, the code must reside in a class. Hence, the classname and filename have to be the same. Java is case sensitive. So, the file name should match the class name.
to compile the source code, execute the program using the javac compiler specifying the name of the source file at the command line as shown below:

C:\jdk1.2.1\bin javac First.java

The java compiler creates a First.class file that contains the bytecodes. The code cannot be directly executed. The java interpreter is required to execute this code. The command is given as follows:
C:\jdk1.2.1\bin java First
The following output is the displayed:

My first program in java    

Analysing the First Program

//  This is a simple program called “First.java”

The symbol ‘//’ stands for a commented line. The compiler ignores a comment line. Java also support multi line comments.

The next line declares a class called ‘First’. To create a class, prefix the keyword class, with the classname (which is also the name of file).

class First

The class name generally begins with capital letter.

The keyword ‘class’ declares the definition of the class. ‘First’ is the identifier for the name of the class. The entire class definition is done with in the open and closed curely braces. This marks the beginning and end of the class definition block.

public static void main(String args[])

This is the main method, from where program will begin its execution. All java application should have one main method. Let understand what each word in this statement means.

The ‘public’ keyword is an access specifier/modifier. It indicates that the class member can be accessed from anywhere in the program. In this case, the main method is declared as public, so that JVM can access this method.

The ‘static’ keyword allows the main to be called, without needing to create an instance of the class. But, in this case, a copy of main method is available in memory, even if no instance of that class has been created. This is important, because the JVM first invokes the main method to execute the program. Hence, this method must be static.

The ‘void’ keyword tells the compiler that the method does not return any value when executed.

The ‘main()’ method performs a particular task, and it is the point from which all java applications start.

‘String args[]’ method is the parameter passed to the main method. The variables mentioned within the parenthesis of a method receive any information that is to be passed to the method. These variables are the parameters of that method.

‘args[]’ is an array of type string. The arguments passed at the command line are stored in this array. The code within the opening and closing braces of the main method is called ‘method block’.

System.out.println(“My first java program”);

This line displays the string, ‘My first java program’ on the screen. It is followed by the new line. The ‘println()’ method produces the output. This method displays the string that is passed to it, with the help of ‘System.out’. Here, ‘System’ is a predefined class that provides access to the system, and ‘out’ is the output stream connected to theconsole.

Passing Command Line Arguments

The following shows how to receive command line arguments in the main method:

Class Pass
{
    public static void main(String parameters[] )
    {
         System.out.println(“This is what the main menthod received”);
         System.out.println(parameters[0]);
         System.out.println(parameters[1]);
         System.out.println(parameters[2]);
     }
}

We can pass arguments to the main method during executing the program as follow:

C:\jdk1.2.1\bin java Pass We change Lives
This is what the main method received
We
Change
Lives