Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #32292
| Newsgroups | comp.lang.java.programmer |
|---|---|
| Date | 2015-08-10 07:56 -0700 |
| References | <ebc4e4ca-b5e1-4da2-8583-1a668606cb2d@googlegroups.com> <mq70q5$20v$1@dont-email.me> <5b08503d-429d-429a-b1af-852563619faa@googlegroups.com> <mqa870$9gi$1@dont-email.me> |
| Message-ID | <da95e9b6-58c1-4cf9-afd3-593f8ee38409@googlegroups.com> (permalink) |
| Subject | Re: PLEASE SOLVE THIS ERROR (case 1:from=="VIJAYAWADA") |
| From | e.d.programmer@gmail.com |
On Monday, August 10, 2015 at 9:18:08 AM UTC-4, Eric Sosman wrote:
> Besides, what advantage would switch on a boolean have, when
> compared to an if or if/else?
>
> > then you could evaluate Strings in the case conditions by comparing your String to other Strings looking for the first case that evaluates to true.
>
> That can't work (unless I've completely misunderstood what you're
> saying). The switch statement is not an if ladder.
>
> > [...] The switch documentation states the switch value may now be a String starting with Java 7, int and enum also work.
>
> Section 14.11 of the JLS requires that the expression in switch
> must be char, byte, short, int, Character, Byte, Short, Integer,
> String, or an enum type.
>
> > Eclipse also flags an error if a case value is not a constant,
>
> Evidence that Eclipse has read JLS section 14.11.
>
> By the way, is anyone else horrified at the bytecode generated for
> a switch on a String value? It produces what amounts to *two* switch
> constructs *and* a set of if ladders (short ones with luck, but ...).
> Roughly speaking,
>
> switch (theString) {
> case "Foo": fooStuff(); break;
> case "Bar": barStuff(); break;
> case "Baz": bazStuff(); break;
> }
>
> turns into something like
>
> int _case = 0;
> switch (theString.hashCode()) {
> case 70822: if ("Foo".equals(theString)) _case = 1; break;
> case 66547: if ("Bar".equals(theString)) _case = 2; break;
> case 66555: if ("Baz".equals(theString)) _case = 3; break;
> }
> switch (_case) {
> case 1: fooStuff(); break;
> case 2: barStuff(); break;
> case 3: bazStuff(); break;
> }
>
> String-switching is a convenience, sometimes, but it's a costly one.
String switch may be ugly behind the scenes but makes the most readable code where the value you're checking is already a String. int switch may be easier but if you start with a String you have to convert it.
switch (theString) {
case "Foo": fooStuff(); break;
case "Bar": barStuff(); break;
case "Baz": bazStuff(); break;
}
This construct is easily readable and allows fall-through. If you wanted to do the same thing and say that all Foo are also Bar you could take out the break.
switch (theString) {
case "Foo": fooStuff();
case "Bar": barStuff(); break;
case "Baz": bazStuff(); break;
}
Alternatives?
1) Write an if statement (values must be mutually exclusive?):
if (theString.equals("Foo")) {
fooStuff();
} else {
if (theString.equals("Bar")) {
barStufF();
} else {
if (theString.equals("Baz")) {
bazStuff();
}
2) Write an int switch based on the if statement?
final int stringMatch;
if (theString.equals("Foo")) {
stringMatch = 1;
} else {
if (theString.equals("Bar")) {
stringMatch = 2;
} else {
stringMatch = 3;
}
switch (stringMatch) {
case 1: fooStuff(); break;
case 2: barStuff(); break;
case 3: bazStuff(); break;
}
With a boolean switch, if a case allows a variable, you could do what the OP suggests:
switch (true) {
case theString.equals("Foo"): fooStuff(); break;
case theString.equals("Bar"): barStuff(); break;
case theString.equals("Baz"): bazStuff(); break;
}
With this construct you could test anything from String matching to object methods which return boolean.
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
PLEASE SOLVE THIS ERROR (case 1:from=="VIJAYAWADA") srcommunicationsvja@gmail.com - 2015-08-08 06:53 -0700
Re: PLEASE SOLVE THIS ERROR (case 1:from=="VIJAYAWADA") Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-08-08 10:39 -0400
Re: PLEASE SOLVE THIS ERROR (case 1:from=="VIJAYAWADA") Eternal Hope <laughingspam99@gmail.com> - 2015-08-09 08:53 +0100
Re: PLEASE SOLVE THIS ERROR (case 1:from=="VIJAYAWADA") e.d.programmer@gmail.com - 2015-08-10 05:36 -0700
Re: PLEASE SOLVE THIS ERROR (case 1:from=="VIJAYAWADA") Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-08-10 09:17 -0400
Re: PLEASE SOLVE THIS ERROR (case 1:from=="VIJAYAWADA") e.d.programmer@gmail.com - 2015-08-10 07:56 -0700
Re: PLEASE SOLVE THIS ERROR (case 1:from=="VIJAYAWADA") Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-08-10 11:50 -0400
Re: PLEASE SOLVE THIS ERROR (case 1:from=="VIJAYAWADA") Arne Vajhøj <arne@vajhoej.dk> - 2015-08-12 20:51 -0400
Re: PLEASE SOLVE THIS ERROR (case 1:from=="VIJAYAWADA") Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-08-13 08:25 -0400
Re: PLEASE SOLVE THIS ERROR (case 1:from=="VIJAYAWADA") leif.roar@dimnakorr.com - 2015-08-13 08:43 -0500
Re: PLEASE SOLVE THIS ERROR (case 1:from=="VIJAYAWADA") Eternal Hope <laughingspam99@gmail.com> - 2015-08-10 15:39 +0100
csiph-web