2

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?

Lucky
  • 16,787
  • 19
  • 117
  • 151
Jay
  • 27
  • 4

4 Answers4

4

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.

Tim B
  • 40,716
  • 16
  • 83
  • 128
  • Well it's not really a fundamental law (and there have been processors with 16 bit integers) but in practice you're absolutely correct. Actually using bytes can worsen performance even on actual hardware out there. – Voo Dec 27 '13 at 08:42
  • I first started programming on 8 bit computers, I'm not aware of any serious computer using less than 32 for a very long time now though. – Tim B Dec 27 '13 at 09:02
  • There are lots of embedded devices running MIPS and co. That's actually one of the originally intended places for java and I've heard people programming microcontrollers in java to some degree. (Sounds awkward) – Voo Dec 27 '13 at 14:22
0

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.

user207421
  • 305,947
  • 44
  • 307
  • 483
0

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.

Andrey Chaschev
  • 16,160
  • 5
  • 51
  • 68
-1

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.

Lucky
  • 16,787
  • 19
  • 117
  • 151
android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • 3
    Can you share some information about what you have specified,please. – Aman Arora Dec 26 '13 at 09:32
  • check this post : http://stackoverflow.com/questions/229886/size-of-a-byte-in-memory-java – android developer Dec 26 '13 at 09:35
  • Thanks for sharing ! But the statement "byte and int will always be same internally" doesn't hold true always. May vary with CPU architecture. – Aman Arora Dec 26 '13 at 09:40
  • i am agree with you.int is default type for developers but, being a java developer we won't drop our memory efficiency...(in my point of view) – Jay Dec 26 '13 at 10:01
  • memory efficiency is more relevant about large object, like images. here it really doesn't matter no matter how you look at it - both the types take the same amount of memory. – android developer Dec 26 '13 at 11:03
  • Just because there's no bytecode for load byte doesn't mean there can't be byte fields. The compiler knows the actual type of the field after all. Since object sizes are always aligned on 8 byte boundaries though it probably really doesn't make a difference here. – Voo Dec 26 '13 at 14:19
  • @Voo i don't understand . please explain . – android developer Dec 26 '13 at 15:31
  • I assumed your reference was about the post that talked about "there's no load byte opcode", because the others are only using arrays which are a different beast, but I may be mistaken. Basically if you have `byte a;` in your class it will still only take up 1 byte, *but* objects are always aligned to 8 byte (independent of 32/64bit btw), so most of the time you won't see a difference between a byte and an int. But if you had for example 8 bytes those would use up 8 byte space and not 32 byte. – Voo Dec 26 '13 at 19:12
  • @Voo no , you are either mistaken or confused. when you declare a single byte, it takes a whole 32 bits (not bytes) which is 4 bytes. on some VMs , it will take less on some cases (for example an array of bytes). in any case, java developers don't use a byte type in order to save space, but because it's the most logical thing to do (for example when messing around with raw data). Java is more about logical high level than low level. – android developer Dec 26 '13 at 21:03
  • If I'm mistaken I assume I should rewrite that part of the graal compiler then - better go and tell the guys at oracle too ;) But it's very easy to disprove your theory, see [here](http://paste.pound-python.org/show/UzCqDJ0ynqBdDfqUfY0i/). If bytes were really stored as integers, the minimal possible size of an instance of that class would be 8*4=32 byte (ignoring the 2 words header overhead, so really best case). If you run the code you'll see that it's quite a bit closer to the 24 byte you'd expect (8 byte + 16byte header). Obviously this is all JVM specific,I'm talking about HotSpot here. – Voo Dec 27 '13 at 01:55
  • Also note that your assumption that "byte is usually used for bitwise operations" has some problems too - it's impossible in Java to do operations on anything smaller than ints - that includes bit wise operations. Try `byte x = 5; x = x & 0x1` for a demonstration (actually I've written a whole lot of byte manipulation the last few weeks due to said compiler and it's pretty much all on ints). Bytes are generally used for space constraints or because the data is logically bytes (then generally an array though). Actually one common optimzation is to pack bytes into ints. – Voo Dec 27 '13 at 02:01
  • when you perform bitwise operations on bytes, either use &0xFF or use an int, because they are upcast to an int. see here: http://henkelmann.eu/2011/02/24/a_curse_on_java_bitwise_operators . anyway, i've removed the part talking about bitwise operations. it's easier to use int anyway... – android developer Dec 27 '13 at 08:11
  • 1
    This answer is simply incorrect. An int is defined as four bytes. – user207421 Jun 06 '15 at 08:06
  • @EJP I wrote "behind the scenes". Of course int will work as 4 bytes and byte will work as 1 byte, just as the docs say. behind the scenes, byte is usually just an int. The VM might have other implementations and optimizations though. – android developer Jun 06 '15 at 11:11