Comparing Computer Languages - Java

This page currently contains Java specific notes. It will be reorganized into an index later.


The primary purpose of java is to keep unix systems from becoming obsolete. It keeps many of the frustrating features of C++ (such as being case sensitive) and drops many features which make C++ a good language (such as pointers and operator overloading).

Java source files must end in .java and the compiled files must end in .class. Therefore, the new universal language can not be used with MS DOS which allows only 3-letter filename extensions.

Java is case sensitive. Even the case of the letters in the filename must match the name of the primary class (i.e. this is a pain in the ...). Uh, running jdk 1.1 in a DOS window under Windows 98, the case of the filename and the class name do not have to match. I guess that some standards aren't.

C & C++ programs are normally compiled into an executable that runs without any additional support. (Microsoft Visual C++ is an exception - it requires a run-time dll, and perhaps a foundation class dll. For more info, see DLL Hell.)

Java improves (not) this by pseudo compiling into a p-code which is interpreted by the run-time environment. Since there are several versions of the run-time, you must provide a way for your client to download the correct version. However, there are several bug fixes to each version. Therefore, if you cause the user's system to get the latest version, you could be responsible for breaking other programs that used to run perfectly.

Unfortunately, there is no way to be sure which versions of these dll's are available on a given machine. As a result, you should expect your programs to fail on any machine except the one they are developed on.

To make matters worse (monopolies sometimes do this), Microsoft provides java run-times which are different than the Sun standard. (This significantly improves the odds that your java code will not run on your client's machine.)

There's more - with unix systems, software is normally distributed as C source files, perl scripts, shell scripts, and other user readable code. Then, it is up to the user (system administrator) to compile the code and install the executables. Well, many companies don't like to let users see their source code. Java to the rescue! Because java converts human readable source code into p-code, this gives companies a way to distribute machine independent source which users can not read. Then, instead of running fast compiled code, each system has an interpreter that slowly "executes" the p-code.



.java Files

.java files are entered in straight ASCII. These are compiled into .class files. There are 2 different interpreters - one for applications and another for applets.

A basic java application requires

class SameAsFilename
{
  public static void main (string[] arguments)
  {
  }
}

When you execute a program, the java run-time starts execution by calling the main subroutine inside the class which has the exact same name as the .class file. Even the case of the letters must match. Arguments can be added to the command-line.

Java programs run via the Internet (i.e. in a browser) are called applets. Being a standard language, the basic applet structure is completely different than the application structure.

import java.awt.*;

public class SameAsFilename extends java.applet.Applet
{

  public void init()
  {
   String param;
   param = getParameter("ParamName");
  }

  public void paint(Graphics g) // could also use - (java.awt.Graphics g)
  {
   // This is where you display some output
   g.drawString("Some stupid string", 5, 20);
  }

}
In this case, the java applet run-time starts execution by calling the init subroutine. Applet parameters are made available using an entirely different syntax from application command-line parameters.

Applets are called via html using code similar to

<applet width=200 heigth=200 code=test> Java is not enabled. </applet> Notice that all the parameters shown are required.


Setting Font Colors

public void paint( Graphics g ){
  g.setColor(Color.red );
  g.drawString("Some String", xValue, yValue);  
}


Misc

Based on a code review (java.swing.JTextField.java), it does not matter where class properties (variables) are defined. In most other languges (especially C/C++), all variables must be defined before they are used. In java, the variables can be defined at the bottom of the calss definition.

Java does not use C/C++ type header files.


Run-Time Design Errors

With Java 1.3, the following function "compiles" ok, but causes a runtime error. However, this code works fine.


Misc

The jdk 1.1 java disassembler requires the name of the class file but without the file extenstion. (It took almost 2 hours to figure out this un-documented feature.)

Character-based output

  System.out.print("Some String" + AnyVariable);   // Applications only
  System.out.println("Some String" + AnyVariable); // Applications only


References


Author: Robert Clemenzi - clemenzi@cpcug.org
URL: http:// cpcug.org / user / clemenzi / technical / Languages / Java / index.html