Why in Thread class " public static final int MAX_PRIORITY" declared as int?

368 views Asked by At

I have a doubt about why java developers has declared as

public static final int MAX_PRIORITY
public static final int MIN_PRIORITY
public static final int NORMAL_PRIORITY

instead of declaring public static final byte MAX_PRIORITY. Because for these variables highest value is 10 only. So I think byte is sufficient int range is higher than byte.
Any specific reason behind this? Could someone please explain this to me?

4

There are 4 answers

14
android developer On

Behind the scenes, the java int(on most cases) has the same size as of a byte (and short type), so it doesn't really matter.

byte is used when it's logical to use it(when the data itself is represented in bytes, like raw data), or in order to save space when you have an array of small numbers.

int is the default type developers use, unless they have a good reason to use something else.

3
Tim B On

Bytes use slightly less space to store but are no faster to use than an integer - since fundamentally all 32 bit processors work in integers anyway.

There is no real reason to use byte over integer unless you are storing an array or similar of them where they can then be packed into a smaller space.

0
user207421 On

So that if in future it was desired to introduce another value that wasn't in the range of a byte, the implementors wouldn't find themselves hamstrung by a prior, inappropriate decision such as the one you appear to be favouring.

0
Andrey Chaschev On

My guess is that it was a code convention in the project, because in this case due to memory alignment making it byte would save 8 bytes on x64 architecture. The rationale behind this could be that in future versions this field could be used to store other thread state information.