From Wikipedia, we can learn: "Even among languages that have this distinction, the exact properties of value and reference types vary from language to language. From my experience in all languages, the difference is clear, namely in the case of reference types we must deal with two values at run time, i.e. reference and the pointed value. In the case of value types, we have always just one value. As a result, we can define a common reference value "null" that can be used to point out that there is no pointed value at all. For value types, it is not possible to have a common value used for this purpose.
Can you provide me an example where this statement doesn't make sense or is incorrect? In other words, I am looking for an example that makes the statement on Wikipedia true.

Mariusz PostolPosted Dec 18, 2024, 9:42 AM
Thanks for your contribution all of you.
I agree—the problem lies in the semantics of many programming languages. I suspect that programming languages often obscure boxing and unboxing operations. This issue becomes evident in scenarios such as parameter passing to methods and when independent references point to a common value. While the statement on Wikipedia is correct from a semantics perspective, my statement is also correct from an implementation and common-sense viewpoint, albeit - I agree - simplified. This simplification can be seen in the context of parameter passing for nullable value types and independent references point to a common value - any more.
Could you confirm the differences in behavior between reference types and nullable value types from the "parameter passing" perspective?
Could you confirm the differences in behavior between reference types and nullable value types from the "independent references point to a common value" perspective?
Additionally, do you think compilers hide boxing operations and apply special treatments for nullable value types after boxing?
What impact does it have on portability and solution robustness if we must be aware of implementation?
Naveen KumarPosted Dec 18, 2024, 4:11 AM
The statement holds true generally but can break down when considering nullable value types in languages like C#.
For example:
Here,
nullableValueis a value type, yet it can hold a specialnullvalue due to being a nullable type (int?). This contradicts the idea that value types cannot have a "common reference value" likenull. However, C# achieves this through boxing and additional metadata.This example aligns with the Wikipedia statement since nullable value types internally mimic reference-like behavior while being fundamentally value types.
Jaish MathewsPosted Dec 18, 2024, 3:31 AM
This statement from Wikipedia generally describes the difference between value and reference types across many programming languages. However, it may not universally apply when considering certain programming languages or paradigms that blur the line between these concepts. To identify cases where the statement doesn't make sense or is incorrect, let's analyze its key claims and find examples:
Key Claims
nullto indicate the absence of a pointed value.nullto indicate absence.Examples of Contradictions or Exceptions
Example 1: C# Nullable Value Types
In C#, value types such as
int,float, orstructcan be made nullable using theNullableor shorthandT?. This allows them to have a value that indicates "no value" (similar tonullfor reference types).Here,
nullableIntis a value type, but it can take on a special value (null) to indicate the absence of a value, which violates the claim that value types cannot use a common value for this purpose.Example 2: Python’s Object Model
In Python, everything is an object, including what are traditionally considered value types (like integers or floats). Integers, for example, are immutable objects, and variables merely hold references to these objects.
Python’s
Noneacts likenulland can be assigned to any variable, including those that would be considered value types in other languages. This contradicts the idea that value types cannot have a common "null-like" value.Example 3: JavaScript
JavaScript has primitive types (value types) such as
number,boolean, andstring. However, JavaScript allows a primitive type to be assignednull.This contradicts the claim that value types cannot have a "null" equivalent.
When the Statement Holds True
The statement makes sense and holds true in languages like C++ (without nullable constructs) or in languages with a strict distinction between value and reference types, such as Rust.
Example: C++
In C++, value types such as
intorfloatcannot have a "null-like" value by default. If you try to simulate a "null" for value types, you'll need to use a workaround likestd::optional.Here,
std::optionalexplicitly adds an optional state, showing that value types don’t inherently support a "null-like" concept.Summary
The Wikipedia statement holds true in languages like C++ where value types and reference types have strict distinctions. It becomes less accurate in languages like C#, Python, or JavaScript, which allow value types to have "null-like" states or treat all variables as references under the hood.