Hello group,
what is a value type in C++ or Java or C#? Is it a class or an object?
I can see one can call a method(For instance ToString()) on an Int16
and so on;So is it an object?At the same time we use Int16 to
instantiate a variable Int16 a =new Int16();So is it a class?
can clarify this for me?Class or Object
Thanks
B.
I've been trying to understand this topic for a while, and the
following is my current understanding of the topic. I hope that it's
accurate.
A type is a named set of values, and a value is a member of such a
set. A variable is a named area of memory which can hold values.
Types have operators that can be applied to values and variables of
the type.
Classes and objects are somewhat more complex. A specific class may
define a value type, if the objects it defines are immutable and
comparable for equality.
A class may also define a composite variable. This occurs when the
attributes of an object of the class can be modified. In this case,
the type defined by the class is not defined only by its attributes,
but also by those methods which can modify the attributes. Objects of
the class aren't values of the type, but variables which contain such
values.
'Entity classes' are a combination of value types and composite
variables. An entity class defines a set of immutable attributes,
called the identity, and a set of modifiable attributes which I'll
call properties. In this case, there are two types being defined (a
value type for the identity, and a composite variable type for the
properties). Objects of the class are both values of the first type
and variables for values of the second. It isn't clear how equality
of such objects should be defined. There are other problems
associated with entity classes, but those aren't directly related to
the topic of types and values.
There is one more set of types associated with classes. Every class
has one or more interfaces. An interface is a type, and that makes
objects of classes that implement a specific interface, values of that
type. Interfaces are used to create systems of substitutable actors.
Finally, every class is an object itself, which is defined by its
static members.
To get back to (Java or C#) Int16, when you call a method on it,
you're calling one of the methods of the static object. When you
create a new Int16 object, you're creating a new object from the
class. And Int16 just so happens to be defined as a value type, which
means Int16 objects are also values of the type of that class.
C++ int doesn't have methods, since it's a type, not a class or
object. You can, however, declare variables of int, assign int values
to them, and derive new int values via the int operators.