Win a copy of Fixing your Scrum this week in the Agile 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Char type

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 22385
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
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
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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
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 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 ]
 
Yes, my master! Here is the tiny ad you asked for:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic