Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

use of Wrappers in switch() and case x: statements

 
Anuradha Prasanna
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


In the above program,
when i used Integer x1 inside switch() as, switch(x1) ,it works.
But when i use x2 in a case statement as, case x2 : s += "c" , does not compile. WHY???
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is because the value after case needs to be a compile time constant and x2 is not because wrapping happens at runtime. If you change it to a int it works just fine.
 
Anuradha Prasanna
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the explanation, Wouter.
 
Jeevan Reddy
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouter Oet wrote:That is because the value after case needs to be a compile time constant and x2 is not because wrapping happens at runtime. If you change it to a int it works just fine.

Thanks Wouter
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!