We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
This is the code I have written, which passes the testcase, but if I add {} after the if condition as shown below, or try to add any comment, the testcase will fail, which should not happen
if(element instanceof Student)
{
a++;
}
//Or if you add any comment like this anywhere in the code, it will fail
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Instanceof keyword
You are viewing a single comment's thread. Return to all comments →
There is an issue with the testcases for this program
public class InstanceOFTutorial{
static String count(ArrayList mylist){ int a = 0,b = 0,c = 0; for(int i = 0; i < mylist.size(); i++){ Object element=mylist.get(i); if(element instanceof Student) a++; if(element instanceof Rockstar) b++; if(element instanceof Hacker) c++;
}
This is the code I have written, which passes the testcase, but if I add {} after the if condition as shown below, or try to add any comment, the testcase will fail, which should not happen
if(element instanceof Student) { a++; } //Or if you add any comment like this anywhere in the code, it will fail