About Question enthuware.ocpjp.ii.v11.2.3311 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
daniko
Posts: 4
Joined: Sun Nov 10, 2019 3:20 pm
Contact:

About Question enthuware.ocpjp.ii.v11.2.3311 :

Post by daniko »

Why is the third option false? The default Locale is set to ITALY, so the fallback method should check for a bundle according to the default locale.

I also ran it with Eclipse and it works. Have i missed something?

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.3311 :

Post by admin »

But the language has been set to "en".
Please post the exact code that you tried and the resource files that you have.

daniko
Posts: 4
Joined: Sun Nov 10, 2019 3:20 pm
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.3311 :

Post by daniko »

Ahm, Sorry now i got it.
I wrote in my First comment about a fallback. There is no Fallback because the "en" bundle is found but it is empty, so there is no reason to search for the "it".

I tried it in eclipse with only the IT bundle and that's why it worked. My bad.

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.3311 :

Post by admin »

In this case, the fallback would be mymsgs_en.properties' parent mymsgs.properties.

mymsgs_it.properties or mymsgs_en_it.properties will not be loaded because the resource bundle is being created for a specific Locale ("en"). If you omit the Locale argument ( i.e. ResourceBundle.getBundle("mymsgs"); ), then files related to the default locale "it" will be used.

daniko
Posts: 4
Joined: Sun Nov 10, 2019 3:20 pm
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.3311 :

Post by daniko »

Code: Select all

import java.util.Locale;
import java.util.ResourceBundle;

public class LocaleTest {
	public static void main(String[] args) {
		Locale.setDefault(Locale.ITALY);
		Locale loc = new Locale.Builder().setLanguage("en").build();
		ResourceBundle rb = ResourceBundle.getBundle("mymsgs", loc);
		System.out.println(rb.getString("helloMsg"));
	}
}
This piece of code loads the bundle mymsgs_IT.properties anyway when it's the only bundle in the classpath and the value of helloMsg is printed...
Running in Eclipse

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.3311 :

Post by admin »

Ok, I guess I will have to go through this again!

borkutip
Posts: 11
Joined: Sat Mar 08, 2014 1:20 am
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.3311 :

Post by borkutip »

Hello,
I do not understand, how can mymsgs_IT.properties be loaded.

Locale ITALY = createConstant("it", "IT");
so the property file for Locale.ITALY should be:
mymsgs_it.properties or
mymsgs_it_IT.properties.

So, I think, at least on Linux, mymsgs_IT.properties will never be loaded.
Is this question/explanation windows-related?

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.3311 :

Post by admin »

Here is the situation:

1. The default Locale is set to Locale.IT That means its language is "it" and country is "IT".

2. A new Locale is being built using Locale loc = new Locale.Builder().setLanguage("en").build(); This means, the language is "en" but the country ( and script and variant) is empty for this Locale.

Now, when you try to load a RB using ResourceBundle.getBundle("mymsgs", loc);, it will look for mymsgs_en.properties only (because country etc. values are empty).

But mymsgs_en.properties is not available, so, getFallbackLocale method is called, which returns the current default locale (here, Locale.IT), and a new search will be made for mymsgs_it_IT.properties and then mymsgs_it.properties

Since mymsgs_it_IT.properties is not available, only mymsgs_it.properties will be loaded (since it is available).

3. Next, the "parent chain" for the resource bundle just loaded (i.e. mymsgs_it_IT.properties) is instantiated. In this case, the parent chain for mymsgs_it_IT.properties will be mymsgs_it and mymsgs.properties (which are not available either as per the problem statement).

Therefore, yes, mymsgs_IT.properties should not be loaded but, since file names are case insensitive on Windows, mymsgs_IT.properties will be loaded on windows.

borkutip
Posts: 11
Joined: Sat Mar 08, 2014 1:20 am
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.3311 :

Post by borkutip »

Hello,

Thank you.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 8 guests