Version 25 of Java

Updated 2003-12-10 11:50:10

Purpose: provide some pointers and references to Java in particular, and its relationship to Tcl.


See http://www.java.com/ for the home of Java, an OO language most commonly implemented as a bytecode interpreter whose byte codes are intended to run cross platform.

Or, whose byte codes run on the Java Virtual Machine, which is ported to a number of platforms.

See Jacl and TclBlend.


Interesting Java article:

http://sakima.ivy.net/~carton/academia/java_languageoftomorrow.html


LV says "So far, in 5+ years of reading and looking at Java, I've only found 1 application that was useful and worked out of the box - and that is a command line app that generates DOC files for the Palm Pilot!". He continues to seek useful applications written in Java that work out of the box.

TP I'll add a few more that work out of the box:

  • Jacl The Tcl interpreter written in Java. The 'java' package that allows introspection and manipulation of other Java objects is absolutely delightful for those of us that work with Java.
  • Jedit A very nice text editor, with modes for dozens of languages, including Tcl. Lots of plugins (mostly Java oriented.)

RS quotes from [L1 ] that Tcl's

 set out [open file.txt a]

is in Java only

 PrintWriter out = new PrintWriter(new FileOutputStream("file.txt",true));

(FW: corrected slightly)


The usual graphical toolkits for Java are AWT and Swing, though each has its deficiencies (notably colossal verbosity relative to Tk...) See for example Click me.

Swank is a Tk-like toolkit as an extension to Jacl.


