Week 9 - March 16, 1999

What is Java?
What is a Java Applet?
How do we compile a Java program/applet?
Java variables
Java statements

What is Java?

Java is a simple, object-oriented,distributed, interpreted, robust, secure, architecture neutral, portable, high-performance, multithreaded, dynamic language.

Simple - does not have a number of features available in C/C++, especially the one that lead to poor programming (goto)
Object-oriented - you focus on data and methods, not procedures
Distributed - designed to support network-based applications
Interpreted - Java compiler generates byte-code rather than machine specific code
Robust - no pointers means no problems
Secure - no malicious code
Architecture neutral - compiler generates byte-code; one uses a JVM to run a Java program
Portable - all data types defined in JVM (they are not machine specific)
High-performance - easy to write GUI and network support, where speed (data processing) is not the main factor (Java is on the average 20 times slower than C)
Multithreaded - allows multiple things going on at the same time
Dynamic - classes are loaded as needed

What is a Java Applet?

A Java applet is a program that runs inside a web browser or an applet viewer, rather than a standalone application. In order to see it, you have to define an HTML page that will contain the specific applet tags.
    <html>
    <head><title>My First Applet</title></head>
    <body bgcolor=#ffffff>
    <h1>My First Applet</h1>
    <applet code="FirstApplet.class" width=500 height=300>
    </applet>
    </body>
    </html>
The width and height tags will let the web browser or appletviewer know how much space (in pixels) the applet will take inside the page. The Java code (FirstApplet.class in this case) has to be in the same directory with the HTML file. To see the applet, we can either point out browser to the URL of the HTML file, or use the appletviewer program.

Note. Web browsers store the applet code in the cache directory. If we are doing any change to the code and want to see the new applet, we need to close and reopen the web browser. Therefore, for debugging, it is better to use the appletviewer.

How do we compile a java program/applet?

To compile a java program, we use the javac compiler. In Java every compiled class is stored in a separate file. The name of the file is the same with the name of the class and should have the .java extension. If more than one class is declared in the same .java file, only one class may be declared public (for outside the class access), and that class must have the same name as the source file. Comments inside a Java program are C/C++ like: you either start them with // and continues untill the end of the line or you start them with /* and continues until the next */.
Java does not have a preprocessor, thus there is no use for #define, #include, and #ifdef. Java has a import statement, which is similar to the C #include directive. This statement  tells the compiler what classes this program is using.

For example, if my class has the name FirstApplet, the source should be in FirstApplet.java and the compiler command

    javac FirstApplet.java
will generate the code file named FirstApplet.class. After we compile the program, only the .class files are needed to run the program, thus we can store the source code in a different directory (if we want).

Java variables

Global variables

Java has no global variables. In Java every variable and method is declared within a class and forms part of that class. To access it we have to use the fully qualified name, which consists of the class name and field name (i.e. variable or method name).

Local variables

The variables declared inside methods definitions behave just like C variables. They can not be accessed from outside the method or class.

Constants

Any variable declared final in Java is a constant. The Java equivalent of the C #define is  static final. The convention for constants name from C is also a Java convention: constants names are written in CAPITAL letters.
Since to use a constant we have to call it by is qualified name (if it is outside our class), there is no risk of a name collision.

Primitive data types

boolean variables can take to values true or false.
char variables do not have a sign; in Java the char variables hold a two-byte Unicode character, to support Internationsl characters. As long as you are using regular ASCII character, you will find no difference between this type and the C char.
byte, short, int, long varibles can store signed integers (8,16,32,64 bits). Note that you are not going to write long int or short int as in C.
float, double variables can sore floating-point numbers on 32 or 64 bits. Note that there is no error for division by zero. The Float and Double classes define some of the extreme values a float or double variable can take as constants: POSITIVE_INFINITY, NEGATIVE_INFINITY, NEGATIVE_ZERO, NaN (not a number). Divizion by zero leads to positive infinity while division by negative zero leads to negative infinity.

Reference Data Types and Arrays

The non-primitive data types in Java are objects and arrays. These are called "reference types" because they are handled "by reference", while primitive types are handled "by value". Therefore, if we want to use = or == for reference types, it will not work the same way we are expecting them to do. For example, if we have to variables, a and b, of the same reference type, and we use the statement
a=b;
they will point to the same object instance, that is changes done on one of the variables will reflect on the other one. If instead of making them pointing to the same object we want to copy the b object to the a object, we have to use the following statement
a=b.clone();
This will make a to point to an object that is a duplicate of b.

To compare to object for equality, we can not use the == operator: it tests only if the two variables  are references to the same object. Instead, we have to write our own method of comparing two varibles, just as you might use strcmp() to compare C strings. In Java, some classes define an equal() method that you can use.

Arrays

Arrays are also reference types. There are two ways to create an array in Java.The first one uses new and specifies how large the array should be:
int first_array[] = new int[100];
The other way to create an array is with a static initializer, which looks just like it does in C:
int table[] = {1,234,234,23,32,2,6};
For multidimensional array, we have to specify only the first dimmension. For example
String mystring[][] = new String[10][];
int another_array[][][]= new int[3][4][8];
int i;
for (i=0;i<10;i++) {
   mystring[i]=new String[i+1];
}
To access the element of an array simply use its address inside the array mystring[1][5], another_array[1][5], another_array[3][2][4].
Arrays are similar to objects and assigning an array simply copies a reference to the array.

Strings

Strings in Java are not null-terminated as they are in C. They are instances of the java.lang.String class. Java strings are treated more like primitive types than like objects. There is no way in which you can modify the content of a string. We have to use a StringBuffer object to be able to modify the content of a string.
Each time the compiler finds a double quoted text, it automatically generates a string object for it.

null

The default value for variable and all reference types is null. null is a reserved value that indicates "an absence of reference", i.e. that a variable does not refer to any object or array. In Java, null is a reserved word, not just a defined constant equal to 0 as in C.

Java statements

Many of the Java's control statements are similar or identical to C statements. We will try to look at some of the main control/loop statements in Java

if/else, while and do/while

These statements are exactely like the ones in C, the only difference arises because the Java boolean type can not cast to other types, thus we can not use zero and non-zero values to substitute for false or true.
int i,f,j;
...
while(i++<10) {
  if (i==5) {
    ...
  }
  else {
    do {
      ...
    } while (f!=0);
}

switch

Switch statement is identical with the one in C. It can use byte, char, short, int or long types as the values of the case labels, and you may also specify a default label, as you do in C.
int i;
...
switch(i) {
  case 10:
    j=5;
    break;
  case 15:
  case 5:
    j=12;
    k++;
    break;
  default:
    k++;
    j=0;
    break;
}

for

The for loop is also identical with the one in C. Moreover, it has the C++ ability to declare local loop variables in the initialization section of the loop.
for (int i=0;i<100;i++) {
...
}
Variables declared this way have their scope the for loop, that is they can not be accessed outside the for loop. Variables with the same name outside the loop will not be changed. For example
int i=25;
for (int i=0;i<5;i++) {
  ...
}
will have 25 stored in the i variable at the end of the loop.