getDefault()
This method returns default Locale set by the Java Virtual Machine. This is static method so it can be called without creating object of the class Locale.
Syntax:
public static Locale getDefault()
Return Value: The method returns default Locale set by the Java Virtual Machine.
Below is the code to illustrate getDefault() method:
Program 1:
import java.util.Locale;
public class GfG {
public static void main(String[] args)
{
Locale locale;
locale = Locale.getDefault();
System.out.println(locale);
}
}
|
getDefault(Locale.Category category)
This method returns default Locale set by the Java Virtual Machine for the specified category. This is static method so it can be called without creating object of the class Locale.
Syntax:
Locale.getDefault(Locale.Category category)
Parameters: It takes a mandatory parameter category of type Locale.Category.
Return Value: The method returns default Locale set of type Locale, for the specified category.
Exceptions: If the category passed in the parameter is null, the getDefault() method will throw NullPointerException.
Below is the code to illustrate getDefault(Locale.Category category):
Program 1:
import java.util.Locale;
public class GfG {
public static void main(String[] args)
{
Locale locale;
Locale.Category category = Locale.Category.DISPLAY;
locale = Locale.getDefault(category);
System.out.println(locale);
}
}
|
Program 2: To demonstrate NullPointerException
import java.util.*;
public class GfG {
public static void main(String[] args)
{
Locale locale;
try {
Locale.Category category = null ;
locale = Locale.getDefault(category);
System.out.println(locale);
}
catch (Exception e) {
System.out.println( "Exception: " + e);
}
}
}
|
Output:
Exception: java.lang.NullPointerException
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
07 Sep, 2018
Like Article
Save Article