Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

In C language char has range from -127 to 128 but in Java char range is from 0 to 65535.  RSS feed

 
karthikeya kumar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In C language char has range from -127 to 128 but in Java char range is from 0 to 65535.Can I know why dont we have negative value range for char data type in Java
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
karthikeya kumar wrote:In C language char has range from -127 to 128 but in Java char range is from 0 to 65535.Can I know why dont we have negative value range for char data type in Java


I would reverse the question. ASCII, extended ASCII, Unicode, etc. are all using non-negative values. So, what would the purpose of having negative values?

Henry
 
karthikeya kumar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
karthikeya kumar wrote:In C language char has range from -127 to 128 but in Java char range is from 0 to 65535.Can I know why dont we have negative value range for char data type in Java


I would reverse the question. ASCII, extended ASCII, Unicode, etc. are all using non-negative values. So, what would the purpose of having negative values?

Henry


Hi Henry ,

I am new to Java. I cant answer to your question. Please expecting a positive reply from you instead of a question back to me. I wish you understand

thanks
 
Stephan van Hulst
Saloon Keeper
Posts: 7802
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry's reply is an answer in question form.

The char datatype in Java contains two bytes of a UTF-16 representation of a Unicode code point. Unicode code points are all non-negative. There is no purpose in representing them with negative values.

So why does C do it? You should ask the C designers. What's important is that Java is a different language from C, and that many designs they used in C didn't carry over to Java, often with good reasons.

Making comparisons between Java and C is rarely useful.
 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
karthikeya kumar wrote:In C language char has range from -127 to 128 but in Java char range is from 0 to 65535.Can I know why dont we have negative value range for char data type in Java

A C char and a Java char are both called "char," but that's about where their similarity ends. A java char is slightly similar to a C unsigned short.

C's char does, as you say, have a range from -128 to 127. Is that useful for anything? Never has been, for anything I've ever done. Now, an unsigned char has a range from 0 to 255, which is exactly what you want if you are doing image processing (which I do a lot of). Why doesn't Java have such a data type? Well, the answer is somewhat controversial, but an interview with one of the language designers has him saying he felt most programmers didn't really comprehend unsigned types. My reaction is that he was the one who didn't really understand them, as I have worked with some world-class computer graphics programmers (you know, those ignoramuses who went on to create ILM and Pixar), who managed to write thousand and thousands and thousands of lines of code they actually, truly, utterly did comprehend, and that used unsigned chars to great effect.

You will find, however, that the Java community is pretty consistent in its view that unsigned bytes are a bad idea. (Then again, you will find that there are few image-processor or animation programmers using Java, so what's that tell you?) At a deeper level, the Java virtual machine converts the one-byte bytes to four-byte integers whenever your compiled Java code manipulates such things, so even adding support for unsigned bytes to Java wouldn't really do you any good, as the run-time overhead created by "enlarging" bytes to integers would still be there (unless and until the virtual machine were redesigned to support one-byte values directly, something I think I can say with confidence will never, ever happen).

Defenders of the no-unsigned-bytes decision usually make two arguments: First, they agree with the Java designers that people find them confusing. Second, they point out that there are work-arounds, so the lack of unsigned bytes is not a barrier to doing what you would do if you had them. As to the first, I am not confused, so I am not impressed by the confusion others would have if the keyword "unsigned" did what I wish it did in Java, particularly since those who would be confused would continue to be able to write the exact same code they would have written anyway, blissfully ignorant of the existence and confusing (or not) effect of the "unsigned" keyword. As to the second, the existence of a work-around is not a reason to leave something useful out of a language, unless one believes that, say, the omission of floating point math can be defended by pointing out that a characteristic and mantissa can be implemented in a class that uses integer member variables.

Those readers who have written thousands of lines of image-processing code in Java, and who want to argue in favor of not having unsigned bytes in the language, are invited to weigh in. Those who have not written thousands of lines of image processing code, and who want to remind me, again, of what a great and very inconsequentially problematic idea it was to leave them out, are asked to trust my ability to remember the last three times we had this debate, and not repeat themselves. I know I lost this argument a long time ago. Let me just brood over it in peace, eh?
 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Making comparisons between Java and C is rarely useful.

I would say that it is rarely useful to expect Java and C to behave similarly just because a lot of C constructs and a lot of Java constructs have superficial similarities. The difference between a C char and a Java char is a good example of why a person should not expect two superficially similar constructs in the two languages to behave in similar ways.

When learning Java, however, I found that well informed comparisons to C were helpful. But those had to come from people who already knew what I was trying to learn. Trying to learn Java by expecting it to be some kind of version of C is, indeed, an exercise in self-delusion, failed expectations, and a river of tears.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!