• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Strange behaviour in char expression inside switch

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi friends,
The following code doesn't print '?'.
But if the case expression is changed to case 65535 it prints '?'.I cannot understand this strange behaviour.Please help
class SwitchTest
{
public static void main(String args[])
{
char c = 65535;
switch (c)
{
case '?' :
System.out.println(c);
}
}
}

char value of 65535 is '?'
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ganeshan,
You are wrong. char value of '?' is NOT 65535.
The character '?' is printed out if we tried to print any
non-printable characters or the characters which our system won't recognise.
Hope this helps,

------------------
Regards
---------
vadiraj

*****************
There's a lot of I in J.
*****************
 
ganeshan thangamuthu
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks vadiraj.
i got it
 
Ranch Hand
Posts: 144
Redhat Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ganesh I tried your code with similar values at assigning the char value and at the case statement.
It works fine for both
My conclusion is that in case statement exact replica of the required variable is needed and there is no retrieval of the char value which it might contain.
Please correct me if I am wrong.
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The bottomline in the switch..case structure is that the case label should be implicitly assignable to the integral expression in the switch argument (can be bytem short, char, int).
Why going for 65535. Try out with this
class SwitchTest1
{
public static void main(String args[])
{
char c = 65;
switch (c)
{
case 'A' :
System.out.println(c);
}
}
}

It prints A.
char c=65
c='A';
both r same.
 
permaculture is giving a gift to your future self. After reading this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic