Paul Anilprem wrote:
Micheal Bush wrote:
Paul Anilprem wrote:The reason 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.
Thanks, Paul, but I still have two questions which I am quite confuse about.
1. Why does mysmsgs.properties load first? It should always load the most specific one. I've read the link provided in the solution. It did not mention that the base file will be load firstly. In this case, the mymsgs_en_UK.properties is available. Why does it need to load base file anyway? The API link says it gonna generate candidate bundle names and getBundle then iterates over the candidate bundle names to find the first one for which it can instantiate an actual resource bundle.
2. Hypothetically speaking, the base file was loaded first. The okLabel from mymsgs_en_UK.properties should superimpose okLabel in mymsgs.properties. Why does the noLabel prints firstly?
Actually, the explanation is simplified so that the result can be easily determined. The complete and exact steps of loading of resource bundles is not as trivial. See this part from JavaDoc API :
Once a result resource bundle has been found, its parent chain is instantiated. If the result bundle already has a parent (perhaps because it was returned from a cache) the chain is complete.
Otherwise, getBundle examines the remainder of the candidate locale list that was used during the pass that generated the result resource bundle. (As before, candidate bundle names where the final component is an empty string are omitted.) When it comes to the end of the candidate list, it tries the plain bundle name. With each of the candidate bundle names it attempts to instantiate a resource bundle (first looking for a class and then a properties file, as described above).
Whenever it succeeds, it calls the previously instantiated resource bundle's setParent method with the new resource bundle. This continues until the list of names is exhausted or the current bundle already has a non-null parent.
Once the parent chain is complete, the bundle is returned.
API description also gives an example where it shows that if your locale is Locale("fr", "CH"), MyResources_fr_CH.class, parent MyResources_fr.properties, parent MyResources.class will be loaded.
Only if a key is not found in the child bundle, MyResources_fr_CH.class, the value from parent MyResources.class. One can visualize this as the entries from the most specific bundle being superimposed on the base bundle.
Regarding printing of values, I don't think there is much to it except that is just using some order while iterating through the keys. It is printing the right values though - the value of okLabel is indeed coming from mymsgs_en_UK.properties bundle and not from the base mymsgs.properties bundle. noLabel is not present in mymsgs_en_UK.properties and so it is coming from mymsgs.properties
Paul Anilprem wrote:The reason 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.
Campbell Ritchie wrote:Please tell us whiich bookthat was from, and page number, so as to reduce copyright problems.
Remembering that resource bundle files go for the most specific first using the language (“en”) and country extensons (“UK”), you are going to get yes, OK, Cancel. Try changing one of the bundle files to read, “Yep”, “Nope”, “Forget it”, and see what happens ()
t2 did specify a Comparator when calling the constructor, so it uses the compare() method, which sorts by the int.
Jeanne Boyarsky wrote:
Micheal Bush wrote:Hi, I don't see line x2 has any problem but line x1 should returns boolean. Can somebody explain the reason of choosing x2. Thank you!
Check the end of chapter explanation. x2 isn't the answer which is why you can't find anything wrong with it!
Stephan van Hulst wrote:You can very easily check this by writing the code and compiling it.
Campbell Ritchie wrote:Good answer, but why isn't IOException permissible?
BTW: OP, please indicate where the blank is. Was it line 6? I think it would be easy to find had you written the code like this:-
a finally block may throw an exception and if so, any exception thrown by the try or catch block is lost.