Why do some object oriented languages let the programmer use primitive data types?
Aren't classes like Integer, Boolean, etc. enough?
Sometimes you need full control of the size a number takes in memory. Or you may want to directly process binary data you read from a file, or copy from video memory if you process a screenshot or grab from some port, network connection, whatever.
In theory you could add more classes that are specialized in processing binary data, but depending on the actual history of a certain language and what exactly its usage intentions were at some time.
Performance may be a reason too in some cases. Especially with highly optimized compiled languages.
There are quite some languages that don't have primitives and are "object only".
Because 90% or more of the time you use primitive objects with primitive semantics anyway, not typical object semantics. The compiler has to carve out special privileges and restrictions for primitive objects, like:
If you're going to make a primitive object that the compiler treats differently than every other kind of object, some language designers feel you may as well provide separate and explicit syntax for primitives. In other words, if an
int and an
Integer have different behavior and are used in different contexts, it makes sense to make them different types.
It makes the language more internally consistent, and makes translation to native instructions much easier. It's also more consistent with the mental model of most programmers. Even in languages where primitives are objects, most programmers don't consider a statement like
int a = 0; to have all the overhead of regular objects, and they would be right.
In other words, primitive objects aren't "just like" other objects, no matter how big of a superficial resemblance. Some languages just don't try to hide the differences from the programmer.
The ultimate reason is performance. There's a modern-day problem where people have gotten used to the very high level abstractions some languages provide and think that that's all there is to computing, unfortunately computer hardware still works like it used to back in the 70s.
This means that, whilst you may like to think that an elegant language should not support primitives, and that everything needs a default base class and every type needs to be a class, the level of abstraction involved would absolutely kill performance of your programs - simply because the computer still operates on the primitive types. Does this mean that your OO apps are slower than low level ones? Why yes - and they are, there's a reason things like device drivers are still written in C and not Ruby or Java (and its not just down to the programmers involved)
For the most part, OO apps don't notice too much performance problems when written as OO apps (even though I have seen some programs go into a massively convoluted mode where there are so many calls between objects the system spends all its time looking up vtables!) simply because the vast majority of the data processing is performed on primitive types. Replace then with objects and you'll see an entirely different performance characteristic. eg, just look at questions like this one where the questioner explains that using a double instead of a Decimal class made his program run "about 15x faster".
First, why not?
Next, in a language like Java, you have a class “Int” or whatever it is called, but it has substantial overhead. You really only want to use it if there is a very good reason to use an object. Try implementing FFT in Java and see if it is “fast” compared to a C implementation.
In Swift, there are five categories of data: Class, struct, enum, tuple, and protocols. Guess what: No objects. And there are value types and reference types. Java objects would be Swift classes except they are nullable, and nullable types in Swift are enums.
In Swift, types like Int or Double are actually structs. The optimiser is good enough to make this zero cost. Being structs allows nicer syntax for some operations. The important thing is that structs are value types. There are primitive types that would only be available to the implementor of the standard library. So in Swift there is no need for anyone else to use primitive types, but other languages are not like that. They allow use of primitive types because they have to.