This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
In which version of Java are you running this? (Also, please make sure that if you post code to make it compilable, or indicate specifically that it won't. Thanks!)
satish varma
Greenhorn
Joined: Feb 11, 2010
Posts: 27
posted
0
yes we can
in wich version of java this code? as per my knowledge " byte,short,int,char" and their corresponding wrapper classes in addition to these "enum" are possible arguments
As noted, above the String example is not valid. Currently (Java 6 and below), two values must be comparable with == to be used in switch. Since this is not the case for String, it cannot be used.
satish varma wrote:in wich version of java this code?
I totally just said that.
satish varma
Greenhorn
Joined: Feb 11, 2010
Posts: 27
posted
0
two values must be comparable with == to be used in switch
check out that statements did you mean s1,s2 are not comparable?
ok any how, every case label must be a constant or a valid expression that gives constant as result. For that the possible switch arguments are type
byte,
short,
int,
char,
Byte,
Short,
Integer,
Character,
enum
here "enum" type value represents constant, and it avialable from JDK 1.5 version onwards. other than these types are invalid
two values must be comparable with == to be used in switch
check out that statements did you mean s1,s2 are not comparable?
Since switch on string isn't supported yet -- a discussion about the implementation is kinda moot.
Regardless, with the proposed Java 7 implementation, I believe the proposal is for a switch on an integer (string hashcode), to be followed by a if check in the case body. And the check will be based on the value of the string (equals comparison).
Henry
satish varma
Greenhorn
Joined: Feb 11, 2010
Posts: 27
posted
0
ohh really!! i dont know about that thing, ok i will search
Thank you
Mike Simmons
Ranch Hand
Joined: Mar 05, 2008
Posts: 2778
2
posted
0
Henry Wong wrote:Since switch on string isn't supported yet -- a discussion about the implementation is kinda moot.
Not entirely - JDK 7 milestone 5 has this feature. It hasn't been made it to a final production release yet, but an implementation is out there.
Praveen Kumar
Ranch Hand
Joined: Nov 06, 2006
Posts: 133
posted
0
Hi ,
We can not use Strings inside the Switch statement. But we can use Compiletime Constants like final String TEMP in LABEL of the Switch statement.
Thanks for the link which explained proposed JAVA7 enhancement.
Mike Simmons
Ranch Hand
Joined: Mar 05, 2008
Posts: 2778
2
posted
0
Praveen Kumar wrote:We can not use Strings inside the Switch statement. But we can use Compiletime Constants like final String TEMP in LABEL of the Switch statement.
Ah, no.
In current Java releases (JDK 6), we cannot use Strings inside a switch statement, period.
In JDK 7, which is not yet officially released, we can use Strings inside a switch - both as the value being switched on, and the values in the case labels. For the case labels, the string values must be compile-time constants.
kalaiyarasan sivaprakasam
Greenhorn
Joined: Aug 17, 2009
Posts: 29
posted
0
Hi all,
i am using java 1.5.
hi Ranch Hand,Hi satish varma
String state variety = condition ? "fish" : "fowl";
in this ,
i do not understand what is state ,what is variety?
as of i know both are string objects.
i do not think it will execute properly..
i know we can use enum
thanks for the link for java 7....
thank you very much to all
with Regards,
Kalai.
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
4
posted
0
The bit about "state variety" is incorrect syntax.
satish varma
Greenhorn
Joined: Feb 11, 2010
Posts: 27
posted
0
yes me too agree with Ritchie
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.