Locale setDefault() Method in Java with Examples
The setDefault(Locale newLoc) method of Locale class in Java is used to set the default locale for this instance of the JVM or the Java Virtual machine and this in no way affects the host locale.
Syntax:
public static void setDefault(Locale newLoc)
Parameters: The method takes one parameter newLoc of Locale type and this refers to the new default Locale that is to be set.
Return Value: The method does not return any value.
Exception: The method can throw exceptions like-
- SecurityException which is thrown if a security manager exists and its checkPermission method doesn’t allow the operation.
- NullPointerException which is thrown if the newLoc is null
Below programs illustrate the setDefault() Method of Locale class:
Example 1:
// Java code to illustrate hashCode() method import java.util.*; class Locale_Demo { public static void main(String[] args) { // Creating a new locale Locale first_locale = new Locale("nu", "NO", "NY"); // Displaying first locale System.out.println("First Locale: " + first_locale); // Setting the Locale Locale.setDefault(new Locale("ar", "SA")); Locale new_locale = Locale.getDefault(); // Displaying the hash_code of new locale System.out.println("The Hash Code: " + new_locale); } } |
First Locale: nu_NO_NY The Hash Code: ar_SA
Example 2:
// Java code to illustrate hashCode() method import java.util.*; class Locale_Demo { public static void main(String[] args) { // Creating a new locale Locale first_locale = new Locale("en", "In"); // Displaying first locale System.out.println("First Locale: " + first_locale); // Setting the Locale Locale.setDefault(new Locale("en", "GB")); Locale new_locale = Locale.getDefault(); // Displaying the hash_code of new locale System.out.println("The Hash Code: " + new_locale); } } |
First Locale: en_IN The Hash Code: en_GB
Reference: https://docs.oracle.com/javase/7/docs/api/java/util/Locale.html#setDefault(java.util.Locale)
Recommended Posts:
- Locale setDefault(Locale.Category, Locale) Method in Java with Examples
- TimeZone getDisplayName(Locale locale) Method in Java with Examples
- Locale getDisplayVariant(Locale inLoc) Method in Java with Examples
- Locale.Builder setLocale(Locale) method in Java with Examples
- Locale getDisplayCountry(Locale) Method in Java with Examples
- Locale getDisplayLanguage(Locale) Method in Java with Examples
- Locale getDisplayName(Locale) Method in Java with Examples
- TimeZone setDefault() Method in Java with Examples
- Locale getCountry() Method in Java with Examples
- Locale getISOLanguages() Method in Java with Examples
- Locale getLanguage() Method in Java with Examples
- Locale getDisplayVariant() Method in Java with Examples
- Locale getVariant() Method in Java with Examples
- Locale getAvailableLocales() Method in Java with Examples
- Locale toString() Method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



