About Question enthuware.ocajp.i.v8.2.1463 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
FrodoBaggins
Posts: 14
Joined: Tue Jun 02, 2015 8:32 am
Contact:

About Question enthuware.ocajp.i.v8.2.1463 :

Post 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);

admin
Site Admin
Posts: 10058
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1463 :

Post by admin »

You can expect such questions in the exam as well :)
If you like our products and services, please help us by posting your review here.

SLangella
Posts: 6
Joined: Sun Aug 26, 2018 1:06 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1463 :

Post 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.

admin
Site Admin
Posts: 10058
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1463 :

Post by admin »

Its a warning, not an error.
If you like our products and services, please help us by posting your review here.

SLangella
Posts: 6
Joined: Sun Aug 26, 2018 1:06 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1463 :

Post 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).

admin
Site Admin
Posts: 10058
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1463 :

Post 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.
If you like our products and services, please help us by posting your review here.

SLangella
Posts: 6
Joined: Sun Aug 26, 2018 1:06 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1463 :

Post 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!

admin
Site Admin
Posts: 10058
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1463 :

Post 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.
If you like our products and services, please help us by posting your review here.

SLangella
Posts: 6
Joined: Sun Aug 26, 2018 1:06 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1463 :

Post 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 :lol:

admin
Site Admin
Posts: 10058
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1463 :

Post 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.
If you like our products and services, please help us by posting your review here.

SLangella
Posts: 6
Joined: Sun Aug 26, 2018 1:06 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1463 :

Post 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!!

admin
Site Admin
Posts: 10058
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1463 :

Post by admin »

You might want to go through section 12.4.1 of OCAJP Fundamentals book. It explains this in short but clearly.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: gadsgadsx and 122 guests