Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can everything be an object? [closed]

Tags:

java

oop

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.

like image 589
user3585773 Avatar asked Nov 12 '15 14:11

user3585773


People also ask

Can everything be an object?

- [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.

Why is everything an object in Java?

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.

Why is everything an object in Python?

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).

Is everything an object in C++?

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.


2 Answers

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.

like image 134
VDanyliuk Avatar answered Sep 27 '22 17:09

VDanyliuk


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.

like image 44
Sleiman Jneidi Avatar answered Sep 27 '22 17:09

Sleiman Jneidi