Micheal Bush

Ranch Hand
+ Follow
since Oct 08, 2018
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Micheal Bush

Is it a link which connects the correspond method and JSP page? And please recommend any good books regarding Spring. Thank you!
4 years ago

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



Thanks you very much!

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?

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 (‍)




Thanks. It is actually from Enthuware. And it prints : "noLabel : NO okLabel : YES cancelLabel : Cancel". I could not figure out why.

What will the following code fragment print when compiled and run?  



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_UK.properties
okLabel=YES
noLabel=NO


Answer:
noLabel : NO
okLabel : YES
cancelLabel : Cancel


Hey, I am doing this question but could not figure out why does this code output "noLabel : NO  okLabel : YES  cancelLabel : Cancel". Please help, Thanks!
  • 1. The identity must be defined such that for all elements in the stream u, combiner.apply(identity, u) is equal to u.
  • 2. The accumulator operator op must be associative and stateless such that (a op b) op c is equalto a op (b op c).
  • 3.The combiner operator must also be associative and stateless and compatible with the identity, such that for all u and t combiner.apply(u,accumulator.apply(identity,t)) is equal to accumulator.apply(u,t).
  • 5 years ago



    I don't quite understand these principles. Whats "apply"? Thanks
    5 years ago



    t2 did specify a Comparator when calling the constructor, so it uses the compare() method, which sorts by the int.




    " TreeSet<Sorted> t2 = new TreeSet<>(s1);  "
    Why does TreeSet's constructor take s1 here? Can s1 treat as Comparator here? Thanks

    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!




    My bad, Thank you, Jeanne!
    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!
    Date: 1/13/2019
    Score: 85%
    Prepare kit: JavaRanch+Enthuware+SYBEX
    Feedback: Basically, all the questions are pretty similar to the Enthuware. Some of options appears exactly same as the questions of Standard test in Enthuware.

    Thanks for the help!

    Stephan van Hulst wrote:You can very easily check this by writing the code and compiling it.




    Thanks for your comment but I need a concrete answer regarding my question. Output from IDE will not tell me there is a rule.
    Does compiler still give an error if m2 throws Exception in the method signature ? Compiler will determine that tc.m2() never compile normally, right?

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



    I put an underline there. Can I say IOException is checked exception; therefore, it is not in the same inheritance branch as RuntimeException.

    a finally block may throw an exception and if so, any exception thrown by the try or catch block is lost.



    Is this true? So, compiler thinks tc.m2() may not throw any RuntimeException, therefore it decides exception inside catch block will propagate and give a compile error.
    But if throw a NPE instead of tc.m2() in catch block, compiler understands it. And according to the rule above, all other exceptions are lost but only exception in finally block will throw.
    Am I right?