About Question enthuware.ocpjp.v17.2.3486 :
Moderator: admin
-
- Posts: 24
- Joined: Wed Sep 28, 2022 9:41 am
- Contact:
About Question enthuware.ocpjp.v17.2.3486 :
This questions has problems? Are sure those alternatives are ok?
-
- Posts: 24
- Joined: Wed Sep 28, 2022 9:41 am
- Contact:
Re: About Question enthuware.ocpjp.v17.2.3486 :
How the method getH() can be executed in the instantiation of Baap b = new Beta(); or Beta bb = (Beta) b;, if it is not in the NoARgs-Constructor?
It is strange.....
How Can the answer be:
Beta 44 4 44 Beta 44 44 44
???
It is strange.....
How Can the answer be:
Beta 44 4 44 Beta 44 44 44
???
-
- Site Admin
- Posts: 10384
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v17.2.3486 :
Did you try compiling and running the given code? There is a detailed explanation also.
-
- Posts: 1
- Joined: Tue Jun 04, 2024 4:35 am
- Contact:
Re: About Question enthuware.ocpjp.v17.2.3486 :
Hi, how is the answer Beta 44 4 44 Beta 44 44 44
and not
4 Beta 44 44 44 Beta 44 44?
I am not understanding why the number is not the first part of the result rather than the word as the system.out method calls b.h/b.hh first = 4/44, then the method b.getH()/bb.getH() = Beta 44 44 / Beta 44 44.
I have run it on my compiler and have achieved the same result as in here, however run on another compiler I have achieved the result I excepted which 4 Beta 44 44 44 Beta 44 44.
I have even played around with the code on my compiler changing the order of the system.out.println() statement and even if I do
b.getH() + " " +b.h
I get
Beta 44 44 4
so here, the number comes last, as expected. Not sure why in the reverse the number does not come first.
Any help would be appreciated. Thank you.
and not
4 Beta 44 44 44 Beta 44 44?
I am not understanding why the number is not the first part of the result rather than the word as the system.out method calls b.h/b.hh first = 4/44, then the method b.getH()/bb.getH() = Beta 44 44 / Beta 44 44.
I have run it on my compiler and have achieved the same result as in here, however run on another compiler I have achieved the result I excepted which 4 Beta 44 44 44 Beta 44 44.
I have even played around with the code on my compiler changing the order of the system.out.println() statement and even if I do
b.getH() + " " +b.h
I get
Beta 44 44 4
so here, the number comes last, as expected. Not sure why in the reverse the number does not come first.
Any help would be appreciated. Thank you.
-
- Site Admin
- Posts: 10384
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v17.2.3486 :
The first print statement is: System.out.println(b.h + " " + b.getH());
In this statement, b.h + " " + b.getH() is an expression that has to be computed before println can print its value.
To compute the value of this expression, it joins b.h (which is 4, because the type of b is Baap and h is an instance field of Baap), " ", and the value returned by b.getH().
Now, to get the value returned by b.getH(), the method getH() has to be invoked. This method has a print statement System.out.println("Beta " + h), which prints "Beta 44". That is why this is the first line of the final output. This method then returns 44 after printing "Beta 44".
After getH() returns, the evaluation of the original expression will be completed as 4 44, which is what the print statement prints. Therefore the second line of the output is 4 44.
In this statement, b.h + " " + b.getH() is an expression that has to be computed before println can print its value.
To compute the value of this expression, it joins b.h (which is 4, because the type of b is Baap and h is an instance field of Baap), " ", and the value returned by b.getH().
Now, to get the value returned by b.getH(), the method getH() has to be invoked. This method has a print statement System.out.println("Beta " + h), which prints "Beta 44". That is why this is the first line of the final output. This method then returns 44 after printing "Beta 44".
After getH() returns, the evaluation of the original expression will be completed as 4 44, which is what the print statement prints. Therefore the second line of the output is 4 44.
Who is online
Users browsing this forum: No registered users and 6 guests