Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is null a value?

 
Jamoba Black
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mala's book P204 Twist4.3 -- answer P511
option b is correct in the answer: No value is stored at multiStrArr[2][2]
multiStrArr[2][2] is null, which means null is NOT a value.

But in page83, it is said "The literal value of all types of object reference variables is null." -- which means null is a kind of value.

Confused!


 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It pertains to references aka pointers only. It means an unassigned reference:

Horror h; // val of h is null
h = new Horror(); //h is no longer null

For the purposes of Java it makes more sense to think of it as a state than a value I think. Because you never have to deal with the value of a reference in Java.
 
Joe Bishara
Ranch Hand
Posts: 175
17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The book says

Option (b) is correct. The array element at the position exists but isn’t assigned any value. It’s assigned to null.

This can be interpreted as

Option (b) is correct. The array element at the position exists but isn’t explicitly assigned a value and is therefore implicitly assigned a value of null.

The statement "No value is stored at multiStrArr[2][2]" is not very accurate because null is a value

The JVM Specification says
The default value of a reference type is null.

null is a literal (just like 2, 1.1, true, false)

The internal representation of the value null is unknown (maybe because we don't need to know this)

The JVM Specification says
The Java Virtual Machine specification does not mandate a concrete value encoding null.
 
Jamoba Black
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Bishara wrote:The book says

Option (b) is correct. The array element at the position exists but isn’t assigned any value. It’s assigned to null.

This can be interpreted as

Option (b) is correct. The array element at the position exists but isn’t explicitly assigned a value and is therefore implicitly assigned a value of null.

The statement "No value is stored at multiStrArr[2][2]" is not very accurate because null is a value

The JVM Specification says
The default value of a reference type is null.

null is a literal (just like 2, 1.1, true, false)

The internal representation of the value null is unknown (maybe because we don't need to know this)

The JVM Specification says
The Java Virtual Machine specification does not mandate a concrete value encoding null.


You are right, it seems the statement of Option (b) is not very exact.
Thank you!
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guillermo Ishi wrote:Horror h; // val of h is null
h = new Horror(); //h is no longer null

One (very) important remark about this code. If h is a local variable, the value of h after the 1st line is not null. It's undefined because local reference variables do not get a default value. Only class and instance reference variables get null as default value.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to JLS section 4.1 The Kinds of Types and Values, besides primitive types and reference types there's also a special null type. And if you look to JLS section 3.10.7. The Null Literal, the null type has one value, the null reference, represented by the null literal null.

I would have selected option B as a correct answer as well. Because in a String array, I don't consider null as a value; in my interpretation only actual strings (like "A", "B", "Java",...) are values. But I can understand why you are confused, because right above the Twist in the Tale exercise there is even this statement: As you might have noticed, multiStrArr[1] refers to a null value. So the statement is quite ambiguous. On the actual exam each question tells you how many correct answers to select, so you should be capable to pick the best fitting answer when needed. If the questions states "select 1 correct answer", you select without any doubt only answer D. If the question tells you to select 2 correct answers, you'll probably select B and D (because the other options A and C are definitely incorrect).

Hope it helps!
Kind regards,
Roel
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Guillermo Ishi wrote:Horror h; // val of h is null
h = new Horror(); //h is no longer null

One (very) important remark about this code. If h is a local variable, the value of h after the 1st line is not null. It's undefined because local reference variables do not get a default value. Only class and instance reference variables get null as default value.

Good point. There's aways horror looming.
 
Jamoba Black
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:According to JLS section 4.1 The Kinds of Types and Values, besides primitive types and reference types there's also a special null type. And if you look to JLS section 3.10.7. The Null Literal, the null type has one value, the null reference, represented by the null literal null.

I would have selected option B as a correct answer as well. Because in a String array, I don't consider null as a value; in my interpretation only actual strings (like "A", "B", "Java",...) are values. But I can understand why you are confused, because right above the Twist in the Tale exercise there is even this statement: As you might have noticed, multiStrArr[1] refers to a null value. So the statement is quite ambiguous. On the actual exam each question tells you how many correct answers to select, so you should be capable to pick the best fitting answer when needed. If the questions states "select 1 correct answer", you select without any doubt only answer D. If the question tells you to select 2 correct answers, you'll probably select B and D (because the other options A and C are definitely incorrect).

Hope it helps!
Kind regards,
Roel


Excellent explanation!
It seems we need to understand it via the context.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic