This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Char type

 
Deepa Priyesh
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class IntToChar
{
public static void main(String[] args)
{
char ch = -90;
System.out.println(ch);
}
}
when i run the above code its gives a compilation error - possible loss of precision. My doubt is when i give char ch = 90; i get an out put z, despite of the fact that 90 is taken 32 bit integer which is put into a 16 bit char. How is ch =-90 a different from that.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A character can only take values between 0 and 65535 (2^16 - 1), the latter also written as \uFFFF. char is the only unsigned type in Java.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

JLS - 5.2 Assignment Conversion explains...
...if the expression is a constant expression (�15.28) of type byte, short, char or int ... [a] narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable.

In this example, the value of the constant expression is negative, so it is not representable in the type of the variable (which must be positive).
[ November 06, 2007: Message edited by: marc weber ]
 
Deepa Priyesh
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob and Marc. I understood the fact that char type does not take negative values. May be i am thinking of it at the bit level and confusing my self. I am not still very clear about it. My doubt is

short 90 is 0000 0000 0101 1010
short -90 is 1111 1111 1010 0110

Now if we put 1111 1111 1010 0110 in a (unsigned type) char, shouldn't it be taken as decimal 65446 which would fall well within the range of char type(0 - 65536).

So char ch should accept a value -90 ??.

I am not very sure about my knowledge in bit calculations.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Deepa Priyesh:
...Now if we put 1111 1111 1010 0110 in a (unsigned type) char, shouldn't it be taken as decimal 65446 which would fall well within the range of char type(0 - 65536)...

That is exactly what would happen if you forced the assignment using an explicit cast...

But without the explicit cast (which tells the compiler that you understand you might be losing information), char x = -90; simply isn't allowed.

Going back to my quote from the Java Language Specification above, when it says, "the value of the constant expression," this means the decimal value (which is -90) -- not the binary representation (which could mean different things depending on the context).
[ November 10, 2007: Message edited by: marc weber ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic