• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

switch statement problem

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What will the output be ?
public static void main(String[] args){
char c = '\u0042';
switch(c) {
default:
System.out.println("Default");
case 'A':
System.out.println("A");
case 'B':
System.out.println("B");
case 'C':
System.out.println("C");
}
}

even value of char is 'c' ...output is b,c which according to me ..should be only C..plz explain
 
Ranch Hand
Posts: 1934
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you use
char c = '\u0043';
it will print only C.
 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
char c = '\u0042';
System.out.println(c);

output:
B

Here I didn't understand 'u0042'. I know ASCII values but not unicodes. Can any one tell me the unicodes for Alphabet and digts pls.
Thanx in advance

Naresh
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai prveen,

When u say "\u0042",it is eual to B.
In ur code there is know break statement after Second case statement,that is the reason why it is printing C also.

I am very poor when it comes to explanation.I hope u understand.

thank u.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
\u0042 is unicode representation of B . It will be hexa decimal .
If u convert that to decimal , 2*1+4*16 = 66 which is B . A is 65 and a is 97 i guess.. So it prints B and C .
 
Naresh Gunda
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanq RamaKrishna
 
Trust God, but always tether your camel... to this tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic