Locale.getDefault(): This method is available in java.util.Locale class of Java.
Syntax:
Locale java.util.Locale.getDefault(Category category)
This method takes one argument of type Category as its parameter. This method returns the current value of the default locale for the specified Category of this instance of the Java Virtual Machine.
Parameters: One parameter is required for this method.
category: the specified category to get the default locale.
Returns: the default locale for the specified Category for this instance of the Java Virtual Machine.
Throws: NullPointerException - if category is null.
For Example:
Category category = Category.FORMAT
Locale.getDefault(category) = > It returns en_US.
Approach 1: When category is not null.
Java
import java.util.Locale;import java.util.Locale.Category;public class LocalegetDefaultCategory {public static void main(String[] args) {Category category = Category.FORMAT;System.out.println(Locale.getDefault(category));}}
Output:
en_US
Approach 2: When a category is null.
Java
import java.util.Locale;import java.util.Locale.Category;public class LocalegetDefaultCategory {public static void main(String[] args) {Category category = null;System.out.println(Locale.getDefault(category));}}
Output:
Exception in thread "main" java.lang.NullPointerException:Cannot invoke "java.util.Locale$Category.ordinal()"because "category" is null at java.base/java.util.Locale.getDefault(Locale.java:942)
No comments:
Post a Comment