Output of Java Program | Set 8
Difficulty level : Intermediate
Predict the output of following Java Programs.
Explanation: Since, s1 and s2 are two different objects the references are not the same, and the == operator compares object reference. So it prints “Not equal”, to compare the actual characters in the string .equals() method must be used.
Inside static method, Person(whoAmI) Kid(who) Kid(whoAreYou)
Explanation: Static binding (or compile time) happens for static methods. Here p.whoAmI() calls the static method so it is called during compile time hence results in static binding and prints the method in Person class.
Whereas p.whoAreYou() calls the method in Kid class since by default Java takes it as a virtual method i.e, dynamic binding.
First statement of try block 15 finally block Main method
Since there is no exception, the catch block is not called, but the finally block is always executed after a try block whether the exception is handled or not.
Explanation : Initially new Thread is started with name gfg1 then in class Two the first run method runs the thread with the name gfg1, then after that a new thread is created calling run method but since a new thread can be created by calling start method only so the previous thread does the action and again gfg1 is printed.Now a new thread is created by calling the start method so a new thread starts with gfg3 name and hence prints gfg3.
This article is contributed by Pratik Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.