About Question enthuware.ocpjp.v8.2.1152 :

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

Moderator: admin

Post Reply
jabenitez
Posts: 9
Joined: Tue Apr 17, 2018 5:03 am
Contact:

About Question enthuware.ocpjp.v8.2.1152 :

Post by jabenitez »

Hi all,

I have a serious question about resource bundles because in the explanation of this question says literally:
While retrieving a message bundle, you are passing a locale explicitly (jp JP). Therefore, it will first try to load appmessages_jp_JP.properties. Since this file is not present, it will look for a resource bundle for default locale. Since you are changing the default locale to "fr", "CA", it will look for appmessages_fr_CA.properties, which is present. This file contains "bonjour" for "greetings", which is what is printed.

Observe that when a resource bundle is not found for a given locale, the default locale is used to load the resource bundle.  Every effort is made to load a resource bundle if one is not found and there are several fall back options. As a last resort, it will try to load a resource bundle with no locale information i.e. appmessages.properties in this case. An exception is thrown when even this resource bundle is not found.

You need to understand this aspect for the purpose of the exam. Please go through http://docs.oracle.com/javase/7/docs/ap ... assLoader) for further details.
However, I have made a small example in which I can observe that it never takes the default properties (it's attached to this post).
Attachments
resourceBundle.zip
(3.59 KiB) Downloaded 247 times

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

Re: About Question enthuware.ocpjp.v8.2.1152 :

Post by admin »

When I run the program, i see "msgs tu" in the output. So it is picking up this value from msgs.properties. Can you tell what exactly are you expecting in the output and why?
If you like our products and services, please help us by posting your review here.

jabenitez
Posts: 9
Joined: Tue Apr 17, 2018 5:03 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1152 :

Post by jabenitez »

admin wrote:When I run the program, i see "msgs tu" in the output. So it is picking up this value from msgs.properties. Can you tell what exactly are you expecting in the output and why?
Hi!

I expect that the program outputs "msgs_es_ES tu" (default locale) as according with the answer explanation ("Observe that when a resource bundle is not found for a given locale, the default locale is used to load the resource bundle.")

Regards.

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

Re: About Question enthuware.ocpjp.v8.2.1152 :

Post by admin »

Yes, the explanation is correct. Notice that it says, "when a resource bundle is not found for a given locale,... ". In your example, it did find a bundle for the matching locale i.e. msgs_en.properties, so why are you expecting it to load msgs_es_ES.properties?
Remove msgs_en.properties and then it will load msgs_es_ES.properties.
If you like our products and services, please help us by posting your review here.

jabenitez
Posts: 9
Joined: Tue Apr 17, 2018 5:03 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1152 :

Post by jabenitez »

jabenitez wrote:
admin wrote:When I run the program, i see "msgs tu" in the output. So it is picking up this value from msgs.properties. Can you tell what exactly are you expecting in the output and why?
Hi!

I expect that the program outputs "msgs_es_ES tu" (default locale) as according with the answer explanation ("Observe that when a resource bundle is not found for a given locale, the default locale is used to load the resource bundle.")

Regards.
Because in my code, the default locale is "es_ES" and the explanation says: "Observe that when a resource bundle is not found for a given locale, the default locale is used to load the resource bundle."

Thanks and best regards.

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

Re: About Question enthuware.ocpjp.v8.2.1152 :

Post by admin »

This line in your code - ResourceBundle rb = ResourceBundle.getBundle("msgs", new Locale("en")); - is looking for a resource bundle for "en". You have msgs_en.properties available. So why would it look for the default resource bundle? It will look for resource bundle for default locale "when a resource bundle is not found for a given locale", which is en in this case.
If you like our products and services, please help us by posting your review here.

jabenitez
Posts: 9
Joined: Tue Apr 17, 2018 5:03 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1152 :

Post by jabenitez »

Hi!

I understood.


Many thanks and best regards.

Mark7777
Posts: 32
Joined: Tue Apr 12, 2016 9:19 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1152 :

Post by Mark7777 »

Tiny bit confused, Paul. So when appmessages_jp_JP.properties can't be found, it looks for appmessages_jp.properties which likewise can't be found. Then it falls back on appmessages.properties and uses the default locale fr_CA. And this is, sort of, appended to the resource bundle name so it is now looking for appmessages_fr_Ca.properties, which it finds in the following:

#In French CA resource bundle file
greetings=bonjour

I'm assuming this file's name is appmessages_fr_Ca.properties which is being sought. Correct? And if this file doesn't exist, an exception is thrown. Am I close?

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

Re: About Question enthuware.ocpjp.v8.2.1152 :

Post by admin »

That is correct!
If you like our products and services, please help us by posting your review here.

d.d.Manchev
Posts: 1
Joined: Wed Oct 27, 2021 5:52 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1152 :

Post by d.d.Manchev »

Hey, Paul, can you please validate the search order:

appmessages_jp_JP
appmessages_jp
appmessages_fr_CA
appmessages_fr
appmessages

correct?

P.S: This is the case it can't find the key in any of the .properties files( assume they exist for the given example )

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

Re: About Question enthuware.ocpjp.v8.2.1152 :

Post by admin »

Yes, that is correct.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 31 guests