• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help needed in switch statement.  RSS feed

 
Pooja Patole
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ,
Can a switch statement take String as an argument.? I tried but it says that switch can only take int values and enum values.
I want to try something like this
 
John Jai
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use String in switch statements from Java 7 version. Read switch statement
 
Amit Ghorpade
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are not using Java 7, then using enum would achieve something similar.
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amit Ghorpade wrote:If you are not using Java 7, then using enum would achieve something similar.

An enum is preferable anyway, as it can not be misspelled and has a finite number of possible values.
 
Krishna Srinivasan
Ranch Hand
Posts: 1880
Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,

In the Project Coin (Java 7), using the string literals in switch is allowed.
 
Randall Twede
Ranch Hand
Posts: 4696
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
using Strings in switch statements is very helpful sometimes, but as was mentioned it is new to java 7
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!