Introduction Java Syntax
Object-Oriented Programming

Java is an object-oriented programming language developed by Sun Microsystems and released in 1995. Modelled after C++, the Java language was designed to be small, simple, and portable across platforms and operating systems, at both the source and binary level. One of the resons for Java's popularity is its ability to bring executable content to the web. A Java-enabled browser has the ability to recognize a special <APPLET> hypertext tag. When a Web page containing an applet is downloaded, the Java-enabled browser also downloads another file of information written in bytecodes. The browser then interprets the bytecodes and run's an executable program on the user's host. Thus Java applets and programs can be distributes across networks and run on many types of computers.

Classes
The importance of object-oriented programming is that it enables programmers to organize their programs in ways that resemble the organization of objects in the real world, which makes programming easier and more logical. Using object- oriented programming, the overall programs are made up of many different components (called "objects"), each of which has a specific role in a program and can talk to the other components in predefined ways.

The fundamental unit of programing in Java is the class. A class is a collection of data and methods that operate on that data. The data and methods, taken together, serve as a blueprint for several objects with similar characteristics. The following example is taken from Java In A Nutshell by David Flanagan.

Suppose we wanted to define a class that describes the properties of circles. Every circle can be described by the (x,y) coordinate of its center and also by its radius. Each circle may have a unique center and radius, but as a class, circles have certain intrinsic properties that can be captured in a definition. The following is a very simple definition of the class of circles in Java.


public class Circle {
	public double x,y;    //coordinates of the center
	public double r;	  //radius

//Methods that return the circumference and area of a circle
public double circumference() {return 2*3.14159*r;}
public double area() {return 3.14159*r*r}
}		

Objects
An object is simply an instance of a class. Above we defined the class Circle, and want to use it. But we can't work with the general class of circles themselves, we need a particluar circle to work with. We need an instance of the class, a single circular object:


Circle c = new Circle();
//initilize our circle to have center (2,2) and radius = 1.0
c.x = 2.0;
c.y = 2.0;
c.r = 1.0;
//invoke the area method
a = c.area();

The first line creates an instance of the Circle class a circle object. The next three lines initialize variables. The last line invokes the area method. The area method has no arguments because it is implicit in the language that a method operates on an instance of the class in which it is defined.

The beauty of object-oriented programming, particularly Java, lies in its organization of classes and objects. Every time I want to calculate the area and/or of a circle, I don't have to rewrite the same code. I can simply create an instance of the Circle class (a circle object) to make calculations pertaining to a particular circle. Java programmers can use a class over and over again to create many objects.

Statements

A statement performs a single Java operation. Some examples are:

int L =20;
import java.applet.Applet;
nFigures = 0;

The most important thing to remember about statements is that they must end in a semicolon.

Variables


There are 3 kinds of variables in Java: instance variables, class variables, and local variables.

Variable Names
Variable names in Java can start with a letter, an underscore (_), or a dollar sign ($). Also, note that Java is case-sensitive, so "Hello" is different from "hello". The naming convention in Java is to use meaningful variable names often consisting of several words, the first word lowercase but the first letter of each following word is capitalized. An example:


int thisIsAReallyLongVariableName; 

Declaring Variables
Variables are usually declared at the beginning of a method definition. Variable declarations must include a type and name, such as:


int x1;
String lastName;

Variable Types
The variable type defines what values that variable can hold, and can be one of the following:

There are 8 basic primitive data types which handle integers, floating point numbers, characters, and boolean values (true or false). They are called primitive because they're built into the language and are not actual objects.

Comments

There are three type of comments:



Send comments and suggestions to jdecarolis@clarku.edu
Last updated 10 October 1997