Java Magazine, Nov/Dec 2016
ORACLE COM JAVAMAGAZINE NOVEMBER DECEMBER 2016 70 fix this option E if all potential resource files including the base bundle are missing In this situation the getBundle method throws a MissingResourceException which is an unchecked exception so no code has to admit this possibility Of course thats not relevant to this question because the base bundle is found By the way the process is explained in more detail in the API documentation This behavior is really quite useful If you had a single French file ConfigData_ fr properties it would probably serve quite well for any French speakers whether they were French or Canadian or they were from any of nearly 30 countries in Africa or about a dozen others around the world Its interesting to note of course that Java has a nontrivial multilingual capability While it generally does not attempt to translate text between languages it knows the names of months and days of the week in many languages so date type objects are converted into locale appropriate text without help from the programmer Question 2 The correct answer is option B This is one of those questions that investigates a situation where misunderstanding can easily occur with potentially difficult to debug consequences This question might be too hard for quizzes but its a truly enlightening problem so Ill share it with you nonetheless So whats going on Because the correct answer is option B it appears that the code compiles and runs but it does not modify the data in the collection The behavior hinges on the method at line n1 If you look at this in an IDE youll see that the argument to the remove method is an Object not an int The remove Object method is defined on Collection and it removes an object that matches the argument But this collection contains only three strings Fred Jim and Sheila Consequently no change is made to the collection and the answer makes sense I think that this leaves a couple of curiosities remaining First isnt there a remove int method defined for a List And doesnt that method remove the item at the given index position Well yes both of those are true statements However the relationship between remove int and remove Object is one of method overloading not method overriding which means that the compiler decides which method to invoke rather than the object thats the target of the invocation making the decision at runtime In this example the collection object is actually a List but the code refers to it as a Collection and Collection does not have the remove int method collections are not intrinsically ordered so positional indexes are not meaningful Because of this the compile time decision is made to invoke the remove Object method As a point of interest if you simply change the declared type of the variable coll from Collection to List Fred is removed from the list and the output does indeed change from whats shown for option B to whats shown for option A But at this point you might reasonably ask why the compiler allows the call to remove on a Collection String to accept an Integer argument Doesnt the generics mechanism restrict the argument to a String and wouldnt that result in a compilation failure option C If you check the documentation for this method it does not actually take a generic argument it takes Object The same is true of the contains method too In efect this allows you to say if the code contains this item remove it while referring to an item that cannot possibly be present because its the wrong type This seemingly useless behavior allows for backward compatibility with code written prior to the advent of generics Similarly you can ask whether a collection of automobiles contains an apple and while the question is valid you just get the answer no Its interesting to note that many IDEs and style guides including the CERT Secure Java Coding recommendations
You must have JavaScript enabled to view digital editions.