• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Char assignments (help)

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, how can we print char x properly?

char x= 29
char y=(char)-60;
System.out.println("x: "+x);
System.out.println("y: "+y);
Ouput:
x: ?
y: (here i am getting a square shape character, prolly ascii)

Should I not get values of 29 and -60 for x and y? We can assign int to character then why is it giving a problem? Kindly comment. Thanks
 
Nomi Malo
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi forum moderators, kindly delete the other two instances of the same post. I guess my net was slow for a moment or javaranch site was very busy.
Thanks
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nomi Malik:
...Should I not get values of 29 and -60 for x and y? We can assign int to character then why is it giving a problem? ...


What output were you expecting?

A char holds a positive integer value between 0 and 65535. The value represents a character symbol that's displayed depending on your platform.

In your example, x has a value of 29, which in ASCII is a group separator. And y has a value (after the cast) of 65476.

If you are looking for output that your command prompt should be able to display, see this ASCII table and note the common symbols represented between values 33 and 126.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic