GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Compile Error Vs runtime Error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Compile Error Vs runtime Error" Watch "Compile Error Vs runtime Error" New topic
Author

Compile Error Vs runtime Error

Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Hi,

Why I am getting one error in loop during compile time and similar error during Runtime in the other loop?



OCPJP 6.0-81% | Preparing for OCWCD
http://www.certpal.com/blogs/cert-articles | http://sites.google.com/site/mostlyjava/scwcd |
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

First of all please QuoteYourSources.

The compile time error is because you're trying to compare a null (reference to nothing) to a primitive boolean. That can't be done. It's like comparing apples to your left shoe. They are completely different.

The NullPointerException is caused by auto-unboxing of the second value of the Boolean array. Note that it is a wrapper Boolean not a primitive thus it's default value is null.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

Wouter Oet wrote:The compile time error is because you're trying to compare a null (reference to nothing) to a primitive boolean. That can't be done. It's like comparing apples to your left shoe. They are completely different.
The NullPointerException is caused by auto-unboxing of the second value of the Boolean array. Note that it is a wrapper Boolean not a primitive thus it's default value is null.


this time it works fine*
as null is comparable to object,also wrapper classes,is it so?


SCJP 6 | FB : Java Certifications-Help. | India Against Corruption
Trivikram Kamat
Ranch Hand

Joined: Sep 26, 2010
Posts: 155
Arjun Srivastava wrote:
this time it works fine*
as null is comparable to object,also wrapper classes,is it so?


Yeah, it must be the correct reason.
An Object can be given a null reference. So, it can be compared to a null.
And all classes are derived from Object class in Java (except Object class of course). So, instances of Wrapper classes can be compared to that of Object class.


OCPJP6
 
GeeCON Prague 2014
 
subject: Compile Error Vs runtime Error