About Question enthuware.ocpjp.i.v11.2.3082 :

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

Moderator: admin

Post Reply
DazedTurtle
Posts: 26
Joined: Wed Oct 02, 2019 1:42 pm
Contact:

About Question enthuware.ocpjp.i.v11.2.3082 :

Post by DazedTurtle »

I assumed the code wouldn't compile because of this bit:

Code: Select all

List<StringBuilder> messages = List.of(new StringBuilder(), new StringBuilder());
It makes it look like it returns an instance of a list, which is impossible since List is an interface. Obviously, I'm wrong about this, so I looked into it.

After reading the bit about "Java 9 has added List.of/Set.of methods that return an unmodifiable list/set containing an arbitrary number of elements" in your explanation, I looked at the documentation, and it doesn't seem to mention what an unmodifiable list is.

I mean, I know what it means in plain English, but what object is it? It mentions "The List instances created by these methods," but as I said above, you can't make an instance of an interface, right?

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

Re: About Question enthuware.ocpjp.i.v11.2.3082 :

Post by admin »

1. In common parlance, when you say "instance of <name of an interface>", you mean, "instance of a class that implements <name of an interface>". It is so because the name of the class is not important. You are interested only in the functionality defined by the interface. You don't care about the actual class name that implements it.
So yes, you can't instantiate List because List is an interface, but "instance of List" really means instance of a class that implements List.

2. The documentation does define what an unmodifiable List is. Again, it is the behavior that is important, not the actual class name. New Java versions may even change the name of the class. If you still want to know the name, just do System.out.println(messages.getClass());

All you should care about is that it is a List and behaves as described in List documentation and interface definition.
If you like our products and services, please help us by posting your review here.

demetrio
Posts: 16
Joined: Mon Sep 30, 2019 11:40 am
Contact:

Re: About Question enthuware.ocpjp.i.v11.2.3082 :

Post by demetrio »

Kindly, could you check if my understanding is correct regard
List<StringBuilder> messages = List.of(new StringBuilder(), new StringBuilder());
?

I understand that:
1 - List.of return an immutable arraylist so I CAN NOT change messages
2 - Although messages is immutable (i.e. I CAN NOT messages.set...) the content inside it I can change since StringBuilder is mutable (this is the reason I can append/insert in each StringBuilder element.

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

Re: About Question enthuware.ocpjp.i.v11.2.3082 :

Post by admin »

1. No, List.of returns a immutable List (not necessarily ArrayList, but very likely). Also, immutable does not mean final. messages is a non-final reference. You can change it to point to some other List object. What you can't change is the list Object returned by List.of. You can't add or remove elements from this list object.
2. Yes, elements of the list are not immutable. So, you can append insert in each StringBuilder.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 46 guests