Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java 7 - String literals in Switch statement

 
Baseet Ahmed
Ranch Hand
Posts: 225
Java Notepad Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 5797
61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 84
5
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Baseet Ahmed
Ranch Hand
Posts: 225
Java Notepad Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 84
5
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3743
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Richard Reese
author
Ranch Hand
Posts: 84
5
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3743
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 225
Java Notepad Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic