Java is an Object Oriented programming language. In this series of posts, we’ll explore different aspects of it: data types, target machine(s), object model, and other details of interest.
Sadly, even though I’d planned to make this in a certain order, and due to my goal of writing a daily post, I’ll not start with the story of the language, but with the primitive data types.
The primitive data types of a language are those which aren’t composed of any other data types. If you’ve never worked with a low level language (and, of course, if you’re new to programming) you may be unaware of certain distinguishing features of the data types: size, operations permitted, the way they’re interpreted, and relation between it and the other types.
In Java, we have 8 primitive data types, which can be roughly divided in categories:
- byte, which is 8-bits in size, signed (two’s complement negative); it’s an integer numeric type at its core.
- short, which is just like byte, except 16-bits in size
- int, same old, but 32-bits
- long, you guessed it, but 64-bits
Floating Point types (which are imprecise and comply with the IEEE 754 floating point spec; wikipedia explanation here)
- float, which is 32-bits
- double, which is 64 bits
Units of Data
- boolean, which is true or false and with undefined size.
- char, which is 16-bits and represents an Unicode character.
Those types are, along with the Object system, the backbone of Java and its architecture.
Speaking about the object system, the String object is… special. It can be created with quotes (“This is a String”), thus not requiring the paraphernalia that object instantiation needs in Java. I’ll cover the String in a future article.
There’s something to notice: numeric data can be interpreted as another type (int as byte, long as short, float as int…) if going from a smaller type to a larger one, this can happen implicitly, as well as when going from integer types to floating point types.
Going in the other direction implies that data can be lost – it may not fit! So the transformation must be explicit, to acknowledge that we know what’s going on.
We’ll talk more about this and other subjects soon.