File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Literal for byte and short Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Literal for byte and short" Watch "Literal for byte and short" New topic

Literal for byte and short

Karsten Wutzke
Ranch Hand

Joined: Jul 20, 2010
Posts: 106


when specifying numbers, you can append an 'f' for floats, 'd' for doubles, 'l' for longs, but I wonder, why isn't there any such thing for byte and short? Because Java then assumes int, you can't make use of autoboxing when a method or constructor has a java.lang.Byte or java.lang.Short in its param list. Instead you have to code something like new Short((short)12345) instead of just 12345s, for example.

Why is that so?


OCJP JavaSE 6 (86%)
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15101

byte and short are not used very much in Java. You'd only use them if you specifically need to store 8-bit or 16-bit values. Normally, you'd just use int as the default type for integral numbers.

Note that there's a special exception; if you declare and initialize a variable, like this:

then you don't need to cast the literal number to byte or short. The compiler will check if the number is in the range for the specified type.

I guess this also has to do with Java's legacy from the C programming language, where there also isn't a special notation for bytes and shorts. (In fact, the type byte doesn't even exist in C).

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46437
Jesper Young wrote: (In fact, the type byte doesn't even exist in C).
They have something very similar in C, however. They call it a char.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

this is my own thinking donot know how much i am correct.
Lets see what experts say.

If we do
Byte b=12; then compiler takes 128 as byte not as int.
try to assign a value 129 to a byte.see you will get compile time error.saying cannot convert from int to byte.
In java implicitly.every primitive(byte,short,int,Long) other than decimal values are treated as integer if within the range of integer
Ranges are defined for example for byte it is -128 to +127.
anything outside the range of byte will be treaded as integer.
and also if we do
short s=-32,768 to 32,767.
then everything outside this range and within the integer range( -2147483648 to 2147483647)will be considered as integer
mentioning long with "L" is mandatory because if we donot mention it compiler takes it
as integer and as long range is more as compare to integer
so it

for float and double(like int above here it is double)
but in the case of float and double
if we donot mention "F" if
float f=1.001f.
then compiler assumes the precision type as double as sees that it is not
within the range float as mentioned so gives compile time error.
so it is mandatory to mention it as "f" after the float value.

even in case of method overloading we call any method
using byte value
byte b=100;
doStuff(100); this will first search for byte value if not found then only widening will happen otherwise not.

SCJP6.0,My blog Ranchers from Delhi
I agree. Here's the link:
subject: Literal for byte and short
It's not a secret anymore!