aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes use of Wrappers in switch() and case x: statements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "use of Wrappers in switch() and case x: statements" Watch "use of Wrappers in switch() and case x: statements" New topic
Author

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

Anuradha Prasanna
Ranch Hand

Joined: Mar 09, 2006
Posts: 115


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???


SCJP 6.0 90%
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

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.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Anuradha Prasanna
Ranch Hand

Joined: Mar 09, 2006
Posts: 115
Thanks for the explanation, Wouter.
Jeevan Reddy
Ranch Hand

Joined: Nov 10, 2009
Posts: 142
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


SCJP 1.6 (94%)
http://faq.javaranch.com/java/JspFaq
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: use of Wrappers in switch() and case x: statements