This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java and have Dr. Raoul-Gabriel Urma & Richard Warburton on-line!
See this thread for details.
Win a flower (🌹) or copy of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java (📚) this week in the Agile and Other Processes forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
master stewards:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
garden masters:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
gardeners:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Java char data type problem

 
pioneer
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
master steward
Posts: 21864
105
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

A char can store exactly one character. "12" is not one but two characters - '1' and '2'. That means it cannot fit into a char. It does fit into a String, which uses double quotes: "12".
 
Marshal
Posts: 68009
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

I have added code tags to your post and you can see how much better it looks

People think that a char stores a character, but it doesn't really. It stores an (unsigned) integer between 0 and 0xffff (=65335, I think). As Rob has already told you, you can't store "12", but you can store 12.Note you need to cast the int 12 to a char, otherwise it won't fit into the available memory. You have already seen you can dispense with the cast if you declare and initialise the char on the same line.
I am not sure what you get when you print 12 out; the character is called 000c (c in hex = 12 in decimal), and you can find out what it is from this Unicode chart.
 
Rameshwar Soni
pioneer
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Spoor wrote:Welcome to the Ranch!

A char can store exactly one character. "12" is not one but two characters - '1' and '2'. That means it cannot fit into a char. It does fit into a String, which uses double quotes: "12".



Thanks for the reply.....Now i understood my mistake
 
Rameshwar Soni
pioneer
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome again

I have added code tags to your post and you can see how much better it looks

People think that a char stores a character, but it doesn't really. It stores an (unsigned) integer between 0 and 0xffff (=65335, I think). As Rob has already told you, you can't store "12", but you can store 12.Note you need to cast the int 12 to a char, otherwise it won't fit into the available memory. You have already seen you can dispense with the cast if you declare and initialise the char on the same line.
I am not sure what you get when you print 12 out; the character is called 000c (c in hex = 12 in decimal), and you can find out what it is from this Unicode chart.



Thanks for the reply.....Now i understood my mistake.Thanks
 
Pay attention! Tiny ad!
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!