Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Alexandra F
1,201 PointsWhy the size of Java <int> data type has always been fixed versus to, for instance, the <int> size in C or C++?
There's a number of compiled languages, like C or C++; they are compiled to the machine code. Over time the size of integer in compiled code has changed. However, Java size of integer is fixed. Why?
2 Answers
Seth Kroger
56,416 PointsThe trouble with int size in C is precisely that is does vary based on compiler/vendor/processor/etc. Without a fixed size, it's difficult to know if the same code will compile correctly on a different system or a different compiler on the same system if some side effect of int's size affects the code (ie, the number is outside the int's range for its size). It's been a source of subtle errors as long as C has been around. Java eliminates that ambiguity by fixing int's size across the language.
Alexandra F
1,201 PointsThank you Seth for detailed answer. So in other words, is Java more generic? a compiled Java code depends solely on compiler (JVM) which actually sets the fixed size for <int> (and other data types), versus to C where the size of an int is dependent on compiler/processor/platform where the code is run. Is my understanding somewhat correct? :-) Thank you