Re: Java: Switch Or If Else

  • From: Andreas Stefik <stefika@xxxxxxxxx>
  • To: programmingblind@xxxxxxxxxxxxx
  • Date: Wed, 23 Feb 2011 13:10:46 -0600

Yes, it is. The primary difference with lookup is that there is
usually one grand-poo-ba Hash in the sky that stores references to all
classes (jumps) that can be loaded on the system. So, for example, if
you are in the NetBeans Platform, and you want to see if Python is
available in your build as a plugin, you could do something
metaphorically like:

PythonParser parser = Lookup.getDefault().lookup(PythonParser.class)

And then the global hash in the sky gets referenced and you can jump
to the appropriate code. So yaa, Lookup is implemented, basically,
with a dictionary.

Stefik

On Wed, Feb 23, 2011 at 11:19 AM, Homme, James <james.homme@xxxxxxxxxxxx> wrote:
> Hi,
> Is this like a dictionary in JavaScript or Python, or a hash in Perl?
>
> Thanks.
>
> Jim
>
> -----Original Message-----
> From: programmingblind-bounce@xxxxxxxxxxxxx 
> [mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Andreas Stefik
> Sent: Wednesday, February 23, 2011 11:06 AM
> To: programmingblind@xxxxxxxxxxxxx
> Subject: Re: Java: Switch Or If Else
>
> To echo Dave, a common example of this sort of thing in modern
> programming is called the Lookup Pattern. Essentially, you have a hash
> table that stores a delegate interface, like actions. You can then
> query the hash (average case big Oh of 1 access), for a particular
> implementation or under a certain condition (e.g., a key, a class
> type). and it will return to you the appropriate place to "jump" if
> you will.
>
> Almost all modern IDE's, and many kinds of dynamic plugin systems,
> also work this way,
>
> Stefik
>
> On Wed, Feb 23, 2011 at 7:49 AM, qubit <lauraeaves@xxxxxxxxx> wrote:
>> Well, since cases in C, C++ and java all require explicit break statements
>> to prevent fall-through, and a pile of case labels can be attached to the
>> same code, a switch is not a 1-1 mapping.
>> In fact it can have some truely harry logic.
>> But as for avoiding gotos by using function pointers, this wouldn't work
>> well because of the case where the code for one case falls through to the
>> next one.  If you invoke a function you wouldn't necessarily have that
>> option.
>>
>> If I sound a bit groggy it's because I've been up all night trying to get a
>> package to compile on windows7 64 bit.
>> I need to go dump ice water over my head or something -- I want to finish,
>> but I am winking dreams of code fragments tearing apart my system... Oops --
>> that wasn't a dream??? *scream* *smile*
>>
>> Happy hacking.
>> --le
>>
>>
>> ----- Original Message -----
>> From: "Dave" <davidct1209@xxxxxxxxx>
>> To: <programmingblind@xxxxxxxxxxxxx>
>> Sent: Wednesday, February 23, 2011 7:01 AM
>> Subject: Re: Java: Switch Or If Else
>>
>>
>> Or to avoid the use of goto's...use a hash between your key (string,
>> int, etc) and a delegate interface (in java), or a func ptr in C/C++.
>> Plenty of ways to do this.  This of course, means you have memory
>> overhead to consider.  The solution I think Sina suggested is writing
>> asm so that you use the input to switch(input) as a base + offset to
>> the case label.  (i.e. $pc + "input" would be the location of the case
>> label pertaining to "input").
>>
>> Of course, this is all predicated on the assumption that you're
>> interested in a one to one mapping and not a mapping of ranges or some
>> other more complex boolean condition which James didn't assume btw.
>> (i.e. a >= 10 && a <= 20, foo->isNull() && baz->bar()); in which case,
>> if/else is still a good branching technique.
>>
>> On 2/23/11, qubit <lauraeaves@xxxxxxxxx> wrote:
>>> Actually yes -- instead of the old fashioned integer cases on the C switch
>>> you can have a hash lookup on x that maps to a label to jump to.  Like in
>>> pseudo-C++ you would say
>>>
>>> Map<String,Label> jumpTable;
>>> ...
>>> l = jumpTable[x];
>>> if l is null, goto defaultLabel;
>>> else goto l;
>>>
>>> Or, if Map<String,Label> is defined so new entries are initialized with
>>> value = defaultLabel, then you wouldn't need the if statement at all.
>>> Just say
>>>
>>> goto jumpTable[x];
>>>
>>> Then the lookup on x will either find a match and would therefore have a
>>> label defined, or if x isn't in the table, the lookup operation would
>>> create
>>> a "null" entry with defaultLabel as its value.
>>>
>>> Hope this makes sense.  I have seen C++ programs that do a truely volumous
>>> amount of work with just 1 or 2 lines of code using libraries that do most
>>> the work in a (hopefully) efficient manner.
>>>
>>> Happy hacking.
>>> --le
>>>
>>> ----- Original Message -----
>>> From: "Homme, James" <james.homme@xxxxxxxxxxxx>
>>> To: <programmingblind@xxxxxxxxxxxxx>
>>> Sent: Wednesday, February 23, 2011 5:46 AM
>>> Subject: RE: Java: Switch Or If Else
>>>
>>>
>>> Hi,
>>> Python has code that looks something like this.
>>>
>>> If x in MyList:
>>>   do some stuff here.
>>>
>>> Is that the same thing?
>>>
>>> Thanks.
>>>
>>> Jim
>>>
>>> Jim Homme,
>>> Usability Services,
>>> Phone: 412-544-1810. Skype: jim.homme
>>> Internal recipients,  Read my accessibility blog. Discuss accessibility
>>> here. Accessibility Wiki: Breaking news and accessibility advice
>>>
>>>
>>> -----Original Message-----
>>> From: programmingblind-bounce@xxxxxxxxxxxxx
>>> [mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of qubit
>>> Sent: Tuesday, February 22, 2011 10:02 PM
>>> To: programmingblind@xxxxxxxxxxxxx
>>> Subject: Re: Java: Switch Or If Else
>>>
>>> another thing about switch statements for simple conditions like int or
>>> character literal values,  is that the compiler can do some tricky things
>>> to
>>> speed up the code -- for example, if you compare an int to values
>>> 1,2,3,...,100, and they all have code to execute, the compiler can do
>>> something like create a jump array and transform the switch to code like
>>> the
>>> following (pseudo code -- I don't know what java low level code looks
>>> like):
>>>
>>> jumpArray[100] = { label1, label2, label3, ... label100 };
>>> # the switch statement "switch (val) becomes:
>>> if x is in range, then goto jumpArray[x-1]
>>> else goto defaultLabel
>>>
>>> I remember seeing this kind of thing in the code I used to support.
>>> Of course that was a c/c++ compiler, but with java there might be similar
>>> optimizations.
>>> --le
>>>
>>> ----- Original Message -----
>>> From: "Dave" <davidct1209@xxxxxxxxx>
>>> To: <programmingblind@xxxxxxxxxxxxx>
>>> Sent: Tuesday, February 22, 2011 7:43 PM
>>> Subject: Re: Java: Switch Or If Else
>>>
>>>
>>> That's assuming you don't have complex conditions you're checking for;
>>> sometimes, if/else if is the only way to go.  When code becomes
>>> long/slightly unreadable, refactoring usually is a good idea.
>>>
>>> On 2/22/11, Homme, James <james.homme@xxxxxxxxxxxx> wrote:
>>>> Hi,
>>>> Thanks, guys. When I saw Ken's example, I liked the switch better.
>>>>
>>>> Jim
>>>>
>>>> Jim Homme,
>>>> Usability Services,
>>>> Phone: 412-544-1810. Skype: jim.homme
>>>> Internal recipients,  Read my accessibility
>>>> blog<http://mysites.highmark.com/personal/lidikki/Blog/default.aspx>.
>>>> Discuss accessibility
>>>> here<http://collaborate.highmark.com/COP/technical/accessibility/default.aspx>.
>>>> Accessibility Wiki: Breaking news and accessibility
>>>> advice<http://collaborate.highmark.com/COP/technical/accessibility/Accessibility%20Wiki/Forms/AllPages.aspx>
>>>>
>>>> From: programmingblind-bounce@xxxxxxxxxxxxx
>>>> [mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Sina Bahram
>>>> Sent: Tuesday, February 22, 2011 1:02 PM
>>>> To: programmingblind@xxxxxxxxxxxxx
>>>> Subject: RE: Java: Switch Or If Else
>>>>
>>>> I can't agree more
>>>>
>>>> Switch is so superior to chains of if statements.
>>>>
>>>> Take care,
>>>> Sina
>>>>
>>>> From: programmingblind-bounce@xxxxxxxxxxxxx
>>>> [mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Ken Perry
>>>> Sent: Tuesday, February 22, 2011 12:59 PM
>>>> To: programmingblind@xxxxxxxxxxxxx
>>>> Subject: RE: Java: Switch Or If Else
>>>>
>>>>
>>>>
>>>> I find switch much easier to use and read in large if else messes.  For
>>>> example in a multi level if else like this.
>>>>
>>>> If (){
>>>> If (){
>>>> If (){
>>>> }else{
>>>> }
>>>> }else{
>>>> }
>>>> }else{
>>>> If (){
>>>> If (){
>>>> }else{
>>>> }
>>>> }else{
>>>> }
>>>> }
>>>>
>>>>
>>>> Could look like this
>>>>
>>>> Switch ()
>>>> Case 1:
>>>> If (){
>>>> }else{
>>>> }
>>>> Case 2:
>>>> If (){
>>>> }Else{
>>>> }
>>>>
>>>>
>>>>
>>>> From: programmingblind-bounce@xxxxxxxxxxxxx
>>>> [mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Homme, James
>>>> Sent: Tuesday, February 22, 2011 12:41 PM
>>>> To: programmingblind@xxxxxxxxxxxxx
>>>> Subject: Java: Switch Or If Else
>>>>
>>>> Hi,
>>>> Would there be a time when you would choose to use switch, and another
>>>> when
>>>> you would choose to use if else? For example, if doing it one way or the
>>>> other would make a program easier to read. When I was just thinking about
>>>> this, it seemed to me that if you would choose if else, you wouldn't have
>>>> to
>>>> keep remembering to put in break statements. For ease of reading, it
>>>> would
>>>> seem that the code would take less time to listen to if you would choose
>>>> if
>>>> else.
>>>>
>>>> Thanks.
>>>>
>>>> Jim
>>>> Jim Homme,
>>>> Usability Services,
>>>> Phone: 412-544-1810. Skype: jim.homme
>>>> Internal recipients,  Read my accessibility
>>>> blog<http://mysites.highmark.com/personal/lidikki/Blog/default.aspx>.
>>>> Discuss accessibility
>>>> here<http://collaborate.highmark.com/COP/technical/accessibility/default.aspx>.
>>>> Accessibility Wiki: Breaking news and accessibility
>>>> advice<http://collaborate.highmark.com/COP/technical/accessibility/Accessibility%20Wiki/Forms/AllPages.aspx>
>>>>
>>>>
>>>> ________________________________
>>>> This e-mail and any attachments to it are confidential and are intended
>>>> solely for use of the individual or entity to whom they are addressed. If
>>>> you have received this e-mail in error, please notify the sender
>>>> immediately
>>>> and then delete it. If you are not the intended recipient, you must not
>>>> keep, use, disclose, copy or distribute this e-mail without the author's
>>>> prior permission. The views expressed in this e-mail message do not
>>>> necessarily represent the views of Highmark Inc., its subsidiaries, or
>>>> affiliates.
>>>>
>>> __________
>>> View the list's information and change your settings at
>>> //www.freelists.org/list/programmingblind
>>>
>>> __________
>>> View the list's information and change your settings at
>>> //www.freelists.org/list/programmingblind
>>>
>>> __________
>>> View the list's information and change your settings at
>>> //www.freelists.org/list/programmingblind
>>>
>>> __________
>>> View the list's information and change your settings at
>>> //www.freelists.org/list/programmingblind
>>>
>>>
>> __________
>> View the list's information and change your settings at
>> //www.freelists.org/list/programmingblind
>>
>> __________
>> View the list's information and change your settings at
>> //www.freelists.org/list/programmingblind
>>
>>
> __________
> View the list's information and change your settings at
> //www.freelists.org/list/programmingblind
>
> __________
> View the list's information and change your settings at
> //www.freelists.org/list/programmingblind
>
>
__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind

Other related posts: