unsigned in java

M

Mark Thornton

asit said:
Why java does not have an unsigned data type ????

The unsigned types in C have been the cause of many bugs, particularly
when mixed with signed types (rather unexpectedly the signed types get
converted to unsigned). It would have been more sensible if the unsigned
types were promoted to signed values of the next larger size (where
possible). However had Java done this, there would have been endless
confusion with mistranslated C code. So the only practical alternative
to C's broken approach was to leave the unsigned types out.

Mark Thornton
 
M

Mark Thornton

Eric said:
But it does! With a range of '\u0000' through '\uffff' ...

As for the other possibilities, I don't know. Perhaps the
inventors thought it would be a needless complication. Or that
a keyword beginning "un" sounded too negative.

Speaking for myself, the only place I really regret their
decision is with `byte', which would be far more useful (to me,
anyhow) if its range were 0 through 255.
Given that all computations involving bytes are performed on int's,
there isn't much lost here. Lack of unsigned int is a bit more of a
nuisance as you have to go to a larger type (although if you are using a
64 bit machine that may not have performance implications either).

Mark Thornton
 
T

Tom Anderson

The unsigned types in C have been the cause of many bugs, particularly
when mixed with signed types (rather unexpectedly the signed types get
converted to unsigned). It would have been more sensible if the unsigned
types were promoted to signed values of the next larger size (where
possible). However had Java done this, there would have been endless
confusion with mistranslated C code. So the only practical alternative
to C's broken approach was to leave the unsigned types out.

That's certainly what James Gosling says:

"For me as a language designer, which I don't really count myself as these
days, what "simple" really ended up meaning was could I expect J. Random
Developer to hold the spec in his head. That definition says that, for
instance, Java isn't -- and in fact a lot of these languages end up with a
lot of corner cases, things that nobody really understands. Quiz any C
developer about unsigned, and pretty soon you discover that almost no C
developers actually understand what goes on with unsigned, what unsigned
arithmetic is. Things like that made C complex." [1]

"In programming language design, one of the standard problems is that the
language grows so complex that nobody can understand it. One of the little
experiments I tried was asking people about the rules for unsigned
arithmetic in C. It turns out nobody understands how unsigned arithmetic
in C works. There are a few obvious things that people understand, but
many people don't understand it." [2]

Personally, i think it's utter tosh. It's yet another example of java's
designers being shaped by their traumatic experience as C++ survivors
Unsigned arithmetic too complicated in C/C++? Leave it out! I don't think
it's hard to come up with a set of rules for unsigneds that let you do
what you need to do, and also have minimal gotchas. The key, i think,
would be to require conversion to be explicit (except in the case of a
widening conversion from unsigned to signed). If anyone knows more about
what's wrong with unsigneds in C/C++, i'd love to hear about it.

tom

[1] http://www.gotw.ca/publications/c_family_interview.htm

[2] http://www.artima.com/intv/gosling33.html
 
A

Arne Vajhøj

asit said:
Why java does not have an unsigned data type ????

http://www.gotw.ca/publications/c_family_interview.htm

says:

.... Quiz any C developer about unsigned, and pretty soon you discover
that almost no C developers actually understand what goes on with
unsigned, what unsigned arithmetic is. Things like that made C complex.
The language part of Java is, I think, pretty simple. ...

I do not agree with the decision, but ...

Arne
 
A

Arne Vajhøj

Mark said:
Given that all computations involving bytes are performed on int's,
there isn't much lost here. Lack of unsigned int is a bit more of a
nuisance as you have to go to a larger type (although if you are using a
64 bit machine that may not have performance implications either).

Choosing an int type a size bigger is not always suitable for
a give context and not possible for long.

Arne
 
A

Arne Vajhøj

Eric said:
Speaking for myself, the only place I really regret their
decision is with `byte', which would be far more useful (to me,
anyhow) if its range were 0 through 255.

C# got that one right !

Arne
 
A

Arne Vajhøj

Eric said:
Tom said:
[...]
That's certainly what James Gosling says:
"For me as a language designer, which I don't really count myself as
these days, what "simple" really ended up meaning was could I expect
J. Random Developer to hold the spec in his head. That definition says
that, for instance, Java isn't -- and in fact a lot of these languages
end up with a lot of corner cases, things that nobody really
understands. Quiz any C developer about unsigned, and pretty soon you
discover that almost no C developers actually understand what goes on
with unsigned, what unsigned arithmetic is. Things like that made C
complex." [1]

Brave words for someone who can't even form a grammatically
meaningful sentence in English. Go ahead: Try to parse the second
sentence of this, this, utterance. The only intelligible piece of
it is "Java isn't," something Redmond might desire but is contrary
to fact.

Maybe Gosling is better at Java than English or maybe the one quoting
him did a poor job.
As for the "corner cases, things that nobody really
understands" -- well, I've got a use of generics that NetBeans
flags as an error but javac accepts without complaint. Which
of them "really understands" Java?

I doubt that the new things in Java 1.5 was the true "Gosling spirit".

Arne
 
M

Mike Schilling

Eric said:
Speaking (again) for myself, I find that nearly every time
I use a `byte' I have to write `b & 0xFF' to make it work.

Speaking for myself, I never want to use a byte to do arithemetic; I
want to represent an octet. The fact that it sign-extends when any
operation is done on it, e.g.

byte hi, lo;
short s = (hi << 8) | lo; // No good
short s = ((hi << 8) & 0xFF00) | lo;

is a needless pain.
 
M

Mike Schilling

Eric said:
That last line looks like a thinko; I imagine you meant

short s = (short)((hi << 8) | (lo & 0xFF));

I deliberately got it wrong to show how treacherous signed bytes are.
(Yeah, that's the ticket!)
 
T

Tom Anderson

Tom said:
[...]
That's certainly what James Gosling says:

"For me as a language designer, which I don't really count myself as these
days, what "simple" really ended up meaning was could I expect J. Random
Developer to hold the spec in his head. That definition says that, for
instance, Java isn't -- and in fact a lot of these languages end up with a
lot of corner cases, things that nobody really understands. Quiz any C
developer about unsigned, and pretty soon you discover that almost no C
developers actually understand what goes on with unsigned, what unsigned
arithmetic is. Things like that made C complex." [1]

Brave words for someone who can't even form a grammatically
meaningful sentence in English. Go ahead: Try to parse the second
sentence of this, this, utterance. The only intelligible piece of
it is "Java isn't," something Redmond might desire but is contrary
to fact.

Heh! The text is, i think, a transcript of speech, and speech is generally
like this. Humans don't speak grammatically most of the time - seriously,
try recording yourself having a normal conversation and then transcribing
it. However, such transcripts are usually edited to make them conform more
closely to the standards of written language; that would seem not to have
been done here.
As for the "corner cases, things that nobody really understands" --
well, I've got a use of generics that NetBeans flags as an error but
javac accepts without complaint. Which of them "really understands"
Java?

Dunno. Tell is what the use is (with an SSSCCCEEE, of course!), and we can
pass judgement!

tom
 
E

EJP

Mike said:
I deliberately got it wrong to show how treacherous signed bytes are.

For the historically minded, this all started because the PDP-11 had
signed bytes, so C bytes were signed too. Originally. K&R hastily saw
the error of their ways and added unsigned char. The occasions when you
want -128..127 are really rather rare, and I'm always doing &0xff, or
defining constants as bytes so that switch cases work correctly, and
having to cast to do so, so IMHO signed bytes were a mistake in Java,
but there you go, there weren't too many ...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top