• Post Reply Bookmark Topic Watch Topic
  • New Topic

using byte vs int  RSS feed

 
Marie Day
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there an advantage in using byte instead of int beyond the space savings? In my program, I'll never need close to the max value of a byte, let alone int, so it seems like a waste to make my primitives ints.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No.

All integer arithmetic is done on ints (or longs if any of the operands is a long), so there is no advantage in using bytes. Bytes are not intended for normal numbers, but for dividing up things to send across networks, so you can send an object as a byte[] to somebody elsewhere. For ordinary use as fields, forget that you are saving 24 bits of memory and use ints. Remember that 123 is not a byte literal but an int, so it matches the datatype already.
A 24 bit saving when you may have hundreds of megabytes of memory to play with for simple Java® applications is less than chickenfeed!

And welcome to the Ranch
 
Marie Day
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, thanks. And, thank you.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!