File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes switch case Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "switch case" Watch "switch case" New topic
Author

switch case

Balaji Bang
Ranch Hand

Joined: Apr 23, 2007
Posts: 180
// from devaka's Practice Exam3 modified a bit

OUTPUT : Default Only A Only B Only C

It is taking int values declared in main. But I want to use char A ,char B declared as instance variables. Means I want Output as Only A Only B Only C . How can I use Char instance variables in switch case???
Dawn Charangat
Ranch Hand

Joined: Apr 26, 2007
Posts: 249
Banu,

If you convert (char)10, you'll get the ASCII equivalent of 10, which is neither A,B or C. Hence the compiler chooses "default", and prints its value. But then, since you haven't mentioned the break; statement after default, it continues to execute other statements in the switch case bundle [till end, or until it encounters a break], and hence you are getting this output.

Dawn.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952


SCJP 6
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Punit Singh wrote:


Yet this does not address the question of using the static class varibles decalred. In this statement you are simply casting the local varibles to a char.

I tried this way:




I recieved the following output:



What function do you give to your final local variables in your original code?




be a well encapsulated person, don't expose your privates, unless you public void getWife()!
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: switch case
 
Similar Threads
an enums question
switch doubt..
switch statement - default case first
Switch- Case doubt in case expressions.
Switch case