• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question about primitive types ?  RSS feed

 
Arbnor Qeku
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If i am writing a program which needs 55 numbers , Is it better to use byte or int ?

I think byte is more better because it saves memory because bytes use only 8 bit and int 32 bits and by using the byte in app will work more faster .... I want to know if am i right ?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bytes won't be any faster, and they probably won't even save any memory. Even though the Java language treats them as single bytes, each one will probably be stored in a single word on in memory--32 or 64 bits.

And even if there was a difference, you wouldn't choose byte over int for something that small, and when there are only 55 of them. Even 55 million ints is easily manageable by a phone these days. This is not 1970. We don't need to micro-optimize to save every precious byte and CPU cycle.

You choose byte or int for design reasons, not performance reasons. If you want to represent a number and do numerical stuff with it--add, subtract, count, etc.--and that number will be an integer, and it will be in the range of roughly +/- 2 billion, then you use an int. If you want to represent an arbitrary chunk of data that is to be interpreted by some software (your app or another) as on part of a larger whole, such as a piece of an mp3, or part of a Word doc or a picutre--then you use a byte.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!