3

In Java, there have always existed primitive types and references, but not value types.

One of the goals of Project Valhalla is to explore and incubate feature candidates such as value types.

I would like to know if null can be assigned to a value type variable, or if a value type variable can be compared to null.

If not, I would also like to know why.

fps
  • 33,623
  • 8
  • 55
  • 110
  • 1
    `null` is a value of *reference* type. Inasmuch as the whole point of JEP-169 value types is that they are *not* reference types, no, `null` cannot be assigned as the value of such a type. At least, not without specifically attributing special-case significance to such an assignment. – John Bollinger Feb 08 '18 at 21:20
  • @JohnBollinger Intuitively, `null` means *nothing*, so a value type representing *nothing* is conceivable. I know that the traditional meaning of `null` is a reference that points to nowhere. Do you know if the sentence *`null` is a value of reference type* (or a similar one) is somewhere in the JLS? – fps Feb 08 '18 at 21:30
  • 2
    Yes, Federico, albeit not in exactly the words I used: "The null type has one value, the null reference, represented by the *null literal* `null`" (JLS9, 3.10.7; formatted as in the original). – John Bollinger Feb 08 '18 at 21:34
  • Hey @JohnBollinger , thank you very much for that reference – fps Feb 08 '18 at 21:34
  • 1
    See also comments about the null type and null reference in JLS9 section 4.1. – John Bollinger Feb 08 '18 at 21:37
  • 3
    Afaik this is the latest draft for the prototype: http://mail.openjdk.java.net/pipermail/valhalla-dev/2017-December/003631.html and it _does_ have "not nullable" for value types. – Jorn Vernee Feb 08 '18 at 21:43
  • @JornVernee Thanks for that brainstorming transcription – fps Feb 08 '18 at 22:00
  • 1
    what a good question! @JornVernee that very much sounds like an answer, I doubt that this part will be changed... – Eugene Feb 09 '18 at 11:48
  • @Eugene It's not that good... I remember when I used to code in C (in a glorious past, ages ago), and assigning `NULL` to a `struct` variable didn't make any sense. Why would it mean anything different now? Anyways, I think that my real intention with this question is to know if a value type can represent an empty/zero/void/nothing value, and how to implement that behavior, i.e. a number could be `0`, a string `""`, a location could be Greenwich, etc – fps Feb 09 '18 at 13:40
  • 2
    @Eugene In C, I could do `memset(myStructVar, 0, size)` and that filled my value type with all its bits set to `0`. In modern C there are other ways to do the same, i.e. `struct MyStructType myStructVar = {0};`. I believe this is what I want to know, i.e. if we'll have a general way to initialize value types to their default values in Java... Maybe I should post another question? – fps Feb 09 '18 at 14:03
  • 1
    @FedericoPeraltaSchaffner I'll ask my wife about this code, she is waaay better in C than me. But that is my thought now too, what is a default value for a value type? `Integer i;` for example ? – Eugene Feb 09 '18 at 14:07
  • @Eugene Here's the question, properly written this time: [https://stackoverflow.com/q/48709375/1876620](https://stackoverflow.com/q/48709375/1876620) – fps Feb 09 '18 at 15:47

2 Answers2

4

From here: https://wiki.openjdk.java.net/display/valhalla/Minimal+Value+Types

Value types are "immutable, identity-agnostic, non-nullable, non-synchronizable, final"

So no, it can't be null

John
  • 2,575
  • 1
  • 17
  • 29
2

Value type variable is value, null is absense of any value. So null and value types are incompatible by definition.

Anton Tupy
  • 951
  • 5
  • 16
  • Yes, you are absolutely correct. I think I mixed up the terms in my question (indeed it's not a very good question). My intention was to ask about value types that represent nothing/zero/empty, i.e. the `0` number, the empty string, the person with no name, etc. In C, I remember I could do a `memset(myStruct, 0, size)`... – fps Feb 09 '18 at 13:53