7
$\begingroup$

Many languages have keywords short and int and long and double as their built-in primitive types. C seems to have started this and other languages such as Java followed. In C one of the reasons why this is the case is because CHAR_BIT is not always 8 so building in and mandating fixed-width types would get complicated. But in Java all are guaranteed to be a certain width as far as I can tell. Though removing ambiguity is an advantage of having the width in the type names, so are there other reasons why I would have: short int long float double instead of int16 int32 int64 float32 or float64?

$\endgroup$

2 Answers 2

9
$\begingroup$

Believe it or not, the original idea in C was that you weren't supposed to worry about the size, and other languages made shortly thereafter followed suit. The idea was "use int for integers, float for decimals, and let the compiler decide the side". Obviously, that hasn't been the reality. Everybody is constantly working around this, most large C programs use typedefs to get precise sizes, and the terminology is largely considered a misfeature.

$\endgroup$
6
$\begingroup$

C's choice to make integer widths depend on the hardware (or, strictly speaking, the compiler's choice ─ but in practice, compilers choose based on the hardware) means these types are "leaky abstractions". They don't save the user from having to think about the lower-level details.

In contrast, Java's primitive types are "safe abstractions" ─ you get the same behaviour regardless of the machine architecture that will end up running your program. But you still need to know the number of bits for each type, because these aren't included in the type names. Probably Java's reason for keeping the names int, long and so on is because the designers were explicitly trying to keep their language familiar to C programmers, in order to encourage adoption. (Java has the same motivation for borrowing other things from C's syntax, including that you can declare array variables like int x[]; instead of int[] x;.)

A downside of type names like uint32 or float64 is that they are longer, and take up more space; this particularly matters for the fundamental primitive types of the language, which users will have to type and read over and over again. Rust's solution here is a good one: the primitive types u32, i32, f32, u64, i64, f64 and so on have explicit bit-widths in their names, while also being short; and if the programmer wants a platform-dependent integer they can use usize or isize. Sure, this means the programmer needs to know that i means integer and f means floating-point number, but that's less to remember than the widths of every type, and it's an abbreviation rather than an arbitrary name like "short" or "long".

$\endgroup$
2
  • $\begingroup$ So in Java int x[]; is valid? $\endgroup$
    – CPlus
    May 20, 2023 at 4:29
  • 2
    $\begingroup$ @user16217248 It sure is! $\endgroup$
    – kaya3
    May 20, 2023 at 4:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .