• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 344
  • Last Modified:

size of an integer

Hi,

I wonder if there is a function that I can know the size of an integer value??  Just like "sizeof" function in C++???
Any idea??

Help~~~~



Xenia
0
xenia27
Asked:
xenia27
3 Solutions
 
brettmjohnsonCommented:
The size of integers varies in dialects of C to correspond to the native word size of
the underlying hardware.  Java is a "Virtual Machine", whose word size is always
32-bits, no matter what the underlying hardware.  Since, by definition, the size of
and int in Java will always be 32 bits, there is no need for sizeof().
0
 
xenia27Author Commented:
so in Java, I don't have any function like "sizeof" because the sizes of any kinda variables are fixed???
0
 
xenia27Author Commented:
so how can I know how many bits I actually used??
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
xenia27Author Commented:
OK...let me confirm what I understand from all the post and the article...so the size of all variables is fixed...and that means all bits will be used when you declare a variable and store something into the variable...even though we don't need to use all bits???
0
 
objectsCommented:
that is correct, so if you wanted to converve space then you would pick a data type that is just big enough.
0
 
thomas908Commented:
>>so the size of all variables is fixed.

Except for boolean, which is platform dependent
0
 
sciuriwareCommented:
xenia27, all of today's computers are byte oriented although they transfer their data in words (32, 48, 60, 64 or 128 bits)

In the good old days all mainframes were word oriented: if a CDC was 60 bits, you had to hussle in assembler
to pack your 6-bits texts or your 8-bits ASCII into words (because memory was few then).

Nowadays memory is plenty; sharing an integer between 2 variables that only need a part
of it (say 3 bits resp. 5 bits) is a bit strange with all those Mb or Gb around.
But if you must, you can write a class in which fields are shared between getters and setters.

(the result will be slower and BIGGER than straight code).

You can do it, if you must ........................

..... did you consider if you ever in your life use the last track on your hard disk?
;JOOP!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now