Introduction to Java

 

1. Brief history.

Many people believe that the next major area in which microprocessors will have a profound impact will be in intelligent consumer-electronic devices.

Recognizing this, Sun Microsystems funded an internal corporate research project named Green in 1991.

The Project resulted in a C- and C++ -based language that its creator, James Gosling, called

Oak after an oak tree outside the window of his office.

It was later discovered that there already had been a computer programming language called Oak. When a group of Sun people visited a local coffee place, the name Java was suggested and it stuck.

The Green project ran into some troubles.

The marketplace for intelligent consumer-electronic devices was not developing as quickly as Sun had anticipated.

Worse yet, a major contract Sun was competing was awarded to another company.

So, the project was in danger of being canceled.

By sheer good fortune, the World Wide Web exploded in popularity in 1993, and Sun people saw the immediate potential of using Java in creating web pages with so-called dynamic contents.

This breathed new life into the project.

Sun formally announced Java at a major conference in May of 1995.

Ordinarily an event like this would not have generated much attention.

However, Java generated immediate interest in the business community because of phenomenal interest in WWW. Java is now used to create Web pages with dynamic and interactive contents,

to develop large-scale enterprise applications, to enhance the functionality of WWW servers (computers that provide the contents we see in our Web browsers) and

to provide applications for consumer devices (such as cell phones, pagers, and personal digital assistants)

 

2. Compile and Execute

                     $ javac classname.java

                                        where "classname" must be the same as a "public" class defined in the source

                                         including the same capitalization.

                                         Compiler will generate a byte-code file with .class extension that

                                         can be subsequently interpreted

                     $ java classname

                                         where "classname" is the same as in the above command.

 

3.                 Some good characteristics/features of java

                     A.               More object-oriented.

                     1. Many predefined class libraries (API, Applications Programming Interface).

                                          Core packages

                                          Extension packages: Newer additions

                                          Website: http://download.oracle.com/javase/7/docs/api/

                                          Some examples:

                                                             java.applet: This package contains the Applet class

                                                             and several interfaces that enables creation of applets,

                                                             interaction of applets with the web browser and playing

                                                             audio clips.

 

                                                             In java2, "java.swing.JApplet" is used to define an applet

                                                             that uses the Swing GIU Components.

 

                                                             java.awt: The java Abstract Windowing Toolkit Package

                                                             This package contains the classes and interfaces required

                                                             to create and manipulate GUI in java1.0, and 1.1

                                                             In java2, these classes can still be used, but the Swing

                                                             GUI Components of javax.swing packages are often used instead.

 

                                                             java.io: The java input/output package

                                                             This package contains classes and interfaces for

                                                             inputs and outputs.

 

                                                             java.lang: The Java Language Package

                                                             This package contains classes and interfaces required

                                                             by many programs and is automatically imported by the

                                                             compiler into all programs.

 

                                                             java.text: The Java Text Package

                                                             This package contains classes and interfaces that enable

                                                             a Java program to manipulate numbers, dates, characters and

                                                             strings. It provides many of Java's internationalization

                                                             capabilities (e.g, an applet may display a string in a different

                                                             language)

 

                                                             java.util: The Java Utility Package

                                                             This package contains utility classes and interfaces

                                                             such as date and time manipulation, and random-number

                                                             processing capability, storing and processing large amount

                                                             of data, and breaking strings into smaller pieces called tokens.

 

                                                             javax.swing: The Java Swing GUI Component Package

                                                             classes and interfaces for Java's Swing GUI Components

                                                             that provide support for portable GUI's.

                                         2. Every method (function) belongs to a class (i.e., a member

                                          of a class) and no class-independent method.

                                          Even the main method is a class member.

                                         3. The Base Class (Object) is defined.

                                          Any user-defined class extends the base class unless

                                          otherwise specified, thereby inheriting all public and

                                          protected members from this base class.

                                         4. Access (Visibility) modifier: public, private, protected or DEFAULT

                                                             public members: accessible from anywhere in the program

                                                             private members: accessible only from the class scope,

                                                             i.e., only from members of the same class.

                                                             For classes, only inner classes can have this modifier

                                                             protected members: accessible from the class itself,

                                                             from any subclasses, and from any class defined in the

                                                             same package.

                                                             Default (No modifier): Package access, Default access or Friendly access as

                                                             Access is for any class within the same package.

                                         5. A method may be for the entire class (static) or for a

                                          class object.

                                         6. Inner classes: A class definition can have class definitions

                                          within itself.

                                         7. More user interfaces (Applets, awt and Swing GUI)

                                         8. Methods can return any type with no restrictions.

                                         (In C++, functions cannot return an array except an array

                                          of characters)

                     B. Safer and simpler syntax

                                         1. No pointer variable (No * operator nor & operator)

                                         2. Data types

                                          primitive: boolean, char, byte, short, int, long(64 bits), float, double(64 bits)

                                          reference: array, String

                                         3. Automatic initialization by compiler

                                          All instance variables of a class.

                                         4. Syntax-error checking of noninitialization of all

                                          'automatic' variables (local variables of a method

                                          and parameters of a method) before their actual uses.

                                         5. Automatic type conversions.

                                         6. A class name starts with a Capital letter

                                          while every keyword is all lowercase letters.

                                         7. Restriction of "static" methods of a class to directly

                                          calling only other "static" methods of the same class.

                                         8. A program file must contain at most one "public" class

                                          definition and the file name must be the same as this public

                                          class name with the .java extension including capitalization.

                                         9. No multiple inheritance, i.e., every class has exactly

                                          one immediate superclass except the base class, Object.

                                         10. No compiler directive like #include (C++ or C)

 

                    4. Some bad features

                     A. No automatic conversions for i/o operations.

                     B. Difficult/confusing to identify the exact package to be imported.

                    /* Another java example program that

                     1. simply defines a public class "FirstProg"

                     to contain the "public static void main(String args[])"

                     2. and read an input integer and print all integers

                     between the input and 1 before printing a welcome message.

                    */

                                         import javax.swing.JOptionPane;                  // for an input dialog box

                                         public class AnotherProg

                                         {

                                          public static void main (String args[])

                                          { String inputt;            //for the input string

                                          inputt = JOptionPane.showInputDialog (

                                                                                 "Enter an Integer < 50");

                                          int num = Integer.parseInt(inputt);

                                                                                 // convert to an int type

                                          for (int k=num; k>=1; k--)

                                                             { System.out.print ("THE VALUE OF k IS:: ");

                                                              System.out.println(k);

                                                             }                   // end of loop body

                                          System.out.println ("Welcome To the First Java Program!");

                                          System.exit(0);            // required for any GUI applications.

                                         }                   // End of method main

                                         }                   // End of class AnotherProg