• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mala Gupta's Twist in the tale 4.3 confusion: Isn't it null considered a value?

 
Winston Liek
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Which of the following individual options are true for the previous code?
a Code on line 4 is the same as {"Jan", "Feb", null, null},
b No value is stored at multiStrArr[2][2]
c No value is stored at multiStrArr[1][1]
d Array multiStrArr is asymmetric


My answers on the question were c and d... But the book says b and d.
I answered C since there really are no objects pointing to multiStrArr[1][1] in the first place so no value is stored in that element.

why B? Isnt't it null is a literal value for String?
 
Dean Iss
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yea this confused me a bit too.

If you do a System.out.println(multiStrArr[1][1]) you will get a NullPointerException, whereas if you print [2][2] you will print out the literal value of null. This is because [1][1] doesn't actually exist, whereas [2][2] exists but also does not have a value.

It seems like you understand but there is a wording issue. "No value" means the literal value "null" in this case, whereas you thought it meant "Does not exist".


 
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
null is a keyword to indicate the absence of a value.

If you would print multiStrArr[1][1] you'll get a NullPointerException, whereas if you would print multiStrArr[2][2] null will be printed.
 
Winston Liek
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your help.

So does it mean 'No value' means 'null'?
I mean when the question asks for 'no value' i should consider it as null?
 
Dean Iss
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Mala Gaupta's book - I think it would be fair to say that would be a good idea.

In the actual exam however....I'm not sure! Hopefully they will word their questions better - saying that, can something "have no value" if it doesn't exist in the first place? Probably not...
 
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
There is a difference between "not existing" and "no value".

Let's say you have 2 bookshelves 1 and 2 with the statement "no books are stored on bookshelf 3". True or false? I would say false, because bookshelf 3 does not exist at all. But of course that's just my understanding and interpretation.
 
Roibeárd Mac Unfraidh
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:There is a difference between "not existing" and "no value".

Let's say you have 2 bookshelves 1 and 2 with the statement "no books are stored on bookshelf 3". True or false? I would say false, because bookshelf 3 does not exist at all. But of course that's just my understanding and interpretation.


I disagree. I mean, there is a difference- but things that don't exist also have no "value".
It is false that any books are stored on bookshelf 3. it is true that no books are stored on bookshelf 3.
I dig Mala Gupta's book, and I get that she's trying to prepare us for tricky questions. But in this instance the wrong answer (OP's) demonstrates better understanding than the 'right' answer.
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3817
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is another issue that is being overlooked here. It is a multidimensional array of Strings i.e. objects not primitives. Now, the word "value" itself becomes ambiguous here. If by value, the book implies the String objects, then none of the array elements store values. They store references to the objects.
If by values it implies some thing (whether actual object or a reference), then [2][2] does have a special value which is interpreted as null by the JVM.
 
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
Hi Roibeárd Mac Unfraidh,

First of all, a warm welcome to CodeRanch!

Roibeárd Mac Unfraidh wrote:I disagree. I mean, there is a difference- but things that don't exist also have no "value".

Luckily that discussion has nothing to do with Java so it's no problem if we disagree with each other

Roibeárd Mac Unfraidh wrote:I dig Mala Gupta's book, and I get that she's trying to prepare us for tricky questions. But in this instance the wrong answer (OP's) demonstrates better understanding than the 'right' answer.

For some (text-only) questions, it's (sometimes) important to take the question's context into account when answering. The question's context is discussed heavily (with some examples) in this thread. And for the exam you also should be able to select the best fitting answer(s). You'll find an example in this thread (which discusses the same twist in the tale exercise).

Hope it helps!
Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic