Introduction to Java Programming Styles

Java uses two styles of programming

Java application programs run is a standalone environment with the support of a virtual machine (JVM), where as Java applets run in a browser and are subject to stringent security restrictions in terms of file and network access, where as an Java application can have free reign over these resources.

Simple Java application example

// HelloWorld.java
// A very simple Java Application

public class HelloWorld {
   public static void main( String args[] )
   {
      System.out.println( "Hello World! \n");
   }
}

## To execute the code you need to compile it, which produces a file HelloWorld.class
javac HelloWorld.java

## You can now execute the program, remember to remove the .class extension when running
java HelloWorld

Simple Java Applet example

## Web Page code HelloWorld.html

<html>
  <head></head>
  <body>
      <applet code="HelloWorld.class" width=800 height=600></applet>
  </body>
</html>

## The Java Applet Code HelloWorld.java, remember you need to compile before accessing the ## web page

// HelloWorld.java
// A very simple Java Applet

import java.awt.*;                     // import class Graphics
import java.applet.*;                  // import class JApplet

public class HelloWorld extends Applet {
   public void paint( Graphics g )
   {
      g.drawString( "Hello World!", 25, 25 );
   }
}

## To execute the Java applet, point your browser to the html file

Java Application Programming

Using the example above you create the source code then compile it using the javac compiler which creates a 'HelloWorld.class' file. Once you have the compiled code you can execute it with the java binary (remember that when executing the compiled code you do not specify the '.class' extension and make sure that the class file in the your CLASSPATH or you are in the same directory as the file)

In Java it is wise to add lots of comments to remind you what the program does, it helps you especially when you need to change code at a later date

Single line comment // This is a single line comment
Multi-line comment /*
This is a multi-line comment
and will be ignored by the compiler
*/

The program name must be the same as the public class name in the code otherwise the compiler will produce an error, you can only have one public class in a Java program otherwise it will error when you compile it.

Compiler error if class name is not same as filename helloworld.java:10 class HelloWorld is public, should be declared in a file named HelloWorld.java
public class HelloWorld {
          ^
1 error

Every Java application must have a place to start executing from this place is the main method, if this is missing then Java will not execute the program, an example of main is seen above in the simple Java Application example.

Java Applet Programming

A Java applet either runs from a applet viewer or a web browser. A Java applet is contained in a more secure environment and has the following restrictions depending if it was loaded from the clients computer or via the web (internet), the applet security manager applies these restrictions

Restrictions when loaded from the web (internet) Applets loaded over the web are prevent from reading and writing to files on the clients computer
Applets loaded over the web cannot make network connections except to the originating host
Applets loaded over the web cannot start other programs on the clients computer
Applets loaded over the web cannot load libraries
Applets have very limited access to a clients computer
Restrictions when loaded from the clients computer Applets loaded from the clients computer are allowed to reading and writing to files
Applets loaded from the clients computer can load libraries
Applets loaded from the clients computer can start other programs
Applets loaded from the clients computer can exit the virtual Java machine

The point I am making is that you need to know there are different restrictions depending where the Java code came from and you need to be aware of these restrictions and they can be different when using different versions of Java.

A Java applet must have a place to start executing from and is different from a Java Application, the start sequence of applet is init, start, paint methods. It is not necessary to have the init or start method as Java will load its own default ones but you must have a paint method, you can see an simple example of a Java applet above.