• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Explain the reason for this output (Q37, Enthuware)

 
s sivaraman
Ranch Hand
Posts: 59
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


how come the output is
beta 44
4 44
beta 44
44 44

rather

4 beta 44
44
44 beta 44
44
 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This confused me too, way more than I thought it should have, so thanks a lot for the question!

Anyway I believe it's because the println in main has to evaluate the 'getH()' method before it can print anything, it's printing the concatenation, not each individual element. So "beta + h" in class Question37's method getH() preceeds main's System.out.println(b.h +""+ b.getH()).

I've a better explanation in more detail if you want it, but it helped me a lot to work it out, so it might help you too using the above info. I'm happy to give it to you if you want.

Regards,

Nick
 
Lukasz Mazurek
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't it the same as posted here: link
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3817
10
 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
might be worth marking questions like this as 'enthuware'?

it just popped up the next day in my standard test - i was pretty relieved to see a nice easy question
 
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
nick woodward wrote:Anyway I believe it's because the println in main has to evaluate the 'getH()' method before it can print anything, it's printing the concatenation, not each individual element. So "beta + h" in class Question37's method getH() preceeds main's System.out.println(b.h +""+ b.getH()).

You are absolutely spot-on!

When this line of code is executedthis is the flow of execution:
1/ b.h is evaluated -> 4 (rule 1: which instance variable is accessed, is determined at compile time based on the type of the reference variable -> b is of type Baap)
2/ 4 and " " are concatenated -> "4 "
3/ b.getH() is evaluated -> getH() method in Question37 class is executed (rule 2: which method is executed, is determined at runtime based on the type of the actual object -> actual object is of type Question37)
3a/ the statement System.out.println("beta "+h); is executed
3a1/ h is evaluated -> 44
3a2/ "beta " and 44 are concatenated -> "beta 44"
3a3/ "beta 44" is printed
3b/ h is evaluated -> 44
3c/ 44 is returned
4/ "4 " and 44 are concatenated -> "4 44"
5/ "4 44" is printed
And that's why "beta 44" is in the output before "4 44".

This (very) detailed explanation lists 2 very simple (and hopefully easy to remember) but very, very, very important rules:
  • Which instance variables you can access is determined at compile time based on the reference variable type.
  • Which instance methods you can call/invoke is determined at compile time based on the reference variable type. Which instance method is actually executed is decided at runtime based on the type of the actual object (= polymorphism).


  • Hope it helps!
    Kind regards,
    Roel
     
    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
    nick woodward wrote:might be worth marking questions like this as 'enthuware'?

    Definitely! When you post a quote and/or a code snippet, you should always QuoteYourSources.

    (Because it's already confirmed to be a question from Enthuware, I updated the subject accordingly)
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic