What is the result?

Given three resources bundles with these values set for menu1: (the default resource bundle in
US English.)
English US Resource Bundle
Menu1 = small
French Resource Bundle
Menu1 = petit
Chinese Resource Bundle
Menu1 =
And given the code fragment:

Locale.setDefault(new Locale(“es”, “ES”)); // Set default to Spanish and Spain
Locale loc1 = Locale.getDefault();
ResourceBundle message = ResourceBundle.getBundle(“MessageBundle”, loc1);
System.out.println(message.getString(“menu1”));
What is the result?

Given three resources bundles with these values set for menu1: (the default resource bundle in
US English.)
English US Resource Bundle
Menu1 = small
French Resource Bundle
Menu1 = petit
Chinese Resource Bundle
Menu1 =
And given the code fragment:

Locale.setDefault(new Locale(“es”, “ES”)); // Set default to Spanish and Spain
Locale loc1 = Locale.getDefault();
ResourceBundle message = ResourceBundle.getBundle(“MessageBundle”, loc1);
System.out.println(message.getString(“menu1”));
What is the result?

A.
No message is printed

B.
petit

C.
small

D.
A runtime error is produced

Explanation:
Compiles fine, but runtime error when trying to access the Spanish Resource bundle
(which doesnot exist):
Exception in thread “main” java.util.MissingResourceException: Can’t find bundle for base name
messageBundle, locale es_ES



Leave a Reply 4

Your email address will not be published. Required fields are marked *


Sergio

Sergio

I think answer could be C.
When a ResourceBundle is searched for in this case ‘es_ES’ and is not found, the ResourceBundle ‘es’ is searched for. If even the latter cannot be found the default resource bundle is searched ‘MessageBundle.properties’. After that MissingResourceException is thrown.

There is a lot to read here:

http://docs.oracle.com/javase/7/docs/api/java/util/ResourceBundle.html#getBundle%28java.lang.String,%20java.util.Locale,%20java.lang.ClassLoader%29

I think the important bit is:

If still no result bundle is found, the base name alone is looked up. If this still fails, a MissingResourceException is thrown.

Freek

Freek

D.
Your statement concerning the default resource bundle is correct, but the default was changed by the code to “es”, “ES”.

Klyaksa

Klyaksa

I tested and got C.