• Post Reply Bookmark Topic Watch Topic
  • New Topic

OCP Chapter 5 Q#5 Jeanne Boyarsky  RSS feed

 
Rhea Camille
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

  Is the answer in OCP Chapter 5 Q#5 in Jeanne Boyarsky's book should be letter E?

This is the question.

5. Suppose that we have the following property files and code. Which bundles are used on lines 8 and 9 respectively?
Dolphins.properties name=The Dolphin age=0
Dolphins_en.properties name=Dolly
age=4
Dolphins_fr.properties name=Dolly
5: Locale fr = new Locale("fr");
6: Locale.setDefault(new Locale("en", "US"));
7: ResourceBundle b = ResourceBundle.getBundle("Dolphins", fr);
8: b.getString("name");
9: b.getString("age");
A. Dolphins.properties and Dolphins.properties
B. Dolphins.properties and Dolphins_en.properties
C. Dolphins_en.properties and Dolphins_en.properties
D. Dolphins_fr.properties and Dolphins.properties
E. Dolphins_fr.properties and Dolphins_en.properties
F. The code does not compile.

As I understand, it should if it didn't find the java/properties file in the given locale it will next search for the default locale.
TIA
 
Rhea Camille
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ooops.. sorry for poor code format, here's the edited one.

Dolphins.properties
     name=The Dolphin
     age=0
Dolphins_en.properties
    name=Dolly
    age=4
Dolphins_fr.properties
   name=Dolly


5: Locale fr = new Locale("fr");
6: Locale.setDefault(new Locale("en", "US"));
7: ResourceBundle b = ResourceBundle.getBundle("Dolphins", fr);
8: b.getString("name");
9: b.getString("age");
A. Dolphins.properties and Dolphins.properties
B. Dolphins.properties and Dolphins_en.properties
C. Dolphins_en.properties and Dolphins_en.properties
D. Dolphins_fr.properties and Dolphins.properties
E. Dolphins_fr.properties and Dolphins_en.properties
F. The code does not compile.
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37403
531
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, D is correct (as in the book.)

There's two "navigation ways" for resource bundle lookups. The first is finding the closest matching bundle. That's Dolphins_fr.properties. You understand that since you asked about Option E.

Once Java finds a matching bundle, it can only navigate up the hierarchy. So it can use a more general bundle (Dolphins.properties) but not jump to a different one.

This means if  Dolphins_fr.properties did not exist, using  Dolphins_en.properties would be fine. Becauase it would use the default locale instead of the provided one. But since the provided locale is found, the default one isn't used at all.
 
Rhea Camille
Greenhorn
Posts: 7
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeanne,

Thank you for clearing that up, my memory messed, I also re-read that chapter and yes, it will find the requested locale first, then the language with no country then the last one will be the default locale.

Cheers!

 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37403
531
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad it is clear now. Far better to get it clarified now than get it wrong on the exam!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!