I correctly understand that this is something like an implicit inheritance, let's say we take C #, all classes in it are implicitly inherited from the Object class, and the int type is the Int16, Int32, Int64 class, that is, let's say when declaring the variable int x; , we create a certain class "x" which implicitly inherits the structure of the class Int, its methods, properties, etc., and during initialization the object itself is created, or for example, Person p = new Person(); in the left part, we declare our class variable “p” which obviously inherits from the Person class, and in the right part we call the constructor and create the object itself. Correct me if you do not understand correctly.
1 answer
You think not quite right.
In languages like Java or C #, you have value types (in Java, these are primitive types, in C # structures) and reference types (classes).
So, the announcement
int x; simply reserves a place in memory where an instance of int (that is, 4 bytes) can be stored. In this case, the name x refers to this place.
But the code x = 1 writes the value 1 to this most reserved place.
For reference types, this works a little differently. Announcement
Person p; it does not reserve a place for the object itself, but for a link to the object (if you are familiar with C, this is something like a pointer). And the code
p = new Person(); creates this object, and in the reserved place writes a link to the created object.
- agree int x; it reserves memory space, I mean that when an int instance is created: int x = 1; then this instance "inherits" methods of the structure Int (in C #), such as Equals (), GetTypeCode (), ToString (), etc. which are described in the int structure. The Int structure in turn "inherits" methods of the abstract class ValueType, which in turn inherits the methods from the base Object class. Those. it turns out by typing an object, we define its functionality, behavior, allowed operations on it that are defined in the base Object class and inherited from it, or redefined + their methods. - Ramil
- An example from C #: the Equals (object) method is defined in the base Object class, then it is in the base value type class abstract class ValueType and finally in the Int64 structure. Or am I confused? - Ramil
- These conclusions came from the analysis of the mscorlib library in Visual Strudio, in particular, mscorlib / System / Int64 / Basic Types / ValueType / Object - Ramil
- @Ramil: Well, uhhhh. we cannot "typify" an object. We create an object of the right type at once, after which the type does not change. - VladD
- @Ramil: Well, yes, the
Equalsmethod is inObject, and is overridden byValueType. But it seems to be irrelevant to the question? If an object is of typeint, then of course it has all the methods of typeint, that goes without saying. - VladD
тип int является классом— no, it is primitive. Primitives do not have methods, mathematical operators are applicable to them. Primitives do not have a class, although wrapper classes have been added to Java 7. - Flippyмы создаем некий класс х- rather, a variable with the name X. And not a class, but a primitive - Flippy