Saturday, September 6, 2025
HomeLanguagesJavaLocale getDefault() method in Java

Locale getDefault() method in Java

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:




// Java code to demonstrate
// getLocale() method in Locale
  
import java.util.Locale;
public class GfG {
  
    // main method
    public static void main(String[] args)
    {
        // declaring object of Locale
        Locale locale;
  
        // calling the getDefault method
        locale = Locale.getDefault();
  
        // printing the locale
        System.out.println(locale);
    }
}


Output:

en_US

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:




// Java code to demonstrate
// getLocale() method in Locale
  
import java.util.Locale;
  
public class GfG {
  
    // main method
    public static void main(String[] args)
    {
        // declaring object of Locale
        Locale locale;
  
        // Specified category.
        Locale.Category category = Locale.Category.DISPLAY;
  
        // calling the getDefault method
        locale = Locale.getDefault(category);
  
        // printing the locale
        System.out.println(locale);
    }
}


Output:

en_US

Program 2: To demonstrate NullPointerException




// Java code to demonstrate
// getLocale() method in Locale
  
import java.util.*;
  
public class GfG {
  
    // main method
    public static void main(String[] args)
    {
        // declaring object of Locale
        Locale locale;
  
        try {
            // Specified category = null
            Locale.Category category = null;
  
            // calling the getDefault method
            // This will throw exception
            // as the category passed is null
            locale = Locale.getDefault(category);
  
            // printing the locale
            System.out.println(locale);
        }
        catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}


Output:

Exception: java.lang.NullPointerException
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32270 POSTS0 COMMENTS
Milvus
82 POSTS0 COMMENTS
Nango Kala
6639 POSTS0 COMMENTS
Nicole Veronica
11803 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11869 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7029 POSTS0 COMMENTS
Thapelo Manthata
6705 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS