Is int type in Dart a value type or reference type?

Viewed 2885

The int type in Dart has default value of null. null is an object of type Null class. (as per Dart documentation). Also, in Dart, int derives from class Object. Hence,

int i = 10;
print(i.runtimeType is Object);   // returns true

This makes me believe that int is not a value type like in other languages (such as C#) but a reference type.

If I am correct, then- int i = 10; means i is a reference variable holding the reference to an int object 10.

Is this correct? If not, I would appreciate if a link to the description in the documentation is shared. Till now, I've been unable to find any proper explanation and hence have come to this conclusion myself. Thanks.

4 Answers

Yes, Dart's int type is a "reference type". Dart does not have value types at all, all values are instances of a class, including integers. (At least technically, function values makes their classes very hard to see.)

Integers are immutable and pretends to be canonicalized. If a and b are int values and a == b, then identical(a, b) is guaranteed to be true, so a and b looks like they are the same object, but it's unspecified whether that's because they really are the same object, or because identical just cheats and does == for integers.

That means that you can largely treat int as a "value type". Whether it is copied on assignment or parameter passing, or you are passing a reference to the same object, is impossible to tell apart. The language ensures that, because it allows the implementations to do whatever is more efficient. In practice, some integers are unboxed inside functions, some integers are stored as a value in the reference itself, and some are real objects. (That's also one of the reasons you can't use an Expando with an int).

(In the current Dart language, all types annotations are nullable, meaning that you can assign null to int x;. With the upcoming Null Safety feature, that changes. Types will only be nullable if you write them as such, so int x = 1; would not accept a null value, but int? x; would. And null is an object too, the only instance of the class Null.)

int is a value type in the sense that if you pass int value into a function and change the value of a parameter inside function, it won't affect outer scope.

void add(int inner) {
  inner += 1;
}

int outer = 0;
add(outer);
print(outer); // 0, unchanged

But int is still a class even though its name starts from lowercase letter. There was a huge discussion about its naming and lots of people consider it an inconsistency.

Everything in Dart is an object, int, double or num may look like keywords but they are built-in abstract classes. Since they are abstract classes which means they can not be instantiated, they do not start with an uppercase letter.

try this line, print('2 is instance of "int" class :\t${ 2 is int}');

this will result in true.

And regarding your concern about 'null' : In Dart, like JavaScript and Python, everything that a variable can hold is an object including null. Every object including null is an instance of some class and all these classes inherit from Object class.

your can refer this link for more info. Hope this helps.

Yes int is a class (it even extends num) and therefor has common methods to use on. But one the other side it is different than "normal" classes since you don't have to use a constructor like

int i = int(3);

See int's class documenation https://api.dart.dev/stable/2.8.4/dart-core/int-class.html

Related