Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #32289

Re: PLEASE SOLVE THIS ERROR (case 1:from=="VIJAYAWADA")

From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: PLEASE SOLVE THIS ERROR (case 1:from=="VIJAYAWADA")
Date 2015-08-10 09:17 -0400
Organization A noiseless patient Spider
Message-ID <mqa870$9gi$1@dont-email.me> (permalink)
References <ebc4e4ca-b5e1-4da2-8583-1a668606cb2d@googlegroups.com> <mq70q5$20v$1@dont-email.me> <5b08503d-429d-429a-b1af-852563619faa@googlegroups.com>

Show all headers | View raw


On 8/10/2015 8:36 AM, e.d.programmer@gmail.com wrote:
> On Sunday, August 9, 2015 at 3:53:33 AM UTC-4, Eternal Hope wrote:
>> true is a Java keyword representing the boolean value 'true'
>> you can't switch on a boolean value
>
> I think switch used to work on a boolean,

     Perhaps in some pre-1.0 proto-Java ("Oak"), but never in Java.
The earliest sources I can lay my hands on are a "Java 1.1 Certification
Study Guide" and "Java in a Nutshell (Second Edition)", both from 1997,
and both agree that switch only works on int values and on things
that promote to int.

     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.

-- 
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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