[200] in java-interest

home help back first fref pref prev next nref lref last post

goto and switch

daemon@ATHENA.MIT.EDU (Jeremy Fitzhardinge)
Tue Jun 6 17:20:14 1995

From: jeremy@sour.sw.oz.au (Jeremy Fitzhardinge)
To: java-interest@java.Eng.Sun.COM
Date: Tue, 6 Jun 1995 22:11:09 +1000 (EST)

In the new white-paper, it says:
    Eliminating goto led to a simplification of the language--there
    are no rules about the effects of gotoing into the middle of a for
    statement, for example.

However, the switch statement is much the same as goto in this case.
For example:

void nasty_switch(int f)
{
	switch (f)
	{
	default:int i = f;
	case 0: dosomething(i);
		for (i = 0; i < 100; i++) {
	case 1:		thing x = new thing(i);

	case 2:		if (i % 2 == 0) {
	case 3:			x.doit(i+j);
			}
	case 4:	}
	}
}

Does Java inherit all of C's switch statement semantics, including
mostrosities like this?

The language spec says the compiler has to analyse the source to
make sure that all variables are initialized.  In general this
isn't possible, and in this case it would take some work to do it.

Also, what types are the argument of switch and the case labels
allowed to be?  Any type, scalar, integer?  Do the cases have to
be compile-time constants, or can they be variable?

What would happen in a case like this:

import thing.myclass;

class x {
	void x(int i) {
		switch(i) {
		case myclass.const1: ...
		case myclass.const2: ...
		case myclass.const3: ...
		}
	}
}

If the compiler inlines myclass.constX, and the implementation of
thing.myclass changes between compiletime and runtime, then there's
obviously a problem.  However, there doesn't seem to be a mechanism
for implementing switches where the cases are non-constant.  From
looking at the VM spec, it seems that the table jumping instructions
can only take an array of constants as arguments.  On the other
hand, a switch could be implemented as a if/else ladder, which is
pretty ugly, but can implement the semantics reliably.

BTW, what is tableswitch used for?  Is it just for switches which can
be represented in that form, or is it for something else (exception
catching)?

	J

-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post