6

It is not System.Object or System.sObject, because we can pass null to methods which require System.String as a parameter, and cannot pass System.Object there.

Following code demonstrated difference between System.Object and th type of the null literal:

'abc'.contains(null); // compiler does not complain
 boolean b;
'abc'.contains(b?null:null); // compiler does not complain
 Object o = b?null:null;
'abc'.contains(o); // compiler error

So the question is, what type null literal and expression b?null:null have? Is there a standard library function like typOf, so that I could call typOf(null)?

5
  • 1
    Can you provide a little more context, maybe share some of the offending code, it'll help troubleshoot but it sounds like you are sending a null argument to a method.
    – Jenny B
    Jun 3, 2016 at 15:15
  • I sort of get what you are asking.so null is just a blank reference i guess which does not have memory allocated.The type we say is of reference and not of the null value.
    – Mr.Frodo
    Jun 3, 2016 at 15:35
  • 1
    @CharlesKoppelman Already linked to that in my answer. :)
    – Adrian Larson
    Jun 3, 2016 at 16:27
  • Are you looking for more detail than is included in the existing answers? Can you edit your post to specify what is missing if so?
    – Adrian Larson
    Jul 1, 2016 at 20:44

2 Answers 2

4

I think what it comes down to is that the compiler doesn't handle the typing for you with an Object the way that it does with NULL.

It would be possible for the language designers to build this functionality, but would be extremely complex since you can use the Object type to create variables and assign them values, but you can't with the NULL type.

The literal NULL always has the same value and this value is applicable to all types. An Object can have many different values which are mostly not applicable to other types.


As for using an Object as a String, if you do know that your object variable does in fact happen to be a string (or at least should be a string), you can indicate that to the compiler by using a typecast.

Object o = 'A String Literal';
try{
    ((String) o).subString(0,8);
} catch(TypeException e){
    ...
}
4
  • 1
    1.2 is a Decimal, and 1.2d is a Double. The compiler masks this difference for you, because they are generally interchangeable. They are not the same thing. Notably, assigning a Double to a Decimal may cause data loss. Null, conversely, is always the same thing (namely, the absence of any thing). You might consider it a parent of Object (e.g. it represents all possible types).
    – sfdcfox
    Jun 3, 2016 at 16:19
  • As a somewhat arbitrary example, consider this runtime exception: Double b = 999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999.0d; Decimal a = (Decimal)b;
    – sfdcfox
    Jun 3, 2016 at 16:48
  • @sfdcfox True, all good points.
    – martin
    Jun 3, 2016 at 16:59
  • @sfdcfox Decided to get rid of that part. Looking back on it now, the compiler's handling of numeric literals isn't that great of an analogy.
    – martin
    Jun 4, 2016 at 2:56
3

It matches every type. Take a look at What is null in Java? for some more background.

There is also a special null type, the type of the expression null, which has no name. Because the null type has no name, it is impossible to declare a variable of the null type or to cast to the null type. The null reference is the only possible value of an expression of null type. The null reference can always be cast to any reference type. In practice, the programmer can ignore the null type and just pretend that null is merely a special literal that can be of any reference type.


Also, try the following with any right operand as the ReferenceType:

null instanceof String

You will get the message:

Operation instanceof is always true since an instance of NULL is always an instance of String


As noted by @sfdcfox, a specific instance of null is not of any type specifically:

String s = null;
Object o = s;
system.assertEquals(false, o instanceof String);
7
  • True, but you might want to mention that a null value in a variable always returns false (at least in the later API versions).
    – sfdcfox
    Jun 3, 2016 at 16:20
  • I don't follow you. You're saying a specific null instance is not an instanceof anything? @sfdcfox
    – Adrian Larson
    Jun 3, 2016 at 16:25
  • Object o; System.assertEquals(false, o instanceof String);
    – sfdcfox
    Jun 3, 2016 at 16:27
  • Well, that doesn't have any reason be true anyway. Tricky because instantiation is what lends o its Type in the first place. To my eye, the question is centered around the null reference specifically.
    – Adrian Larson
    Jun 3, 2016 at 16:31
  • 1
    Believe me, I'm glad it's no longer true. It created all sorts of problems when it used to be. I just think it's useful to note that "null", as a reference, is both no type (as I demonstrated), and every type (as you demonstrated). It's a very magical type, indeed.
    – sfdcfox
    Jun 3, 2016 at 16:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .