Page 1 of 1
About Question enthuware.ocajp.i.v8.2.1463 :
Posted: Fri Jun 05, 2015 5:08 am
by FrodoBaggins
This question is a trick question.
Code: Select all
List s1 = new ArrayList( );
s1.add("a");
s1.add("b");
s1.add("c");
s1.add("a");
if(s1.remove("a")){
if(s1.remove("a"))}
s1.remove("b");
}else{
s1.remove("c");
}
}
System.out.println(s1);
Re: About Question enthuware.ocajp.i.v8.2.1463 :
Posted: Fri Jun 05, 2015 5:42 am
by admin
You can expect such questions in the exam as well

Re: About Question enthuware.ocajp.i.v8.2.1463 :
Posted: Sun Aug 26, 2018 1:10 pm
by SLangella
The code doesn't compile without:
List<String> s1 = new ArrayList<>( );
It prints:
Note: Main1.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Re: About Question enthuware.ocajp.i.v8.2.1463 :
Posted: Sun Aug 26, 2018 7:09 pm
by admin
Its a warning, not an error.
Re: About Question enthuware.ocajp.i.v8.2.1463 :
Posted: Mon Aug 27, 2018 6:37 pm
by SLangella
admin wrote: ↑Sun Aug 26, 2018 7:09 pm
Its a warning, not an error.
Ok, so the compiler assumes:
Code: Select all
List<Object> s1 = new ArrayList<>();
is it right?
May I ask, assuming
Code: Select all
ArrayList arr = new ArrayList();
String[] str = {"Marco", "Sergio","Davide","Antonio"};
for(String s:str)
arr.add(s);
arr.removeIf(((String) x) -> x.equals("Sergio"));//1
why line //1 won't compile? Where is the problem with casting in Lambda expression?? (I know that this code works without casting, because Object has equals).
Re: About Question enthuware.ocajp.i.v8.2.1463 :
Posted: Mon Aug 27, 2018 10:03 pm
by admin
List s1 = new ArrayList(); and List<Object> s1 = new ArrayList<>(); are similar but the compiler doesn't need to assume anything. It can work with the non generic version without any assumption.
//1 doesn't work because it is bad syntax. It should be (String x) instead of ( (String) x) but that will still not compile because the arr is not typed to String. So it doesn't have removeIf(String ) method. If you type arr as ArrayList<String>, then (String x) will work in the lambda.
Re: About Question enthuware.ocajp.i.v8.2.1463 :
Posted: Tue Aug 28, 2018 2:20 am
by SLangella
admin wrote: ↑Mon Aug 27, 2018 10:03 pm
List s1 = new ArrayList(); and List<Object> s1 = new ArrayList<>(); are similar but the compiler doesn't need to assume anything. It can work with the non generic version without any assumption.
//1 doesn't work because it is bad syntax. It should be (String x) instead of ( (String) x) but that will still not compile because the arr is not typed to String. So it doesn't have removeIf(String ) method. If you type arr as ArrayList<String>, then (String x) will work in the lambda.
"Non generic version"? I can't find it in the documentation... So casting can't be used in Lambdas, can it? Thank you for support!
Re: About Question enthuware.ocajp.i.v8.2.1463 :
Posted: Tue Aug 28, 2018 6:58 am
by admin
After reading your post, I am not sure if I understand your original issue. I am not sure if you understand the difference between ArrayList<Object> and ArrayList. Further, when you say, "So casting can't be used in Lambdas", it seems like you are not clear on the syntax of lambda expression. You can certainly use casting inside a lambda expression but what you are trying to do with ((String) x) doesn't make any sense. You might want to read up on its syntax from a book.
Re: About Question enthuware.ocajp.i.v8.2.1463 :
Posted: Tue Aug 28, 2018 8:47 am
by SLangella
admin wrote: ↑Tue Aug 28, 2018 6:58 am
After reading your post, I am not sure if I understand your original issue. I am not sure if you understand the difference between ArrayList<Object> and ArrayList. Further, when you say, "So casting can't be used in Lambdas", it seems like you are not clear on the syntax of lambda expression. You can certainly use casting inside a lambda expression but what you are trying to do with ((String) x) doesn't make any sense. You might want to read up on its syntax from a book.
Yes, I'm here to learn. Let's see if I can figure it out starting from your answer:
1 - ArrayList has Object as type like ArrayList<Object>, but it seems they are two different classes. I can't find any documentation about a non generic ArrayList class, can you point me in the right direction please?
2 - Maybe I see my error... the left part of Lambdas is parameters declaration, so if I write ((String) x) I'm casting x to String, but x isn't declared. Am I right? Maybe I can use casting in the right part of Lambdas, do you mean that?
Thank you for your help, I'm reading a lot of books and using Enthuware to prepare for OCA, it's tough

Re: About Question enthuware.ocajp.i.v8.2.1463 :
Posted: Tue Aug 28, 2018 9:12 am
by admin
1. I was not talking about a non-generic ArrayList class. I was talking about its non-generic usage in code i.e. raw ArrayList instead of ArrayList<SomeClass>.
2. Correct. Casting is done when you are using a variable in the body. Not in the declaration. Right, you can use it in the right part of the lambda.
Re: About Question enthuware.ocajp.i.v8.2.1463 :
Posted: Tue Aug 28, 2018 9:50 am
by SLangella
Ok. I went deep in JAVA 1.4 documentation and now I see the difference. Before I didn't know "raw types" and all is much clearer now! Thank for your precious help!!
Re: About Question enthuware.ocajp.i.v8.2.1463 :
Posted: Tue Aug 28, 2018 9:48 pm
by admin
You might want to go through section 12.4.1 of
OCAJP Fundamentals book. It explains this in short but clearly.