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?