Running on Java 22-ea+27-2262 (Preview)
Home of The JavaSpecialists' Newsletter

067BASIC Java

Author: Dr. Heinz M. KabutzDate: 2003-03-31Java Version: 1.4Category: Language

Abstract: A typical first computer language used to be BASIC. It had a bad reputation for producing spaghetti code, due to the GOTO statement. We show how switch-case and exceptions in Java can look like BASIC.


Welcome to the 67th edition of The Java(tm) Specialists' Newsletter, sent to 6299 Java Specialists in 94 countries.

In the western world, we celebrate the 1st of April with jokes, trying to catch out unsuspecting and gullible people. Last year, I sent out a newsletter in which I announced that from that day forth, you would have to pay to become unsubscribed, and that the unsubscription fee would automatically be deducted off your credit card. Naturally, quite a few subscribers sent me emails such as: "haha, look at the date". Others sent me outraged emails telling me that they had not agreed to this and that I could not do it. My wife Helene had warned me against putting in the joke, but being a stubborn South African German, I naturally did not listen. This year, I was going to repeat my folly, but having thought about it for a while, have decided that with a hundred countries on my list, an April fool's newsletter would only serve to confuse a significant number of readers.

HOWEVER, in this newsletter I have some lighthearted material on how you can write BASIC code in Java. In the middle of March I was in Germany visiting my grandmother for her 90th birthday, and while I was there, managed to present a Design Patterns Course to a company in Frankfurt. Our friend Carl Smotricz, who used to host our newsletter on his website, was one of the 13 course participants. While Carl was taking me to the train station after the second day of lectures, Carl told me that it was possible to program GOTO in Java. Naturally I was curious, so I asked Carl to give me an example. The theme sounded like an ideal opportunity for an April 1st joke, so I will send this newsletter to you with the warning that Carl and I were basically having fun and seeing how far we could push the Java Programming Language to look like BASIC. Please visit our new self-study course catalog to see how you can upskill your Java knowledge.


Carl and I have something in common. Both of us started programming in BASIC. BASIC stands for Beginners All-purpose Symbolic Instruction Code and is the easiest language in the world to understand. It is not surprising that Visual BASIC is so popular. We even have plans to produce a Design Patterns Course for Visual Basic .NET (the Delphi version is already underway - and that is not an April Fools joke ;-)

BASIC has the following advantages over Java:

  1. BASIC is for beginners. Most programmers in the world today have less than 10 years experience and therefore should be considered as beginners. Java is definitely not for beginners. It is for expert programmers. It gives you more ways of shooting yourself in the foot than an AK47. (I do not consider VB.NET to be BASIC, but rather just another flavour of Java.)
  2. BASIC allows global variables. Global variables reduce the number of instances of variables needed, they reduce the namespace. Global variables make it easy to write a program, although maintenance becomes almost impossible. Do not you believe me that global variables make coding easier? Think of a Design Pattern. Think of any Design Pattern. Any one. Name it out loud, and then please go to the end of this email and look at the P.S.
  3. BASIC supports the GOTO statement. GOTO is perhaps not the darling of Edsger W. Dijkstra but it does allow us to write interesting succinct code, which certainly ensures that we will be employed in a maintenance role for many years to come.
  4. BASIC is widely understood and supported. Say "goodbye" to expensive finder fees from recruitment agencies. Hire one of thousands of BASIC programmers and pay them very little. Or, since BASIC is for beginners, take someone straight out of arts to do the programming for you.

Consider the following BASIC program. Don't just skip over it, try to understand what is happening. If you read it carefully, you will see that it constructs a list of 10 numbers, then sorts them using the Bubble Sort algorithm. (Thanks Carl for this piece of code.)

20 I = 0
30 N(I) = INT(1000 * RND)
40 I = I + 1: IF I < 10 GOTO 30
50 S% = 1 : I = 0
60 IF N(I) < N(I + 1) GOTO 80
70 T = N(I) : N(I) = N(I + 1) : N(I + 1) = T : S% = 0
80 I = I + 1 : IF I < 9 GOTO 60
90 IF S% = 0 GOTO 50
100 I = 0
110 PRINT N(I)
120 I = I + 1 : IF I < 10 GOTO 110
130 STOP