escargo I think there should be a distinction made between the Java language, the implementations of Java compilers, and the Java Virtual Machine. The Java language is a hybrid object-oriented langauge (hybrid in the sense that there are both primitive types and objects) with single inheritance. (Some aspects of multiple inheritance can be simulated using interfaces.) Source code in the Java language is often translated into Java class files (which contain byte codes) that are interpreted by a Java Virtual Machine (JVM). There are native compilers that translate Java source code into native machine code that does not require the JVM (and isn't cross platform anymore, of course).


davidw I think one of Tcl's advantages over Java is that it scales down, in a number of ways. Let's have a look:

  • Getting started: as Richard indicates, you can do the same thing in Tcl that it takes almost twice as much Java to do. Furthermore, the Java version requires you to understand a little bit about classses.
  • Application size: this depends on the implementation, but most Java programs seem to require much more in the way of system resources. To be fair, a lot of that may be common to any Java application, large or small, so when scaling up it's less noticable, but for an app that is not large, it's quite apparent that Tcl wins hands down.
  • Application structure: Java can be used to force a more elegant architecture in some cases, but falls down for "quick and dirty". You can't do quick web pages (like Rivet, for example) with Java - you need servlets, jsp's, and maybe a bunch of other junk.

If you are working on a large project, all this extra overhead might not make a difference, but if you need to scale down, to something small, fast, easy, light and portable, Tcl is probably a better bet.


Java and Tcl; Tcl and other languages


Java Basics

This posting gives a high level overview of basic Java language concepts and object oriented programming concepts. Feel free to correct or add to this posting what you like. Scott Nichols

Java Programs: There are two types of programs in Java.

  1. Java Application. A Java application runs from your desktop or console and is usually a collection of Java class files (Java bytecodes) zipped into a Java Jar file. Note: Java JSP pages and servlets fall in this category
  2. Java Applet. A Java Applet runs within a web browser and can also be within a Jar file.

Java Bytecodes: When java source files get compiled they are then known as Java bytecodes (*.class files) and are interpreted into machine code by the Java Virtual Machine (Java Interpreter).

Java Naming Conventions: The industry standard for Java naming convention is very simple. All classes start in uppercase and all variables and methods start in lower case, and constants should be in all upper case.

1. Java Classes start in uppercase. Each individual word is capitalized in the class name. 2. All Java methods and variables start in lowercase. Each individual word in the name is captilized. 3. Final variables should be written in all uppercase. This is known as a constant.

Examples:

 public class SysValResponder
 private final int INCREMENT = 5;
 StringBuffer stringBuffer = new StringBuffer();

That's it for Java's naming conventions. This is the naming convention that Sun uses for their Java objects and you can find examples of this in Sun's Java Docs.

Classes: A class is a collection of methods and properties. A class file also can contain inner private classes. A java source file is allowed only one public class per file. A class can either be stand alone or be extended from another class (inheritance) or can implement one or more interfaces (interfaces).

A subclass can access both protected and public member of the superclass.

If a java class has a static main method then this is called first when the object is constructed. A Java application can be started from only one main method. Even though several classes my have main methods within them the application itself is started from only one main.

A java class contains a constructor method and finalize method. A Java class that is abstract does not contain a constructor. The constructor is called first, unless the class is being started from main. The Java constructor name is the same as the class name and is allowed multiple signatures. The finalize method is called when the object is destroyed or garbage collected. If the Java source code does not contain the constructor or finalize methods the Java compiler will use the default constructor and finilize method for you.

Example of multiple constructor signatures:

 public class Circle extends Point {
  // Constructor signature One
  public Circle() {
      radius = 0;
  }
  // Constructore signature Two
  public Circle(double circleRadius) {
      radius = circleRadius;
  }
  protected void finalize() {
      System.out.println("Do any clean up work");
  }
 }

NOTE: A class that is abstract can not be constructed with the new method call.

DKF: A useful technique in more complex classes is constructor chaining, which is where you define one constructor in terms of another (this must always be the first thing in the constructor). This allows you to do simpler variations on an otherwise-complex constructor without repeating the contents of the constructor (which can be a real problem for maintenance.)

Example of multiple constructors showing constructor chaining:

 public class Circle extends Point {
  // Constructor signature One
  public Circle() {
      this(0.0); // chain to other constructor
  }
  // Constructor signature Two
  public Circle(double circleRadius) {
      radius = circleRadius;
  }
  protected void finalize() {
      System.out.println("Do any clean up work");
  }
 }

Inheritance: A class can inherit methods and properties from another class. This is known as inheritance. The class that your subclass is inheriting from is known as the superclass. The class you write is known as the subclass. Java only supports one level of inheritance: meaning that the subclass can only be extended from one superclass. But, Java supports multiple interfaces to get around this.

Example on Abstract inheritance (You will see this alot in Java) :

 // File PizzaStore.java
 public abstract class PizzaStore {
    public abstract void run();
 }

 // File PizzaHut.java
 // In this example, the class PizzaHut is 
 // being extended from PizzaStore and
 // the method run, is overwriting the method
 // run, in the superclass. This is known as
 // Polymorphism.
 public class PizzaHut extends PizzaStore {
   // Implemented method run
   public void run() {
      System.out.println("Pizza Hut");
   }
 }

 // File Nancys.java
 public class Nancys extends PizzaStore {
  // Overridden method run
  public void run() {
      System.out.println("Nancys");
  }
 }

 // File MyStores.java
 public class MyStores {
   public static void main(String[] args) {
      // The one and only Inheritance PizzaStore variable
      PizzaStore myStore;

      // Set myStore to PizzaHut
      myStore = new PizzaHut();
      myStore.run();

      // Set myStore to Nancys
      myStore = new Nancys();
      myStore.run();
   }
 }

Interfaces: A Java Interface is a collection of methods only. A interface does not contain any properties. Interfaces are commonly used as a way of enforcing policy. A interface can not be constructed. It can only be implemented or declared by the implementing class.

Important Note: A subclass can implement multiple interfaces, but can only inherit from one superclass.

Abstract Interface's methods only contain a signature of the method. The body of the method must be implemented in the implementing class.

Abstract Interfaces Example:

 // File ipizzaStore
 public interface IpizzaStore {
   public abstract void run();
 }

 // File IPizzaHut.java
 public class IPizzaHut implements IpizzaStore {
   public void run() {
      System.out.println("Pizza Hut");
   }
 }

 // File INancys.java
 public class INancys implements IpizzaStore {
   public void run() {
      System.out.println("Nancys");
   }
 }

 // File IMystores.java
 public class IMyStores {
   public static void main(String[] args) {
      // The one and only Interface IPizzaStore variable
      IpizzaStore myStore;

      // Set myStore to PizzaHut
      myStore = new IPizzaHut();
      myStore.run();

      // Set myStore to Nancys
      myStore = new INancys();
      myStore.run();
   }
 }

Method Overloading: Method overloading is used in a Java classes when methods have the same name, but have different parameters. This is known as the Java signature.

Example:

 // In this example the SampleMath.java contains two overloaded 
 // square methods that accept both integer 
 // and double parameters
 public class SampleMath {
  public double square(double doubleValue) {
        returnd doubleValue * doubleValue;
  }
  public int square(int intValue) {
        returnd intValue * intValue;
  }  
 }

Method Overriding: Method overriding is when a inherited class over writes a method from the superclass. This is a form of polymorphism. Please see my PizzaHut example above with the run method.

Key word final: A variable that is declared with the keyword final value can not be updated. This is handy for setting up constants. Constants are usually written in all uppercase. The final keyword can also be used with a method to keep a extended subclass from overriding the method.

Example:

 private final int INCREMENT = 5;

Static Variables and Methods: A static class variable or method is shared by all objects of a class. If the static variable is updated by a shared object then all objects of the same type (class) will see the change. Static variables are often used as counters. This can be handy in a JSP page to find out how many users are currently connected to your Servlet.

this and super keywords:

The this keyword followed by a dot is used to access members variables and methods of the current object.

Example:

 public SimpleTime(int hour, int minute, int second) {
    this.hour = hour;
    this.minute = minute;
    this.second = second;
 }

The super keyword followed by a dot is used to access the original version of the method from the subclass. The super keyword can be handy if you want to access the original method in the superclass that has not been overwritten in your subclass

Pass-By-Value and Pass-By-Reference : When calling a method and passing parameters, primative data types, (char,byte,short,int,long,float,double, or boolean) are transferred to the new method by, pass-by-value. And objects, collections, vectors, and arrays are transferred to the new method as pass-by reference.

When the contents of an object is changed when doing pass-by-reference the objects contents in the calling class are changed too.

Java Programming Tips: The heart of Java programming is inheritance and interfaces. Both of these concepts allow for reusability and scalability of Java code. Once you understand both of these concecpts your Java programming will happen much easier. If you understand the concepts the syntax will come on its own.

JSP and Java Servlet Hint: If you will be doing alot of string concatenations use the class StringBuffer instead of String. StringBuffer is much faster at appending strings together.

[ Category Language | Category Java ]