Home arrow static arrow Java Programming [Archive] - how can I use Switch with String ?
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - how can I use Switch with String ?
This topic has 28 replies on 2 pages.    1 | 2 | Next »

Posts:44
Registered: 12/25/01
how can I use Switch with String ?  
Jan 11, 2002 2:54 AM



 
Hi !

I want to switch by Strings.
ie
String name;
switch(name)
{
case "Joe": ....

}

How can I do it ?

10x
 

Posts:468
Registered: 3/16/01
Re: how can I use Switch with String ?  
Jan 11, 2002 3:02 AM (reply 1 of 28)



 
tip: hashCode( ) will solve your problem!

If you need more help, then please let me know!
 

Posts:44
Registered: 12/25/01
Re: how can I use Switch with String ?  
Jan 11, 2002 3:42 AM (reply 2 of 28)



 
10x

can u explain me more
 

Posts:468
Registered: 3/16/01
Re: how can I use Switch with String ?  
Jan 11, 2002 5:03 AM (reply 3 of 28)



 
Every Object has a hashCode value, if you write new Objects you can add a hashCode method to override the super (Object) hashCode.

You can call the "".hashCode() to get the code of a String value ( int ).

e.g.

String name = "Two";
switch( name.hashCode() )
{
case 79430 : System.out.println(name + " = " + 1); break;
case 84524 : System.out.println(name + " = " + 2); break;
case 80786814 : System.out.println(name + " = " + 3); break;
default : System.out.println("Unknown");
}

This is a way to solve the problem...

79430 = "1".hashCode();

good luck!

 

Posts:2,586
Registered: 6/22/99
Re: how can I use Switch with String ?  
Jan 11, 2002 6:39 AM (reply 4 of 28)



 
It's a horrible idea to use the hashcode of a String in this way. The hashcode algorithm is not guaranteed to be the same on different platforms, or even on different versions of the same platform. You will find that the above code mysteriously fails on some platforms.

Remember, if it isn't guaranteed by the API docs, don't rely on it in your code!
 

Posts:331
Registered: 10/24/00
Re: how can I use Switch with String ?  
Jan 11, 2002 6:41 AM (reply 5 of 28)



 
The hashcode algorithm will not generate a unique id for all strings.... It is unreliably for the way you want to use it...
 

Posts:107
Registered: 12/12/98
Re: how can I use Switch with String ?  
Jan 11, 2002 8:02 AM (reply 6 of 28)



 
Switch doesn't accept Strings period.

JVM doesn't gurantee the same hashcode for everytime you invoke the application, it depends on the platform and memory usage.

Use string comparision (even though it's ugly).

/Sreenivasa Kumar Majji.

tip: hashCode( ) will solve your problem!

If you need more help, then please let me know!
 

Posts:278
Registered: 1/31/00
Re: how can I use Switch with String ?  
Jan 13, 2002 1:23 AM (reply 7 of 28)



 
JVM doesn't gurantee the same hashcode for everytime
you invoke the application, it depends on the platform
and memory usage.

for java.lang.String the hascode does not depend on the platform or memory use. since it overrides the hascode() method of Object and provides a hashcode depending in the contents of the string. But depending on the JVM you are using, it MAY be different, since JLS does not mandate any algorithm to calculate hashcode of string. I think it was actally changed once in 1.x-1.1.x era.
 

Posts:107
Registered: 12/12/98
Re: how can I use Switch with String ?  
Jan 13, 2002 7:07 AM (reply 8 of 28)



 
Really??

I tested some applications spcially invloved hashcodes on Solaris 8, Windows NT 4.0 SP6a, and Linux RedHat 6.2 (of course with the same JDK/JVM version) as part of my job. The hash codes are never same in any of two platforms.

/Sreenivasa Kumar Majji.

JVM doesn't gurantee the same hashcode for
everytime
you invoke the application, it depends on the
platform
and memory usage.

for java.lang.String the hascode does not depend on
the platform or memory use. since it overrides the
hascode() method of Object and provides a hashcode
depending in the contents of the string. But depending
on the JVM you are using, it MAY be different, since
JLS does not mandate any algorithm to calculate
hashcode of string. I think it was actally changed
once in 1.x-1.1.x era.
 

Posts:278
Registered: 1/31/00
Re: how can I use Switch with String ?  
Jan 13, 2002 12:45 PM (reply 9 of 28)



 
Really??

I tested some applications spcially invloved hashcodes
on Solaris 8, Windows NT 4.0 SP6a, and Linux RedHat
6.2 (of course with the same JDK/JVM version) as part
of my job. The hash codes are never same in any of two
platforms.

hascode() of which class??
 

Posts:262
Registered: 2/2/01
Re: how can I use Switch with String ?  
Jan 13, 2002 12:50 PM (reply 10 of 28)



 
String s = "string";switch(s.hashcode()){    case("something".hashcode()):{        doStuff();        break;    }    case("thing".hashcode()):{        doOtherStuff();        break;    }    case("some".hashcode()):{        doMoreStuff();        break;    }}


That will work on any platform
 

Posts:468
Registered: 3/16/01
Re: how can I use Switch with String ?  
Jan 14, 2002 12:58 AM (reply 11 of 28)



 
String s = "string";switch(s.hashcode()){case("something".hashcode()):{doStuff();break;}case("thing".hashcode()):{doOtherStuff();break;}case("some".hashcode()):{doMoreStuff();break;}}


That will work on any platform

The idea is like the that is mentioned earlier above, but this will not work while the hashCode is not a constant, its realtime calculated.
But its easy to come around that one...

and yes it works, on any platfrom... thats right.
 

Posts:166
Registered: 1/10/02
Re: how can I use Switch with String ?  
Jan 14, 2002 1:24 AM (reply 12 of 28)



 
don't use switch, because the switch construct is designed for int values. better idea is to use if ... else if ... else.
 

Posts:468
Registered: 3/16/01
Re: how can I use Switch with String ?  
Jan 14, 2002 1:58 AM (reply 13 of 28)



 
don't use switch, because the switch construct is
designed for int values. better idea is to use if ...
else if ... else.

Definitely... equal operations are not dangerous!

And I recommend "if" instead of "switch" in this place, for performance and standards.

And like it says... about avoiding java.lang.String.hashCode() .
If the String's length exceeds 16 characters, hashCode() samples only a portion of the String. So if the places that a set of Strings differ in don't get sampled you can see lots of similar hash values. This can turn your hash tables into linked lists!
 

Posts:1,860
Registered: 12/14/00
Re: how can I use Switch with String ?  
Jan 14, 2002 2:54 AM (reply 14 of 28)



 
String s = "string";switch(s.hashcode()){case("something".hashcode()):{doStuff();break;}case("thing".hashcode()):{doOtherStuff();break;}case("some".hashcode()):{doMoreStuff();break;}}


That will work on any platform

Actually, there is no JLS-conformant platform on which that code will work. But even if hashCode was spelt correctly and switch statements were allowed to use runtime values for cases, it would be perfectly legal for an implementation of the JRE to calculate the same hashCode for all three strings "something", "some" and "thing", so that doStuff would be called when s has the value "thing".

To achieve the desired effect in the original post for this thread, use
if ("Joe".equals(name)) {  doStuff();}

There is nothing you can do with a a switch construct that you can't do with if-else.
 
This topic has 28 replies on 2 pages.    1 | 2 | Next »