Putting all the hype aside, Java is simply a programming language. It has many strengths and among these are: it is simple, it is advanced, and it is supported by a large infrastructure. I begin our discussion with an overview of some of the buzzwords that are commonly associated with Java, with a liberal dose of my own opinions.
Object Oriented Programming
There is no such thing as an object-oriented programming language! Object-oriented is a programming style or methodology, not an attribute of computer languages. I have known many people (and C++ compilers) who write object-oriented C-code. Likewise, it is trivial to write Java code in a procedural style. There are other styles also, such as functional programming. I'm not going to take a stance on what is the best methodology. But, the prevailing wisdom is that object-oriented programs are the easiest to understand, write, and maintain. While being "object oriented" is not a language feature, languages do vary in the extent with which they support an object-oriented programming approach. Thus, a proper statement would be that Java's design supports an object-oriented programming (OOP) model.
To say that Java is original, or even unique, in providing a particular level of support for OOP is tantamount to suggesting that Microsoft invented the graphical user interface. Similar OOP environments were provided by Simula, Smalltalk, and Turing (just to mention a few you might have heard of), all long before Java sprang from Oak (or should that be Spring). In my opinion there are four distinguishing attributes, none of which are unique independently, whose combination make Java more than simply notable, but potentially the next predominate programming language. They are:
The object-oriented approach is data-structure centered, rather than algorithm centered. When designing an object-oriented program the user identifies data-structures, commonly called objects, and associates with them various interface routines, called methods, for creating and managing these data structures.
In order to qualify as a proper discussion on OOP these days there is a certain litmus test of buzzwords that need to be discussed. Likewise, the same is also applied to languages in order to see if they measure up to the OOP standard.
Thus, Java passes the test, and we can not dispense with the history/philosophy/religion lesson and talk about the language itself.
A Java source program is composed of white space, comments, declarations, and statements.
White space is a very important part of any Java program. While it has no effect on the meaning of the code, white space, more than any other factor, contributes to the readability and signals the structure of a code fragment. White space includes, spaces, tabs, and new lines. Typically, tabs or indention is used to set off blocks of code, and new lines are used to separate statements. There are no hard-fast rules how and when to use white space, all I suggest is that you adopt a consistent style.
Next to white space, the most important part of any Java program is comments. Keep in mind the syntax of a language is designed for communicating a program's design to a computer. Comments, on the other hand, are the primary mechanism for communicating a program's design to humans. Java has three types of comments.
Inline comments:
answer = 42; // valid independent of the input
/*
... This next bit of code is so obvious that I hesitated to
comment it a first. But, rather than risk any point reduction
in this otherwise flawless program, I have decided to add
this block comment with a recipe for a Gremolata garnish:
2 tablespoons of finely chopped parsley
1 minced clove of garlic
½ teaspoon of grated lemon rind
Sprinkle this mixture on sauce, gravy, or computer
programs during the last 5 minutes of preparation.
*/
question = "To be, or not to be".
Java also provides a special form of comment that is for generating automatic documentation, and web pages, for Java programs using a tool called javadoc. Within these comment blocks several special variables, indicated by a leading @, are recognized.
Documentation comments:
/**
* This interface is sure to make any Java object Cool. It
* defines no methods, but it adds a certain amount of positive
* karma to any class (not to mention that it yields an edible
* combination of mathematical constants).
*
* @see java.hot
* @author Leonard McMillan
* @version 1.0
*/
public abstract interface Cool {
public final static int KARMA = Math.PI + Math.E;
}
All variables in Java must be declared as an object, an array, or a primitive data types.
Java has 8 primitive data types:
boolean, byte, short, int, long, double, float, char)
In Java arrays fall somewhere between a primitive data type and an object
new
operator, and uninitialized arrays have a value of null
length
0
and length - 1
The last data-type in Java is the object.
new
operator. Uninitialized arrays have a value of null
The final component of a Java program is its statements. Individual statements are terminated by semicolons. Groups of statements and declarations enclosed within braces, { and }, are called block statements and they act like an individual statement. Block statements do not require a terminating semicolon. The simplest type of statement is the expression. Java allows the following set of operators within statements:
. [] () ++ -- ! ~ instanceof * / % + - << >> >>> < > <= >= == != & ^ | && || ?: = *= /= %= += -= <<= >>= >>>= &= ^= |=
An expression is a string of variables and constants separated by operators. A common type of expression is the assignment where a variable is given the value of the expression to the left of an equal sign. If one of the operator-assignment forms is used, the variable is given the value of expression to the left of the equals sign combined with it previous value of the variable as determined by the operator to the left of the equals sign. For more specific details on operators and the types of arguments that they allow see the text.
The second type of statement is the control statement.
The following list illustrates some of the control statements
available in Java:
if (BooleanExpression) Statement
if (BooleanExpression) Statement else Statement
while (BooleanExpression) Statement
do Statement while (BooleanExpression)
for (Expression; BooleanExpression; Expression) Statement
break <Label>
continue <Label>
return Expression
Label: Statement
switch (IntegerExpression) {
case IntegerValue:
Statements
default:
Statements
}
In these examples reserved keywords are shown in bold.
This is all of Java that will be covered today. Next class we will discuss
new operator
this, super, final, static,
instance, and class variables)
package and import)
public, private,
protected)
But we know enough now to write a simple example.
This page last modified 8/24/96