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

Minor typo about method returns on page 313, chapter 6 (Java OCA 8 Programmer I Study Guide)

 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On page 313 is asked “What do you think this method returns?”

But there isn't any space character in additional String values. So the following sentence

The correct answer is before catch finally done.

should be

The correct answer is “beforecatchfinallydone”.

or it would be good that to add space to the end of each additional String values so return value looks clearly. Maybe authors intend to add space.
 
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
Mushfiq Mammadov wrote:The correct answer is before catch finally done.

should be

The correct answer is “beforecatchfinallydone”.

Indeed!

Mushfiq Mammadov wrote:or it would be good that to add space to the end of each additional String values so return value looks clearly. Maybe authors intend to add space.

Based on the issues Jeanne already had with using spaces, she'll probably use a dash or colon or some other punctuation mark Return value: before:catch:finally:done:

And now an additional question for you (which could be mentioned in the book as well): what's the output if I add the following line of code to the outermost catch-block (the one where "done:" is added to result)?

Hope it helps!
Kind regards,
Roel
 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:And now an additional question for you (which could be mentioned in the book as well): what's the output if I add the following line of code to the outermost catch-block (the one where "done:" is added to result)?

At first, thanks a lot for your question, Roel. It is a very interesting question for me and help me to learn new things. I explore about instanceof in detail.
I had two choices:
1. true:true:true - according to Polymorphism
2. false:false:true - the try statement throws the last exception in finally block (throw new Exception();), the previous exception in catch block (throw new RuntimeException();) is forgotten. I read it today, I hope I remember it correctly)
I run these code and output is second option.

P.S. I don't understand the following example when I explore instanceof.
then the output is
Returns a boolean value true
Returns a boolean value true
Returns a boolean value true
Returns a boolean value false


The 3rd output true (m instanceof Object) is unclear. I understand instanceof for this example such
1st: Map m=new Map();
2nd: Map m=new HashMap();
3rd: Map m=new Object();
4th: Map m=new Date();

What is wrong with me?
 
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
Mushfiq Mammadov wrote:I had two choices:
1. true:true:true - according to Polymorphism
2. false:false:true - the try statement throws the last exception in finally block (throw new Exception();), the previous exception in catch block (throw new RuntimeException();) is forgotten. I read it today, I hope I remember it correctly)
I run these code and output is second option.

First of all, polymorphism is not involved in this code at all! It only applies when you are invoking a method on an object. You probably meant inheritance: NullPointerException IS-A Exception and RuntimeException IS-A Exception. But the 2nd option indeed is correct and your reasoning is spot-on! So you definitely remembered correctly (and that was exactly the reason why I asked you this question )

Mushfiq Mammadov wrote:The 3rd output true (m instanceof Object) is unclear. I understand instanceof for this example such
1st: Map m=new Map();
2nd: Map m=new HashMap();
3rd: Map m=new Object();
4th: Map m=new Date();

What is wrong with me?

