Solved

size of an integer

Posted on 2004-09-19
8
334 Views
Last Modified: 2010-05-18
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
Comment
Question by:xenia27
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 23

Accepted Solution

by:
brettmjohnson earned 200 total points
ID: 12098843
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
 

Author Comment

by:xenia27
ID: 12098868
so in Java, I don't have any function like "sizeof" because the sizes of any kinda variables are fixed???
0
 

Author Comment

by:xenia27
ID: 12098899
so how can I know how many bits I actually used??
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 92

Assisted Solution

by:objects
objects earned 100 total points
ID: 12099067
0
 

Author Comment

by:xenia27
ID: 12099128
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
 
LVL 92

Expert Comment

by:objects
ID: 12099147
that is correct, so if you wanted to converve space then you would pick a data type that is just big enough.
0
 
LVL 8

Expert Comment

by:thomas908
ID: 12099206
>>so the size of all variables is fixed.

Except for boolean, which is platform dependent
0
 
LVL 24

Assisted Solution

by:sciuriware
sciuriware earned 100 total points
ID: 12099467
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

Featured Post

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question