Difference between revisions of "The Choice of Java"

From RogueBasin
Jump to navigation Jump to search
 
m (added to category:articles)
Line 43: Line 43:
  }
  }
The syntax of the ''for'' statement should look familar to a C programmer, except that the variable ''y'' can be declared inside the for loop, rather than before it.  For more information on Java programming, there are many excellent tutorials online, but the best is probably the offical one from Sun Microsystems - http://java.sun.com/docs/books/tutorial/
The syntax of the ''for'' statement should look familar to a C programmer, except that the variable ''y'' can be declared inside the for loop, rather than before it.  For more information on Java programming, there are many excellent tutorials online, but the best is probably the offical one from Sun Microsystems - http://java.sun.com/docs/books/tutorial/
[[Category:Articles]]

Revision as of 02:13, 21 January 2009

"The Java Roguelike Development Guide"
Issue One - The Choice Of Java
andrew.d.joiner@googlemail.com




It is fair to say that the vast majority of roguelike games are implemented in the C/C++ programming language. C/C++ are very common langauges that most programmers know how to use, it has a variety of curses libraries to choose from, and a fairly extensive list of functionality.

It is not common, however, to find many roguelike games which have been programmed in the Java programming langauge. The big problem with Java, of cause, is its modern GUI sensibilities. There is no official curses support with Java, and the event model is strictly limited to non-blocking events. However, if these deficiencies can be removed, then Java becomes a viable roguelike programming langauge, and in fact, becomes one of the better prospects for long-term roguelike development. See Java Curses Implementation for more information on solving this Java problem.

So the question is, why Java? One reason is th excellent object-orientated paradigm that Java takes right to its core. OO-Programming is essential for creating larger scale programs, because it is more intuitive and easier to maintain. Things that were are a nightmare to represent in C, such as an efficient Item data structure, are a breeze in Java.

The Java API is also excellent. Many foundation classes are provided, which allow the programmer to get on with implementation, rather than keep reinventing the wheel. Linked Lists, Vectors, Trees, Random Numbers, Strings. While all of these things can be implemented in the C langauges, in Java they are already implemented, and very easy to use.

For example, the following code shows how easy it is to use a Linked List ADT in java;

import java.util.LinkedList;
public class ListExample extends LinkedList {

    // constructor()
    public ListExample() {
        super();
        // create a new Object reference
        Object o = new Object();
        // add to the list
        this.addFirst(o);
        // remove from list
        this.removeFirst(o);
    }

}

This is just a crude example of how easy Linked Lists are to use in Java. This ability is all based on the fact that every Java class is a sub-class of Object. Object is the root Java class, and because Java has very strong OO features, inheritance of Object is automatic for all classes.

Java is a recommended programming langauge to learn, especially for roguelike programming, since it is so straight-forward to use. The algorithmic syntax of Java is almost identical to C, which makes it very easy to learn Java if you already understand C. The basic Java algorithmic constructs are the same as C;

  • if, else if
  • for
  • do, do while, while, break
  • switch, case, break

For example, a for statement in Java

String string = "0123456789";
for (int y = 0; y < 10; y++) {
    System.out.print(string.charAt(y));
}

The syntax of the for statement should look familar to a C programmer, except that the variable y can be declared inside the for loop, rather than before it. For more information on Java programming, there are many excellent tutorials online, but the best is probably the offical one from Sun Microsystems - http://java.sun.com/docs/books/tutorial/