5

The Java Language Specification 20, Section 4.10. Subtyping states that

The subtypes of a type T are all types U such that T is a supertype of U, and the null type. We write T <: S to indicate that that the subtype relation holds between types T and S.

Does this mean, that the null type is a subtype of all primitive types? And if yes, are there any practical consequences?

I'm writing a library to parse and analyze java code, so should I treat null as a subtype of, say, int? Or does it not matter at all?

user3840170
  • 26,597
  • 4
  • 30
  • 62
Rotstein
  • 87
  • 1
  • 4
  • 1
    No. `null` is a reference type. Primitives cannot have reference types. See answers to [Why can't primitive data types be "null" in Java?](https://stackoverflow.com/questions/11047276/why-cant-primitive-data-types-be-null-in-java). An example: If you have a method call, say signature is `foo (int bar, double bat)`, you cannot call `foo (null, 3.14159)`. There is no place you can use `null` in place of a primitive. – Old Dog Programmer Aug 20 '23 at 17:42
  • @OldDogProgrammer This is a JLS question. The quoted sentences do not restrict T to class and interface types; they're in the section that applies to all types: primitives, objects, and arrays alike. I, too, am curious how to reconcile the quoted bit with the fact that primitives can't be null. – John Kugelman Aug 20 '23 at 17:44
  • @JohnKugelman I don't think so. Go back 9 sections to [4.1. The Kinds of Types and Values](https://docs.oracle.com/javase/specs/jls/se20/html/jls-4.html#jls-4.10), which says there are two kinds of types and then adds a third (special) null type. – Elliott Frisch Aug 20 '23 at 17:48
  • subtype or rahter "supertype"? Anyway, *no* – Antoniossss Aug 20 '23 at 17:50
  • @ElliottFrisch Yes, I see that. Section 4.1 defines what the null type is. What I find notable is that 4.10 doesn't say "The subtypes of a *reference* type T...". It places no restrictions on T, implying that T can be a reference type, primitive type, or null type. – John Kugelman Aug 20 '23 at 17:52
  • I'm sure there's an answer, but I just want to point out that this JLS citation is neither asked about in the [linked question](https://stackoverflow.com/questions/11047276/why-cant-primitive-data-types-be-null-in-java) nor mentioned in any of the answers, so I disagree with marking it a duplicate of such. – John Kugelman Aug 20 '23 at 17:53
  • 4
    I wonder if it's a spec defect. In section 4.10.2 it says, "The direct supertypes of the null type are all reference types other than the null type itself." That is a better match for what all of us believe to be the case. – John Kugelman Aug 20 '23 at 18:01
  • 2
    *"I'm writing a library to parse and analyze java code, so should I treat `null` as a subtype of, say, `int`? Or does it not matter at all?"* This last paragraph of the O/P question makes it a practical question, not just a JLS question. – Old Dog Programmer Aug 20 '23 at 18:48
  • 3
    The term "Subtype" is defined through the "Supertype". Now, the question boils down to showing if it is possible for a primitive type to be a supertype of null? The answer is no, according to 4.10.2: The direct supertypes of the null type are all reference types other than the null type itself. Hence, null can't be a subtype of primitive types either. We have to use the definition of supertype to reason about subtypes, not vice versa. – Turkhan Badalov Aug 22 '23 at 07:55
  • 1
    @TurkhanBadalov That's how it *should* be. However, the subtype definition includes the phrase "and the null type", which as I read it makes the definitions asymmetrical, and inconsistent with the sentence you quoted from 4.10.2. – John Kugelman Aug 22 '23 at 12:50
  • @JohnKugelman, gave it a thought. Perhaps "and null" part indeed makes statements contradict for primitive types. In fact, if we omit it, the conclusion that the null type is a subtype for all reference types can be inferred from 4.10.2 and the definition of the subtype. – Turkhan Badalov Aug 22 '23 at 14:46
  • 1
    What if we say “yes”? I couldn’t find any place in the JLS where this had an impact. It was even hard to find a place where the subtype relationships specified in §4.10.1 have an impact and the only place I eventually found is not affected by this question. – Holger Aug 22 '23 at 15:24

0 Answers0