Difference between revisions of "The Choice of Java"

From RogueBasin
Jump to navigation Jump to search
(Many spelling corrections, one url correction and some minor edits)
(Added information on the foreach loop, etc.)
Line 42: Line 42:
     System.out.print(string.charAt(y));
     System.out.print(string.charAt(y));
  }
  }
The syntax of the ''for'' statement should look familiar to a C programmer, except that the loop-variable can be declared inside the loop instead of outside it. For more information on Java programming, there are many excellent tutorials online:
The syntax of the ''for'' statement should look familiar to a C programmer, except that the loop-variable can be declared inside the loop instead of outside it. In Java-6 there is support for a kind of <i>foreach</i> construct similar to the for-loop but more simpler and readable. The same example given above can be more efficiently implemented using a ''foreach'' loop like this:
String s = "0123456789";
for (char c : s.toCharArray()) {
    System.out.println(c);
}
 
The toCharArray() is a method (like a function in C) which converts the String into a char-array (a char[]). In this ''foreach'' construct the loop traverses over the char-array and assigns the current element to the variable char-c, the println() method of the System.out instance prints the variable to the Standard-Output.
 
For more information on Java programming, there are many excellent tutorials online:
* The best would probably be the official one from Oracle: http://docs.oracle.com/javase/tutorial/
* The best would probably be the official one from Oracle: http://docs.oracle.com/javase/tutorial/
* There's also an introductory tutorial on MIT Open-Course (It uses Think-Java which is available as a Open-Book online): http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-092-introduction-to-programming-in-java-january-iap-2010
* There's also an introductory tutorial on MIT Open-Course (It uses Think-Java which is available as a Open-Book online): http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-092-introduction-to-programming-in-java-january-iap-2010

Revision as of 04:49, 5 May 2015

"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 languages 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 language. 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 language, 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 the 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 languages, 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 language 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 familiar to a C programmer, except that the loop-variable can be declared inside the loop instead of outside it. In Java-6 there is support for a kind of foreach construct similar to the for-loop but more simpler and readable. The same example given above can be more efficiently implemented using a foreach loop like this:

String s = "0123456789";
for (char c : s.toCharArray()) {
    System.out.println(c);
}

The toCharArray() is a method (like a function in C) which converts the String into a char-array (a char[]). In this foreach construct the loop traverses over the char-array and assigns the current element to the variable char-c, the println() method of the System.out instance prints the variable to the Standard-Output.

For more information on Java programming, there are many excellent tutorials online: