This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Characters

 
Gaia Nathan
Ranch Hand
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! I have a question. Why is this allowed?
char c = 34;
Shouldn't it be:
char c = (char)34;
When I tried it out in a test program, the output for both lines of code was the same, the " character.
I think I'm missing something here. Thank you in advance.
 
Bosun Bello
Ranch Hand
Posts: 1510
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The cast was not needed because 34 is within the valid range for a character. The conversion is done automatically here. try assing a very large number to your character. The compiler will complain. ex. char c = 100000

Bosun
 
Gaia Nathan
Ranch Hand
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bosun.
Are these valid ways of creating and initializing char variables?
char c = 'U';
char c = 67;
char c = '\u0067';
char c = '\r';
Thanks again.
 
Michael Fitzmaurice
Ranch Hand
Posts: 168
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, the fact that you have assigned a literal value makes it legal without the cast. If you tried something like:
<code>
<pre>
byte b = 34;
char c = b;
</pre>
</code>
the compiler would complain, telling you an explicit cast was required.
------------------
"One good thing about music - when it hits, you feel no pain"
Bob Marley
 
Gaia Nathan
Ranch Hand
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah ha...I'm clearer now. Thanks to Bosun and Michael.
Michael, nice quote.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic