I have specific code from a Java certification question, and its output look puzzled me. Here is the code
class Baap { public int h = 4; public int getH() { System.out.println("Baap " + h); return h; } } class Beta extends Baap { public int h = 44; public int getH() { System.out.println("Beta " + h); return h; } public static void main(String[] args) { Baap b = new Beta(); System.out.println(b.h + " " + b.getH()); } }
Conclusion:
Beta 44 4 44
I expected this to be:
4 Beta 44 44
Why is he making this conclusion?
The output consists of two parts:
getH()
main()
The line created getH()is printed before the line created main()because it getH()must end before main, ending the construction of its output.
main
Now the conclusion should be clear: even if it 4is evaluated before the call getHis made inside main, it will be printed after it getH()returns.
4
getH
System.out.println(b.h + " " + b.getH()) -, b.h + " " + b.getH() .
System.out.println(b.h + " " + b.getH())
b.h + " " + b.getH()
b.getH() Beta ( ), Beta 44.
b.getH()
Beta
Beta 44
b.h (4) b.getH() (44) println 4 44.
b.h
println
4 44
b.h h (4), b Baap ( ), . , b.getH() h (44), .
h
b
Baap
JLS:
15.7.2.Java , ( &, ||, ?:) , .
15.7.2.
Java , ( &, ||, ?:) , .
getH() , , , , .
As there are two parts:
String baapBetaStr = b.h + " " + b.getH();
A) string concatenation
B) method call
And, of course, to combine this line, all parts of this expression must first be evaluated ("run")!
public static void main(String[] args) { Baap b = new Beta(); System.out.print(b.h + " "); System.out.print(b.getH()); }
Source: https://habr.com/ru/post/1667121/More articles:Clojure Keyword Example Security Problem - securityhttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1667117/unit-testing-with-generic-repository-in-net-core&usg=ALkJrhiMJugNJDFN9A533Ka6sFTczjA2ggGraphical representation of a GraphQL schema - graphqlHow to add wiredesignz modular extension to codeigniter 3.0 - phpuse protection to check zero level without implicit deployment - swiftCkEditor Cannot set property 'dir' from undefined - javascriptHow to check if two loops match using OpenCV matchShapes? - javaAdd blur blur to navigator text when unfolding Bootstrap carousel - javascriptNSNotificationCenter Swift 3.0 on keyboard show and hide - iosDoes it make sense to create an array property? - arraysAll Articles