About Question enthuware.ocpjp.v7.2.1768 :

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

Moderator: admin

Post Reply
fariz.siracli
Posts: 22
Joined: Mon Jul 06, 2015 11:45 am
Contact:

About Question enthuware.ocpjp.v7.2.1768 :

Post by fariz.siracli »

i could not understand why 3 items were loaded. Please explain this in more detail. Never saw such kind of question.

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

Re: About Question enthuware.ocpjp.v7.2.1768 :

Post by admin »

Resource bundles created from property files are additive i.e. it contains all the properties contained in the base file and the most specific file. So for example, you can have a base file for English and then special file for US English. So when you create bundle for US English, you will see properties defined in base English and US English. If there are any common properties, US English will override the base ones.

Did you go through this link mentioned in the explanation? http://docs.oracle.com/javase/8/docs/ap ... assLoader-

It explains in detail exactly why 3 items were loaded.
Paul.
If you like our products and services, please help us by posting your review here.

fariz.siracli
Posts: 22
Joined: Mon Jul 06, 2015 11:45 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1768 :

Post by fariz.siracli »

Thank you.

jagoneye
Posts: 97
Joined: Wed Dec 28, 2016 9:00 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1768 :

Post by jagoneye »

Also if suppose default bundle had some additional key value pair say
defaultlabel = default
which was not present in the localized bundle, and say you are using the localized version i.e. en_UK just like in this question.
If you tried to retrieve the key values then first the localized bundle will be searched here en_UK, if not found then the parent file of this localized version will be searched recursively in our case the default bundle and that key values will be retrieved. Hence, this type of question can also be asked! Great job on this question!

jagoneye
Posts: 97
Joined: Wed Dec 28, 2016 9:00 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1768 :

Post by jagoneye »

I want to add another level of complexity say we had three properties file:

Code: Select all

Locale myloc = new Locale.Builder().setLanguage("en").build(); //L1
ResourceBundle msgs = ResourceBundle.getBundle("mymsgs", myloc);

Enumeration<String> en = msgs.getKeys();
while(en.hasMoreElements()){
    String key = en.nextElement();
    String val = msgs.getString(key);
    System.out.println(key+" : "+val);
}

Assume that only the following two properties files (contents of the file is shown below the name of the file) are accessible to the code.

1. mymsgs.properties
okLabel=OK
cancelLabel=Cancel   

2. mymsgs_en.properties
exitLabel=Exit
okLabel=YES
noLabel=NO

3. mymsgs_en_UK.properties
okLabel=YES
noLabel=NO
newLabel=NEW
In this case will all the keys even from en_UK will be loaded
or just from en and its parent resources files???
Last edited by jagoneye on Sat Jan 14, 2017 4:41 am, edited 1 time in total.

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

Re: About Question enthuware.ocpjp.v7.2.1768 :

Post by admin »

What happened when you tried it out?
If you like our products and services, please help us by posting your review here.

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

Re: About Question enthuware.ocpjp.v7.2.1768 :

Post by admin »

BTW, here is a good discussion on this topic: https://coderanch.com/t/672492/certific ... a-SE-Study
If you like our products and services, please help us by posting your review here.

jagoneye
Posts: 97
Joined: Wed Dec 28, 2016 9:00 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1768 :

Post by jagoneye »

My guess was right, only the parents are searched but not the specific bundles.
In this case, only keys from "en" and the base bundle "mymsgs" will be loaded
but not from those below which is "en_UK".

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1768 :

Post by crazymind »

Hi, I am totally lost after finish reading the provided links. How come noLabel print first if mymsgs.properties is loaded first? Any straightforward reading that I can hinge on?

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

Re: About Question enthuware.ocpjp.v7.2.1768 :

Post by admin »

You might want to google. You will find tons of articles on this. The best is the JavaDoc API descrirption though.
If you like our products and services, please help us by posting your review here.

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1768 :

Post by crazymind »

admin wrote:
Wed Mar 06, 2019 7:52 pm
You might want to google. You will find tons of articles on this. The best is the JavaDoc API descrirption though.
create these candidate bundle names
mymsgs_en_UK
mymsgs_en
getBundle then iterates over the candidate bundle names to find the first one for which it can instantiate an actual resource bundle. getBundle attempts to locate a property resource file using the generated properties file name
It find mymsgs_en_UK.properties and getKeys() return {"okLabel", "noLabel"}. So it should prints okLabel=YES noLabel=NO

Which part did I get wrong?

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

Re: About Question enthuware.ocpjp.v7.2.1768 :

Post by admin »

It is difficult to tell because it depends on the locale of your machine, how you are loading the properties, and any other resource property files that might be present (even if you think there is no other file!). There is a particular sequence in which resource bundle files are loaded and the key are may be overwritten by a property file with higher priority. Honestly, you need to go through a book or some article to understand the complete process in detail because it is not a simple thing to describe in a forum post.

Also, you need to post complete and exact code that you are running otherwise it is pretty much impossible to figure out what is wrong at your end.
If you like our products and services, please help us by posting your review here.

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1768 :

Post by crazymind »

admin wrote:
Sat Mar 09, 2019 11:19 pm
It is difficult to tell because it depends on the locale of your machine, how you are loading the properties, and any other resource property files that might be present (even if you think there is no other file!). There is a particular sequence in which resource bundle files are loaded and the key are may be overwritten by a property file with higher priority. Honestly, you need to go through a book or some article to understand the complete process in detail because it is not a simple thing to describe in a forum post.

Also, you need to post complete and exact code that you are running otherwise it is pretty much impossible to figure out what is wrong at your end.
Thanks, I just need to understand the logic of this case. The code is exactly same as the question and ignore all other situations. And Ive been struggle on this for very long time. Can you please let me know why does it print "noLabel : NO okLabel : YES cancelLabel : Cancel"

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

Re: About Question enthuware.ocpjp.v7.2.1768 :

Post by admin »

I ran the code given in the question with the two properties files and it prints the following :

noLabel : NO
okLabel : YES
cancelLabel : Cancel

The logic is as given in the explanation:
mymsgs.properties is the base file for this resource bundle. Therefore, it will be loaded first. Since the language and region specific file is also present (_en_UK), it will also be loaded and the values in this file will be superimposed on the values of the base file.
If you like our products and services, please help us by posting your review here.

Bhaskar
Posts: 19
Joined: Fri Aug 02, 2019 7:04 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1768 :

Post by Bhaskar »

admin wrote:
Sat Jan 14, 2017 12:58 am
BTW, here is a good discussion on this topic: https://coderanch.com/t/672492/certific ... a-SE-Study
I think the discussion in this link is very important to understand how .java and .properties bundle files work together, given that the original book has an errata on this topic. I'd spent a good chunk of my time on this while studying localization. Surprisingly, i didn't come across any practice question on this particular topic. If there is any please share. Thanks.

Post Reply

Who is online

Users browsing this forum: No registered users and 91 guests