• Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubt on Java Data Types(int,short,byte)  RSS feed

 
Greenhorn
Posts: 6
Java Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Respected Sir,
i have small doubt on core java(Data types).



Actually we define variable date type as byte or short ,but after compilation it stored as int type in memory.

we are using theses byte,short for reduce the memory size,but it stored as int type,then it take more memory for storing,so what is the use of VARIABLE DECLARING AS SHORT OR INT..?
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sanyasirao nali wrote:after compilation it stored as int type in memory.

Are you sure of that?

I'm not positive, but I thought the spec says that the JVM can be implemented however you want, as long as it meets certain criteria. In other words, if I were writing an implementation, i could store a short in 87 bytes if I wanted, as long at the correct behavior happens...


we are using theses byte,short for reduce the memory

Why are you doing that? Computers these days have gigabytes of memory - even phones comes with several gig standard, and most allow a SD card to be inserted to give even more...so really, why are you worried about saving a few bytes here and there?

Your time would generally be better spend writing clean, easy to maintain code, rather than worrying about saving a byte here or there.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sanyasirao nali wrote:Actually we define variable date type as byte or short ,but after compilation it stored as int type in memory.

I'm not quite sure where you got that from.

As I recall, a byte is a byte (8 bits), but I can't remember seeing anything about how much memory it takes; since that's a machine (or platform) restriction (word-aligned memory springs to mind). And this is what you have to remember about Java - it is platform-transparent. It's the JVM that decides how and where thing are stored, not the language (except as defined by the JLS)

we are using theses byte,short for reduce the memory size,but it stored as int type,then it take more memory for storing,so what is the use of VARIABLE DECLARING AS SHORT OR INT..?

Well then your thinking is wrong. You don't store things as bytes to save space, you store them as bytes because it defines the only possible value set (0-255, or -128-+127). If you can point me to something in the JLS that says that a byte is only allowed to occupy 8 bits of memory I'll eat my hat.

It's also worth noting that ALL integer operations (unless they're long) work on int's. Maybe that's your confusion.

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!