3 ms·
Java doesn't have unsigned as primitive types, because James Gosling did a series of interviews at Sun among "expert" C devs, and all got the C language rules f
by pjmlp 7mo ago
Java doesn't have unsigned as primitive types, because James Gosling did a series of interviews at Sun among "expert" C devs, and all got the C language rules for unsigned arithmetic wrong.
Yes I miss them in Java as primitives, however there are utility methods for unsigned arithmetic, that get it right.
- layer8 7mo agoJava has char as an unsigned 16-bit integer type. They should have made byte unsigned as well.
- pjmlp 6mo agoUsually you don't do arithmetic with char in Java, this isn't C culture of anything goes.
- uecker 6mo agoIt is not even possible to do arithmetic on char in C.
- pjmlp 6mo ago#include <stdio.h> unsigned int pack_rgb(unsigned char r, unsigned char g, unsigned char b) { return (r << 16) | (g << 8) | b; } unsigned int pack_rgb_arith(unsigned char r, unsigned char g, unsigned char b) { return (r * 65536) + (g * 256) + b; } int main(void) { printf("The color value of (246, 176, 223) is %d\n", pack_rgb(246, 176, 223)); printf("The color value of (246, 176, 223) is %d\n", pack_rgb_arith(246, 176, 223)); } Compiler Explorer link, https://godbolt.org/z/3jExdaTT9 https://godbolt.org/z/3jExdaTT9 I would expect a better comment from someone working on the standard.
- uecker 6mo agoYou should know that the type is promoted to int first, which is also what makes your example work. This is what happens when you perform the computation using an non-promoting 8 bit unsigned type: https://godbolt.org/z/fxxva4nWq https://godbolt.org/z/fxxva4nWq
- 3836293648 6mo agoDon't they get promoted to short?
- layer8 6mo agoThere’s nothing preventing you from doing so.
- deleted 6mo ago[deleted]
- pjmlp 6mo agoI did not say otherwise, I said the culture is not the same towards safety.
- jstimpfle 6mo agoThe way he conducted those interviews, and the conclusions he drew from them, may have been flawed. Because the situation now is that C has unsigned types and Java mostly has not. And despite all pitfalls especially around mixing signed and unsigned in C, unsigned types are very useful, I'd in fact say that for low-level programming they are essential.
- pjmlp 6mo agoDoesn't seem to affect the extent Java is used across the industry, including many workloads that in the last century companies would use C instead. Books like Yourdon Structured Method were mainly targeted to business C back in the day.
- __s 6mo agoHe could've removed implicit signed conversion