Page 1 of 1

About Question enthuware.ocpjp.v11.2.3378 :

Posted: Sat Apr 10, 2021 2:45 pm
by minajev3
static void doElements(List l) {
l.add("abc");
}
public static void main(String[] args) {
List<Integer> l = new ArrayList<Integer>();
doElements(l);

System.out.println(l);
}

output [abc]


I tried as hard as I can but cant get that warning. Or I am missing something? but I am more amazed that it did add String to Integer List and works without exception. Whaaat?

Re: About Question enthuware.ocpjp.v11.2.3378 :

Posted: Sat Apr 10, 2021 10:36 pm
by admin
That's because the code you have written above is different from the one given in the problem statement.
The code that you have written above doesn't compile.
Problem statement has static void doElements(List l) , while you have static void doElements(List<> l) .

Re: About Question enthuware.ocpjp.v11.2.3378 :

Posted: Sun Apr 11, 2021 2:58 am
by minajev3
<> - that I experimented a bit.
But without it it compiles and runs without any warnings.

Re: About Question enthuware.ocpjp.v11.2.3378 :

Posted: Sun Apr 11, 2021 3:17 am
by admin
Not sure how you are compiling but I get the following message upon compilation from the command line:

Code: Select all

Note: TestClass.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.