When we run this with Microsoft QuickBasic 3.0, we see something like:


If we wanted to convert this to Java, we could either hire an expensive Java programmer (who probably would not understand the above code), or we could write a tool to automatically translate the above code to "Java". Remember the GIGO principle - Garbage in - Garbage out.

Is it possible to translate this to Java? Yessir!

public class Bubble extends Basic {
  // Original Code:
  // 20 I = 0
  // 30 N(I) = INT(1000 * RND)
  // 40 I = I + 1: IF I < 10 GOTO 30
  // 50 S% = 1 : I = 0
  // 60 IF N(I) < N(I + 1) GOTO 80
  // 70 T = N(I) : N(I) = N(I + 1) : N(I + 1) = T : S% = 0
  // 80 I = I + 1 : IF I < 9 GOTO 60
  // 90 IF S% = 0 GOTO 50
  // 100 I = 0
  // 110 PRINT N(I)
  // 120 I = I + 1 : IF I < 10 GOTO 110
  public static void main(String[] args) {
    int I=0;int S=0;int N[] = null;
    while(jump != -1) {
      try {
        switch(jump) {
          case 10: N = DIM(10);
          case 20: I = 0;
          case 30: N[I] = INT(1000 * RND());
          case 40: I = I + 1; if (I < 10) GOTO (30);
          case 50: S = 1; I = 0;
          case 60: if( N[I] < N[I + 1]) GOTO (80);
          case 70: int T = N[I] ; N[I] = N[I + 1] ; N[I + 1] = T ; S = 0;
          case 80: I = I + 1 ; if (I < 9) GOTO (60);
          case 90: if (S == 0) GOTO (50);
          case 100: I = 0;
          case 110: PRINT (N[I]);
          case 120: I = I + 1; if (I < 10) GOTO (110);  
          case 130: STOP();        
        // if there was no GOTO then we want to end the program
      } catch(GotoException ex) {
        // GOTO was called, and a GotoException has caused the
        // control to pass outside of the switch statement

The output would be something along the lines of:


What does the code for look like?

public class Basic {
  // GotoException is thrown by GOTO to avoid having to call 
  // "break" after the call to GOTO in the switch statement.
  // I warned you before that Exceptions were dangerous ;-)
  protected static final class GotoException
      extends RuntimeException {
    private GotoException() {}
  private static final GotoException gotoEx = 
    new GotoException();
  public static int jump=10;
  public static void GOTO(int line) {
    jump = line;
    throw gotoEx;
  // STOP changes the current line to be -1, which ends the
  // program
  public static void STOP() {
    GOTO (-1);
  public static void PRINT(String s) {
  public static void PRINT(int i) {
  public static int[] DIM(int n) {
    return new int[n];
  public static double RND() {
    return Math.random();
  public static int INT(double d) {
    return (int)d;

What makes me scared is that the code runs and actually works. Perhaps one day Carl and I will write a tool to automatically convert legacy BASIC code to Java ......

Kind regards, and don't get caught out tomorrow!


P.S. You probably thought of the Singleton? The Singleton's intent is to provide a global access point to an object. Is that not a global variable?



We are always happy to receive comments from our readers. Feel free to send me a comment via email or discuss the newsletter in our JavaSpecialists Slack Channel (Get an invite here)

When you load these comments, you'll be connected to Disqus. Privacy Statement.

Related Articles

Browse the Newsletter Archive

About the Author

Heinz Kabutz Java Conference Speaker

Java Champion, author of the Javaspecialists Newsletter, conference speaking regular... About Heinz

Superpack '23

Superpack '23 Our entire Java Specialists Training in one huge bundle more...

Free Java Book

Dynamic Proxies in Java Book
Java Training

We deliver relevant courses, by top Java developers to produce more resourceful and efficient programmers within their organisations.

Java Consulting

We can help make your Java application run faster and trouble-shoot concurrency and performance bugs...