• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question about primitive types ?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I just had the craziest dream. This tiny ad was in it.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic