GeeCON Prague 2014*
The moose likes Features new in Java 7 and the fly likes Java 7 - String literals in Switch statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Features new in Java 7
Bookmark "Java 7 - String literals in Switch statement " Watch "Java 7 - String literals in Switch statement " New topic
Author

Java 7 - String literals in Switch statement

Baseet Ahmed
Ranch Hand

Joined: Dec 18, 2006
Posts: 224
Salaam/GA!

As most of you know that in Java 7 they have added a new feature "Strings literals in switch statement"
Upon reading Java 7 New Features Cookbook online...

I realized that why there was a need for string literals?

If so, then why they have not made switch statement to take any OBJECT?

...........
Regards
Baseet Ahmed
Knowledge is not to be memorized but to benefit others - ISLAM
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  16

Each case in a switch statement is supposed to compare the switch argument with a unique value. The compiler can check this uniqueness for literals and enum constants only. For instance, the compiler can reason that "Hello" is a different String from "World", so the two cases are disjoint.

For any old object this is not possible. The compiler can't check that two Object cases are unequal to each other, unless the compiler runs the code before it's completely compiled.
Richard Reese
author
Ranch Hand

Joined: Jul 13, 2011
Posts: 84
    
    5

I concur with Stephan. If you would like more detail about how Java handles strings in a switch statement, I would refer you to the following site: https://blogs.oracle.com/darcy/entry/project_coin_string_switch_anatomy.


Richard Reese
Java 8 New Features: A Practical Heads-Up Guide
Baseet Ahmed
Ranch Hand

Joined: Dec 18, 2006
Posts: 224

Thank you for reply.

I more thing. If I use toString() method on any object then can I pass it to switch statement with success at compilation stage?

...........
Regards
Baseet Ahmed
Knowledge is not to be memorized but to benefit others - ISLAM
Richard Reese
author
Ranch Hand

Joined: Jul 13, 2011
Posts: 84
    
    5

You have to use the toString method as follows:

Book b = ...;
switch(b.toString()) {

You cannot use the reference variable by itself. I hope this helps.
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3305
    
    7
Stephan van Hulst wrote:Each case in a switch statement is supposed to compare the switch argument with a unique value. The compiler can check this uniqueness for literals and enum constants only. For instance, the compiler can reason that "Hello" is a different String from "World", so the two cases are disjoint.

For any old object this is not possible. The compiler can't check that two Object cases are unequal to each other, unless the compiler runs the code before it's completely compiled.


That explains why it can't be done at compile time, but then why does it have to be done at compile time?

Why can't a switch statement work like an if-else if-else statement (if there is an dynamic object involved)? After all, there is little difference between the two constructs, at least functionally.


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Richard Reese
author
Ranch Hand

Joined: Jul 13, 2011
Posts: 84
    
    5

Switch statements, by their nature, require constant values for their cases. Thus they cannot be dynamic. There are a lot of things that a compiler can do for you. However, not all of these are easy and when they are not, compiler writers often elect not to do them. On the surface some operations may appear to be simple but in reality they are not. I am not sure this completely answers your question.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2383
    
  28

IMO, the answer is that at this point, it;s too hard to add it into Java compilers. From the POV of the people who implement compilers going from a point where case only has constants to case having any ol expression is pretty big. It's a pretty big change in the grammar

I don;t think it;s impossible, just not worth it. You can do pretty much what you want using if-else, or you can always implement a function that does the comparisons for you and returns an enum that you can use in the switch.
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3305
    
    7
Richard, Jayesh, I agree with what you said. The only reason I posted that question was to basically drive that point. The real reason for not allowing any object in a case is because that is how it is designed. It just a a different tool with a different flavor than an if-else if. There is no particular technical reason for not allowing any object in a case, IMHO.
Baseet Ahmed
Ranch Hand

Joined: Dec 18, 2006
Posts: 224


Thanks for replies.


Regards
Ahmed..

"Eat your Dinner positively whether you have only fistful of Dates because the old age rapidly advents by not eating the Dinner. The man suffers from Weakness and Debility" [Final Prophet Mohammed - peace and blessings be upon Him]
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

String literals are compile time constants. But what about a String created by new i.e.


Based on my understanding that i had, the String literals were resolved at compile time but a String created by new is resolved at runtime and then the object is made. Based on this theory, the above code should not run but it does run perfectly fine. This shows that the String creation on a whole has been optimized to run under all circumstances.


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

The argument to the switch never needed to be a compile time constant. It's the case labels that need to be constants. For instance, the following will not work:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

Oh yes its the Switch case that needs to be a compile time constant and not the Switch expression.

Thanks Rob for making it crystal clear for me.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

You're welcome.
 
GeeCON Prague 2014
 
subject: Java 7 - String literals in Switch statement