First of all, what's the purpose of the instanceof operator? The instanceof operator compares an object to a specified type. You can use it to test if an object is an instance of
  • a class,
  • a subclass, or
  • a class that implements a particular interface.

  • So if an object passes the IS-A test, the instanceof operator will return true; otherwise false.

    So let's revisit your examples. The reference variable m is referring to a HashMap instance, so
    1/ HashMap IS-A Map --> true (HashMap implements the Map interface)
    2/ HashMap IS-A HashMap --> true (HashMap is of course a HashMap )
    3/ HashMap IS-A Object --> true (Object is the mother class of all classes, so every non-null object/instance IS-A Object)
    4/ HashMap IS-NOT-A Date --> false (Date isn't a parent class of HashMap)

    I hope that clears your doubts a bit. Time for another pop quiz question: will the output of instanceoftest be the same if you changed the type of reference variable m to HashMap? So what's the output if you replace Map m=new HashMap(); with HashMap m=new HashMap();?

    Kind regards,
    Roel
     
    Mushfiq Mammadov
    Ranch Hand
    Posts: 187
    25
    Java jQuery Netbeans IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Roel De Nijs wrote:First of all, polymorphism is not involved in this code at all! It only applies when you are invoking a method on an object. You probably meant inheritance: NullPointerException IS-A Exception and RuntimeException IS-A Exception.

    I confuse this "Polymorphic Parameters"

    I imagine catch as a method and Exception e as polymorphic parameter of catch.


    Roel De Nijs wrote:
    Mushfiq Mammadov wrote:The 3rd output true (m instanceof Object) is unclear. I understand instanceof for this example such
    1st: Map m=new Map();
    2nd: Map m=new HashMap();
    3rd: Map m=new Object();
    4th: Map m=new Date();

    What is wrong with me?

    So let's revisit your examples. The reference variable m is referring to a HashMap instance, so
    1/ HashMap IS-A Map --> true (HashMap implements the Map interface)
    2/ HashMap IS-A HashMap --> true (HashMap is of course a HashMap )
    3/ HashMap IS-A Object --> true (Object is the mother class of all classes, so every non-null object/instance IS-A Object)
    4/ HashMap IS-NOT-A Date --> false (Date isn't a parent class of HashMap)

    I was sleepy when I wrote this The right side is instance and we mustn't change it, we change the left side to test it. So
    1st: Map m = new HashMap();
    2nd: HashMap m = new HashMap();
    3rd: Object m = new HashMap();
    4th: Date m = new HashMap();

    Am I right?


    Roel De Nijs wrote:Time for another pop quiz question: will the output of instanceoftest be the same if you changed the type of reference variable m to HashMap? So what's the output if you replace Map m=new HashMap(); with HashMap m=new HashMap();?

    I thought that the output will be the same: true true true false. But I got compile error in line 10 when I test these codes: "incompatible types: HashMap cannot be converted to Date". But we use Map without problem. Maybe the reason is that Map is interface. You noted above

    Roel De Nijs wrote:You can use it to test if an object is an instance of
  • a class,
  • a subclass, or
  • a class that implements a particular interface.

  •  
    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
    Mushfiq Mammadov wrote:I imagine catch as a method and Exception e as polymorphic parameter of catch.

    According to the explanation you mentioned, it only applies to methods (not with a catch block), although in that case it's also appropriate. I always try to use the "official" terms/jargon as mentioned in Oracle's Java Tutorials. And it seems "polymorphic parameter" isn't used in these tutorials, so I don't use it (because only people who have read the OCA8 study guide - by Jeanne & Scott - know the meaning and all others are probably confused because they are unfamiliar with the term/jargon).

    Mushfiq Mammadov wrote:The right side is instance and we mustn't change it, we change the left side to test it. So
    1st: Map m = new HashMap();
    2nd: HashMap m = new HashMap();
    3rd: Object m = new HashMap();
    4th: Date m = new HashMap();

    Am I right?

    I don't know why you want to do it like that. Seems like you are overcomplicating a little bit for no obvious reason. Why don't you use the very easy IS-A check? HashMap IS-A Map, so instanceof returns true. HashMap IS-NOT-A Date, so instanceof will return false or the code fails to compile (depending on the type of the reference variable).

    Mushfiq Mammadov wrote:I thought that the output will be the same: true true true false. But I got compile error in line 10 when I test these codes: "incompatible types: HashMap cannot be converted to Date". But we use Map without problem. Maybe the reason is that Map is interface.

    Yes! Map being an interface is indeed the reason. When you use instanceof with an interface (or casting to an interface) there is some very subtle difference than when you use 2 classes. It's very well explained in this post (by me, so little biased here )

    Hope it helps!
    Kind regards,
    Roel
     
    Mushfiq Mammadov
    Ranch Hand
    Posts: 187
    25
    Java jQuery Netbeans IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Roel De Nijs wrote: Yes! Map being an interface is indeed the reason. When you use instanceof with an interface (or casting to an interface) there is some very subtle difference than when you use 2 classes. It's very well explained in this post (by me, so little biased here )

    It is a very useful link, I read it twice. I bookmark this link and I will read it again in future) Thanks a lot!
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic