The Java Story
Back in 1990, James Gosling headed a team of people at Sun Microsystems in developing a program to control consumer electronics. The software was initially designed using C++ which was the "next big thing" in computer languages. Gosling however, found that C++ was not suitable for the projects they had in mind after encountering troubles with the complicated aspects of multiple inheritance of classes and program bugs such as memory leaks. He then decided to come up with his own simplified computer language that would avoid all the problems he had with C++.
Gosling adopted the object-oriented features of C++ and stripped all the features that made C++ difficult to use with consumer-electronics project. After completing his language-design project, he had a new programming language which he named Oak.(The story goes that the name Oak came to Gosling as he gazed out of his office window at an Oak tree).
Oak was first used in something called the Green project wherein the development team attempted to design a control system for the use in the home.
Language Features
Object-Oriented. Just like C++, Java uses classes to organize code into logical modules. At runtime, a program creates objects from the classes. Java classes can inherit from other classes, but multiple inheritance, wherein a class inherits methods and field from more than one class, is not allowed.
Portable and Platform-Independent. Java is platform-neutral, which means that programs developed with Java can run on any computer system with no changes. This is attained by using a special file format (byte code) which is read and executed by any computer system that has a Java interpreter.
Dynamically Linked. Java codes are linked at runtime. Sections of an application are linked in as they are needed, thereby reducing the need to download all classes before execution can begin.
Multithreaded. Java programs can contain multiple threads of execution, which enables programs to handle several tasks concurrently.
Garbage collected. Java does its own garbage collection, which means that program are not required to delete objects allocated in memory. This relieves programmers of virtually all memory-management problems.
Object-Oriented Programming
Object-Oriented Programming enables you to think of program elements as objects. The following are the three main concepts that are the backbone of OOP.
Encapsulation. Enables you to hide both data fields and the methods that act on the data inside the object. After you do this, you can control access to the data, forcing programs to retrieve or modify data only through the object’s interface.
Inheritance. Enables you to create a class that is similar to a previously defined class, but which still has some of its own properties.
Polymorphism. Used to create new objects that perform the same functions as the base object, but which perform one or more of these functions in a different way.
Applets and Applications
There are two kinds of programs you can write with the Java language: They are:
.
Java Applets. Java applets are small programs that travel with HTML code and execute on the Web user’s computer..
Java Applications. Java applications are complete stand-alone programs that do not require a web browser or HTML to execute.
Applet vs. Application
There are a couple of major differences between an applet and an application, among which are:
. An application contains a constructor to perform all the initial setup. The constructor is called with the new operator to instantiate the class.. An applet uses the init()method for its initialization. |
. An application contains a main()method which makes a call to the constructor to instantiate the class. The main()method can be considered as the control center of the application.. An applet requires an HTML code to be able to run. |
Converting Applets to Applications
General guideline:
.
Change the init() method to a constructor, with all the same contents.
Subclass Frame instead of Applet.
Remember to call show()on the Frame.
Write a main() method that calls the constructor
Converting Applications to Applets
General guidelines:
.
Subclass Applet instead of Application.
Change the constructor to be the applet’s init() method.
Remove the main() method
Example: From applet…
1 import java . applet . * ;
2 import java . awt . * ;
3
4 public class HelloWorld extends Applet {
5 public void init () {
6 add (new Label ("Hello World!") ;
7 }
8 }
… to application
1 import java . applet . * ;
2 import java . awt . * ;
3
4 public class HelloWorld extends Frame{
5
6 public HelloWorld () {
7 setLayout (new FlowLayout () ) ;
8 add (new Label ("Hello World!") ;
9 pack () ;
10 show () ;
11 }
12 public static void main (String args [] ) }
13 new HelloWorld () ;
14 }
15 }
A Frame is a window with a title bar, borders, menus, etc. A typical graphical application will derive from the Frame class, and this serves as the main window for the application. The Frame class is a template for applications, like the Applet class is for applets.
An applet does not have a constructor. On the other hand, a constructor in applications is called by the new operator to create a new instance of the class. Here, components are added and packed (Line9) so that the Frame will big enough to hold all the components. The show() method (Line10) will display the frame.
The main method acts as the "connection" to the Java runtime system. It is called automatically by the runtime system and provide a means for instantiating objects. [Applet vs. Applications, 13 of 14]
Java Packages
The Java basic language is supported by a set of classes that do everything from encapsulate data types to enable programs to connect to the Internet. These classes are organized into a set of packages, as listed below:
. awt . net . io. lang . applet . util |
.
Java. lang. Contains the classes that give Java much of its power. It is automatically used by every Java program..
Java.io. Responsible for the input and output operations of the Java language..
Java.util. Features classes that encapsulate useful data structures, including stacks, vectors, dates, dictionaries, hash tables and bit fields..
Java.net. Features classes that handles online communications..
Java.applet. Responsible for the creation of Java applets..
Java.awt. Provides classes needed to run Java programs in a windowed environment such as Windows95. [Java Packages, 14 of 14]
Data Types Data types represent different sets of data values and determine
which standard operators can be applied. There are two kinds of data types: Simple and Composite. Simple data types are the built-in Java data types. Composite data types are composed by the programmer using simple types, arrays, classes, and interfaces. [Data Types, 1 of 8]
Boolean Data Type
Java’s Boolean data type is one-bit wide and takes on the values true or false. The initial value of a Boolean variable is false. A boolean is not a number and cannot be cast to a number or any type. Nor can numeric operators be used with boolean.
5
6 b = (n ! = 0 ) ;
7 b = (n ! = 0 ? true : false ) ;
8
9 b = n ;
10 b = (boolean ) n ;
11
12 n = b ;
13 n = (init ) b ;
14 . .
15 }
16 }
In line 9, an error occurs because a boolean cannot take an integer value. Lines 10 and 13 will generate an error because a boolean cannot be cast to a number or any other type. Lastly, line 12 will generate an error because a boolean cannot be converted to an integer data type. [Boolean Data Type, 2 of 8]
Char Data Type
Java defines one character built-in type, char. A char is a two-byte Uni-code character. The initial default value for char is \u0000. A char can be treated as an unsigned 16-bit number. However, if any data loss occurs, then casting will be required.
Lines 8 and 9 will result in an error because a boolean type cannot be converted to char and vice versa. Lines 11 and 13 will also generate an error because it requires an explicit cast. [Char Data Type, 3 of 8]
Integer Data Types
Java has four integer types namely: byte (8 bits), short (16 bits), int (32 bits) and long (64 bits). An integer is initially 0. Conversion is done during assignment and requires casting in case of data loss.
Line 9 will result in an error and will require 3 casts (variables b, s and i ). Lines 10, 11 and 12 will generate no error and will have no occurrence of data loss. [Integer Data Types, 4 of 8]
Floating Point Data Types
There are two floating point data types, namely: float and double.
Its initial value is 0.0.
Lines 7, 8, 13 and 14 will result in an error. Lines 10 and 11, on the other hand, will not result in an error. [Floating Point Data Type, 5 of 8]
Reference Data Type
A reference data type is like a simple, built-in data type, that contains a value. It is initially null. It is as close to a pointer as you will get in Java though references are more than pointers (smart pointers). They are smart pointers because they collectively managed created objects.
Line 3 declares s1 and s2 as string only and does not assign a value to it. Lines 6 and 7 "creates" a new string with s1="S1" and s2="S2". In line 8, a value false is assigned to the boolean b. The next line assigns the value of s1 to s2. As a result, s1 and s2 are now equal. [Reference Data Type, 6 of 8]
Arrays
Java arrays are implemented as objects; therefore, arrays are referenced the same as any other object.
Facts about Arrays
.
The first element in an array has index 0..
The array class has a public, read-only variable named length that can be used to get the number of elements in the array.8
9 i = i1 = i2 = j = j1 = j2 = new int [2] ;
10
11 }
12 }
Lines 3 to 7 are all integer array references. Line 9 will not result in an error as all variables are instances of int[]. [Arrays, 7 of 8]
Two-Dimensional Arrays
Two-dimensional arrays are supported as array of arrays.
The above code simply places the value 5 to array TwoDimArray. [Two-Dimensional Arrays, 8 of 8]