This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Literal for byte and short

 
Karsten Wutzke
Ranch Hand
Posts: 106
Hibernate MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

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?

Karsten
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15284
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
like
byte b=100;
doStuff(100); this will first search for byte value if not found then only widening will happen otherwise not.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic