So java has a long type suffix for literals: (123L), a double type suffix (43.21D), a floating point suffix (1.234F). So ... why no byte type suffix? For example, when writing some testing code you MUST cast all your bytes when they are used as function parameters.
ByteBuffer b = ByteBuffer.allocate(100);
b.put((byte)3); // super annoying
b.put(3b); // if only
It is clear that using B or b would not work since it would conflict with the ability to specify a byte in hexadecimal or octal (a critical language feature). But some other letter, like Z z? or Y y (for bYte)?
The Java byte keyword is a primitive data type. It is used to declare variables. It can also be used with methods to return byte value. It can hold an 8-bit signed two's complement integer.
The byte data type in Java is a signed integer based on the two's complement 8-bit mechanism. It is different from the int data type that uses 4 bytes (i.e., 32-bit to store a number). The values that can be stored in a single byte are -128 to 127. byte data types are primitive.
(long) 1 is a constant expression (because the 1 is directly known) and hence, by the rules of the Java Language Specification (JLS), will be a long after compilation already. However, in my experience, it is far more common that people use the long literal and write 1L for readability.
This does not really answer the question of why, but for what it's worth, there was a proposal put forward in March of 2009 for just this with the Y byte suffix for bytes and S for shorts: Byte/short suffix proposal
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With