About Question enthuware.ocajp.i.v7.2.1137 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Roibeard
Posts: 19
Joined: Sun Jul 12, 2015 6:40 am
Contact:

About Question enthuware.ocajp.i.v7.2.1137 :

Post by Roibeard »

int index = 1; String[] strArr = new String[5]; String   myStr  = strArr[index]; System.out.println(myStr);
Returns null.
However in viewtopic.php?f=2&t=1065 I see:
Passing null in itself never causes NPE. Accessing null always causes NPE.
What is the difference between printing an element which is initialized to null, and accessing a null element?

admin
Site Admin
Posts: 10384
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1137 :

Post by admin »

When you try to access a null i.e. try it call a method on it or try to refer to a field of that class, the JVM throws a NPE because there is no object to access.

By "printing", if you mean passing null to the println method, then you are not accessing null, you are calling the print method of System class's out variable (i.e. you are accessing out, not null). Inside the code of the class of out variable, you will see that it doesn't access null. It does something like this:
if(refToBePrinted == null){
print("null"); //this part is hardcoded in the code.
}
YOu should check out the source code of the class to see exactly what it does.

HTH,
Paul.

Roibeard
Posts: 19
Joined: Sun Jul 12, 2015 6:40 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1137 :

Post by Roibeard »

Thank you

MarkWebDeveloper
Posts: 1
Joined: Wed Nov 20, 2024 9:07 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1137 :

Post by MarkWebDeveloper »

Hi, everyone! This is a curious exercise. I thought the code would not compile since assigning a null value to a variable marked with var is prohibited. But it turns out that in this case the compiler has enough information about the type of the object, since it is taken from an array of Strings. So, in this case it works. Am I right?

admin
Site Admin
Posts: 10384
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1137 :

Post by admin »

Yes, that is right.

raphaelzintec
Posts: 167
Joined: Sun Apr 21, 2024 10:43 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1137 :

Post by raphaelzintec »

i'm starting loving entuwear for real, because i have not seen this in my learning materials but i like the fact that entuwear put these type of questions on purpose for our exam

in my case i chose option non of above because i tought wont compile as well

but now i understand that it has enough informations about the type

Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests