Computed GOTOs in Java - HOWTO

This document describes a workaround for the striking lack of a computed GOTO feature in the Java language - a feature every senior developer has been long looking for. Lets all petition SUN to add computed GOTOs to the language core!!! Heck, even Microsoft added GOTO to C# (granted, not computed GOTO but its a first step in the right direction).

But, thanks to the research department here a p-nand-q.com headquarters, you can now enjoy the following fine technology findings, free of charge, in the spirit of keeping the spirit of original developerdom alive. As Steve Balmer said: Developers, Developers, Developers!

Lets start with a simple method that uses that perverted concept of "loops". :

public class computed_goto
{ 
    public int i, j;
    
    int example()
    {
        i = 1;
        j = -3;
        while( i < 10 )
        {
            j += i;
            i++;
        }
        return j;
    }
}

Ugly, butt ugly. The first step is to define an interface. Interfaces are basically pointers to function tables. If you look at them this way, they're great. If you look at them with all the bad propaganda the structured-programming-fetishists with their sick perverted minds keep spitting out, you will abstain from them. But, since we know the truth, and the truth - that interfaces are pointers to function tables - shall set computed GOTOs free !!! yipee.

Next, as an example, we add a local label "loopbody" with some code in it. Ignore for a moment the pretty bad syntax here: this is after all java, what do you expect?

public class computed_goto
{ 
    public int i, j;

    interface method
    {
        int invoke();
    }
    
    int example()
    {
        method loopbody = new method() {
            public boolean invoke() {
                j += i;
                i++;
                return 0;
            } };
    
        i = 1;
        j = -3;

        while( i < 10 )
        {
            loopbody.invoke();
        }
        return j;
    }
}

At this point lets have a moment of silence and all pray for the return of the VAX assembler.


Back to our regularily scheduled programme. If you are a REAL PROGRAMMER(TM), I'm sure you can sense where I'm heading. I'm heading straight for

ip = 0;
while( !done )
    goto ip;

and who wouldn't! That fine piece of software engineering can best be expressed in the following Java code:

public class computed_goto
{ 
    public int i, j;
    boolean done = false;

    interface method
    {
        int invoke();
    }

    int example()
    {
        method [] methods = {
            new method() {
                public int invoke() {
                    i = 1;
                    j = -3;
                    return 1;
                } },
            new method() {
                public int invoke() {
                    if( i >= 10 )
                        done = true;
                    return 2;
                } },
            new method() {
                public int invoke() {
                    j += i;
                    i++;
                    return 1;
                } },
            };
        int ip = 0;
        while( !done )
            ip = methods[ip].invoke();
        return j;
    }
}

Trivial, really. Its a shame its not in the language core.