• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

Explain the reason for this output (Q37, Enthuware)

 
Ranch Hand
Posts: 59
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 386
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Isn't it the same as posted here: link
 
Enthuware Software Support
Posts: 4679
51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and here: http://enthuware.com/forum/viewtopic.php?f=2&t=2075
 
nick woodward
Ranch Hand
Posts: 386
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11604
    178
    Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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)
     
    When it is used for evil, then watch out! When it is used for good, then things are much nicer. Like this tiny ad:
    Thread Boost feature
    https://coderanch.com/t/674455/Thread-Boost-feature
    reply
      Bookmark Topic Watch Topic
    • New Topic