[205] in java-interest

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

Re: goto and switch

daemon@ATHENA.MIT.EDU (Arthur van Hoff)
Tue Jun 6 19:42:44 1995

Date: Tue, 6 Jun 1995 16:33:44 -0700
From: Arthur.Vanhoff@Eng.Sun.COM (Arthur van Hoff)
To: java-interest@java.Eng.Sun.COM

Hi Jeremy,

> 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?

No. The code is illegal. The case statements are not inside the
switch.

> 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.

In Java it is possible to know when a variable could be used
uninitialized. You have to do fairly sophisticated data flow to
make sure...

> 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?

The can only be constant integer expressions. The value has to
be known at compile time.

> 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.

Right. However, the compiler always generates a constant table entry so
that the dependencies can be checked correctly. If you want to make
sure that you get all the dependencies right you have to use the
-depend flag of the compiler.

> 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)?

It is for switches that can be represented that way.

Have fun,

	Arthur van Hoff (avh@eng.sun.com)
	http://java.sun.com/people/avh/
	Sun Microsystems Inc, M/S UPAL02-301,
	100 Hamilton Avenue, Palo Alto CA 94301, USA
	Tel: +1 415 473 7242, Fax: +1 415 473 7104

-
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