Java Programming

Developed by a group from Sun Microsystems

Developed in 1991 for programming appliances such as toasters, VCRs, Microwave Oven’s

By 1994, it became the hottest tool on WWW

WWW is accessed by a wide variety of computers running a variety of Operating Systems

If you want people to run a program on their site that resides on your site, the program should be able to run on any machine and operating system.

Compilers used to convert higher level language to native code:

The source programs compilable on one OS were not always compilable on other Oss

Even if the compilers are all consistent, we still have to recompile the programs for different CPUs

Cannot do this on the Web, we don’t have time to figure out where client’s C++ compiler is. Copile the program on clien’t machine and then execute it.

We cannot run it directly on client’s machine because we don’t know client’s CPU

Interpreted language will solve our problem

Interpreted languages can be very slow.

Java provides intermediate solution

Has a compiler and interpreter

Java program is compiled and translated to byte code

Java interpreter runs the byte code

A lot more inefficient than the compiled program

There are utilities that can convert byte code to native code.

Java comes under the Object oriented paradigm

Java = C++--++

 

class HelloWorld{

public static void main(String args[])

System.out.println("Hello World");

}

};

 

Small language with a huge and growing library

Anyone who knows C++ can read a Java program and understand it.

Class definitions include the function definition and the implementation at the same time. You cannot separate the definition from the implementation the way we did in C++ using HPPs and CPPs.

A class that can be executed needs a main member function

If this program needs to be an applet, then instead of main() there will be a paint() function.

import java.awt.Graphics;

import java.awt.Font;

import java.awt.Color;

 

public class HelloAgainApplet extends java.applet.Applet {

Font f = new Font("TimesRoman", Font, Bold, 36);

public void paint(Graphics g){

g.setFont(f);

g.setColor(Color.red);

g.drawString("Hello Again!", 5, 50);

}

};

 

extends corresponds to inheritance. HelloAgainApplet is a descendant of java.applet.Applet

The concept of interface provides a facility for data abstraction

The interface provides ADT

You can define a class that implements a given interface

 

interface Fruitlike extends Foodlike {

void decay();

void squish();

. . .

};

 

class Fruit extends Food implements Fruitlike {

private Color myColor;

private int daysTillRot;

....

void decay()

{

.....

}

void squish()

{

....

}

};

 

Class fruit implements fruitlike. That means fruit is going to provide all the functions specified in the interface fruitlike.

You cannot declare variables for a given interface. You can declare variables for a class that implements an interface.

A class that implement an interface can be using a different inheritance hierarchy than the interface.

Java provides a huge library for graphics, internet programming, and threads.

C used to provide built-in internet tools such as sockets but there are many more things that need to be done over the internet such as issuing URLs from your programs.

Concurrent programming are also provided in C using forks, pipes and sockets.

Look at forking.cpp

One final comment Java has taken out some of the features of C++ which had debatable value such as multiple inheritance, and some which are definitely an asset such as templates, operator overloading

Error handling is much more simplified