In Java for example there is the primitive data type "int" which represents a 32 Bit value and there is "Integer" which is just a class with a single "int" property (and some methods of course). That means that a Java "Integer" class still uses primitives behind the scenes. And that's the reason Java is not a pure object oriented programming language.
Where could a value be stored if there where no primitives? For example I imagine this pseudo class:
class Integer
{
private Integer i = 12;
public Integer getInteger
{
return this.Integer;
}
}
This would be recursive.
How can a programming language be implemented without primitives?
I appreciate any help solving my confusion.
- [Instructor] In object-oriented programming, everything is an object. In fact, we have been working with objects all this time. You might just not have realized it. Let's take a simple integer, int my value equals five.
In Java, everything extends into an Object class. It means the coding is mostly wrapped in Java objects. The Java language assumes that you want to do only object-oriented programming. You cannot code anything in Java without declaring classes and objects.
Everything Is an Object In object-oriented programming languages like Python, an object is an entity that contains data along with associated metadata and/or functionality. In Python everything is an object, which means every entity has some metadata (called attributes) and associated functionality (called methods).
C++ is an object-oriented programming language. Everything in C++ is associated with classes and objects, along with its attributes and methods. For example: in real life, a car is an object.
Behind the scene always will be primitives because it just a bits in memory. But some languages hide primitives that You can work only with objects. Java allows You to work both with objects and primitives.
If you mean by primitives
value types, then yes you can live without them as a user and use Integer
instead of int
and pay for the overhead of heap allocation and GC
. But this doesn't come for free and you have to pay the cost. Primitives like 32-bit/64-bit integers and IEEE-754 floating points
will always be faster because there is a hardware support for them.
From a compiler writer point of view you have to use what the machine supports to make things work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With