Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does java have no byte type suffix? [closed]

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)?

like image 307
Justin Avatar asked Oct 30 '10 15:10

Justin


People also ask

What is byte literal in Java?

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.

Does Java have byte?

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.

What is 1L in Java long?

(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.


1 Answers

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

like image 165
Michael Goldshteyn Avatar answered Sep 17 '22 19:09

Michael Goldshteyn