Question:

Hi!i am appearing for scjp 1.5!I have a problem with this code!!Can u help me out?

by  |  earlier

0 LIKES UnLike

//It's printing the default value,how do i get the //first value printed??

class Tester{

public static void main(String[]ar){

char a='a';

switch (a){

case 1:

System.out.println("X's a");

break;

case 2:{

System.out.println("X's 2");

break;}

default:

{System.out.println("No X");

break;}

}}}

 Tags:

   Report

2 ANSWERS


  1. hi dude the ascii value of 'a' is 97 which is taken as value of a in switch as there is no case 97 default value gets printed


  2. hi buddy ,

    the reason it is printing the defualt value, is because java compilers

    take the ascii equivalent of a which is 97

    to make it print the first value , you can say

    case 65 : instead of 1 and it should work  

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.