| Richard 2006-03-08, 9:55 pm |
|
Frank Swarbrick wrote:
> Without something similar to COBOL's EVALUATE you must, in Java, do
> something like....
>
> if (myString == "one") {
> one();
> }
Actually you probably must not do that as it is not likely to do what
you intend.
> Wouldn't it be nicer to say:
> switch (myString) {
> case "one":
> one();
> break;
> case "two":
> two();
> break;
> case "three":
> three();
> break;
> default:
> four();
> break;
> }
No. Not nicer. I use Cobol to write Cobol programs. There is no case
statement at all in Python and I don't miss it.
> (Actually, eliminating those silly 'breaks' woul be even nicer, but...)
The 'breaks' are not 'silly'. They allow one to do:
case 1:
case 2:
dopart1and2;
break;
or even drop through:
case 1:
dopart1only;
case 2:
dopart1and2;
break;
But the break, or lack of it, is one of the bug traps in C-like
languges. In Cobol one of the bug traps is not having an imperitive
statement after a WHEN, or commenting it out, and having this drop into
the next action. Python eliminated the case statement exactly for the
reason that doing so removes the opportunity to create bugs.
> Of course the above assumes we're comparing the value of the object myString
> and not the object itself. I'm now totally as to which does
> what...
An == operator will only compare primitives. Object references are
primitives, so Object == Object is only true if the two object
references are identical, ie they both reference the same object.
|