• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why Conditional expression null accepted to primitive datatype.  RSS feed

 
shan raj
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

if we assign a null to primitive data type
e.g:

javac throw an error.

but if we assign value using condition operation.
e.g:

then the javac is not throwing an error and it is throwing nullpointer exception in runtime

Note: On the same condition operation if we assign both as null.
e.g:
then it shows compilation error.

Could you please tell me what is the reason.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here's my GUESS...

when you use the ternary operator, both the possible returned 'things' have to be of the same type. if you have an int and a null, the null gets promoted to an int with a value of zero.

but if you have two nulls, neither gets promoted to a valid thing that can be assigned to an int.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use javap to generate the byte code and see if that shows you what is happening.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I could, but I don't care that much. I'll leave it as an exercise for the reader. ;-)
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:here's my GUESS...

when you use the ternary operator, both the possible returned 'things' have to be of the same type. if you have an int and a null, the null gets promoted to an int with a value of zero.

but if you have two nulls, neither gets promoted to a valid thing that can be assigned to an int.

I think it's slightly different.

According to the language spec "The conditional operator may be used to choose between second and third operands of numeric type, or second and third operands of type boolean, or second and third operands that are each of either reference type or the null type. All other cases result in a compile-time error."

So (my interpretation/guess):

(i == 1)? null: null, has a null type, which cannot be assigned to an int.
(i == 1)? i + 1: null, here, I think the first argument is getting autoboxed to an Integer, and the result has Integer type. This can then be assigned to an int (via auto-unboxing). But if the result is null the auto-unboxing will throw a NullPointerException.
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the point in guessing? Try it.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the fun in that?

Oh, go on then.
Output is "java.lang.Integer : 2"

Look's like I was right.
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is different from what was originally posted; it was originally posted as an int. Using Object will "force" boxing to occur.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And using an int will force unboxing to occur. It's tricky to be sure exactly what's happening.

OK, try this:
Output "java.lang.Integer".

Now, as I'd read it, if (i == 1) ? i + 1 : null didn't resolve to a reference type, that wouldn't compile. Calling a method on an int doesn't box it. So that shows that it does in fact provide an Integer. Unless I'm missing something else.
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We shall have to look in the JLS, but obviously the compiler sees two possible types: Object and int. The only way to reconcile those two is to impute boxing and unboxing.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but int can have null when we try to autobox it...it will throw null pointer exception
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!