Primitive Types
Primitive Types
Primitive types are the basic building blocks of programming, and are present in almost all languages. Primitives cannot have methods, and cannot be inherited from or redefined. They can be directly manipulated with the math operators such as +, - and *.
For much of your time programming you will not care much about the size of the primitives you use. Indeed in some languages such as PHP there is no concept of assigning a type to a variable, and millions of lines of PHP code are written and many people love the languages.
Java was designed to allow large scale software engineering however and being able to control data types gives the programmer a fine control over resource usage. Thus for many (most?) simple counting operations the data type called int will do just fine, as it will store numbers up to 2 billion odd and if you are counting the numbers of windows your program has open or the number of CD's in your collection an int will probably do just fine.
However of Bill Gates, is calculating his net worth he might need to pay more attention to his choice of data type (OK so he wouldn't admit in public that he was using Java, but he is a smart technologist under all that marketing).
Unlike C/C++ the size of Java primitives are fixed no matter what platform it is used on. In C/C++ you can often see code that uses the sizeof operator to try to ensure that code is platform neutral by determining the size of a data type on the current platform. Because this is not mandatory the move from one platform to another can often break C/C++ code in a subtle manner.
Primitive data types can broadly be divided into integral types and floating point types. Floating point types can store vast, huge, enormous numbers but they bring with them a certain imprecision. For many purposes one of the integral types such as int or long will do just nicely.
The size of Primitives |
||
Name |
Size |
Range |
byte |
8 bit |
-27 to 27-1 |
short |
16 bit |
-215 to 215-1 |
int |
32 bit |
-231 to 231-1 |
long |
64 bit |
-263 to 2 63-1 |