Win a copy of Microservices in Action this week in the Web Services forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

OCP Chapter 5 Q#5 Jeanne Boyarsky  RSS feed

 
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.
 
author & internet detective
Posts: 38925
686
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
Posts: 38925
686
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!