It's not a secret anymore!
The moose likes Beginning Java and the fly likes Adding null to Long Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Adding null to Long" Watch "Adding null to Long" New topic

Adding null to Long

Sharon whipple
Ranch Hand

Joined: Jul 31, 2003
Posts: 294
In Car Class, I have plate Number (Long)
when I increment it I do :

what will happen if the PlateNumber is null?
isn't it like null + Long?
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

I got a NullPointerException when I tried it. What heppened when you tried it?

When you do this:

remember that autounboxing is happening in the background. So effectively what you are doing (pre JDK 1.5) is:

One of the reasons (in my opinion) autoboxing is best avoided.
[ June 18, 2008: Message edited by: Paul Sturrock ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Rajkamal Pillai
Ranch Hand

Joined: Mar 02, 2005
Posts: 445

Hi Sharon,

A NullPointerException will be thrown cause you are trying to add a value to 'null'. Never had considered a situation this kinds so I tried it out and thats what happened for me.

Gopikrishna Kunisetty
Ranch Hand

Joined: Jun 12, 2008
Posts: 35
Hi Sharon,

If you have declared the data type of Plate number as long, then obviously its initial value will be '0' and not 'null'. So, if you try to increment, it will run without any issues.

- Krishna<br /> SCJP 1.4 SCWCD 5
Sharon whipple
Ranch Hand

Joined: Jul 31, 2003
Posts: 294
I have to Use Long in order to identify null values,
Well i guess the answer is

I agree. Here's the link:
subject: Adding null to Long
It's not a secret anymore!