aspose file tools*
The moose likes Beginning Java and the fly likes Char type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Char type" Watch "Char type" New topic
Author

Char type

Deepa Priyesh
Greenhorn

Joined: Sep 07, 2007
Posts: 6
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

Joined: Oct 27, 2005
Posts: 19672
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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 ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Deepa Priyesh
Greenhorn

Joined: Sep 07, 2007
Posts: 6
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

Joined: Aug 31, 2004
Posts: 11343

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 ]
 
jQuery in Action, 2nd edition
 
subject: Char type