Home » Java programming language

Java Locale getDisplayLanguage() Method with Example

Locale Class getDisplayLanguage() method: Here, we are going to learn about the getDisplayLanguage() method of Locale Class with its syntax and example.
Submitted by Preeti Jain, on March 08, 2020

Locale Class getDisplayLanguage() method

Syntax:

    public final String getDisplayLanguage();
    public String getDisplayLanguage(Locale lo);
  • getDisplayLanguage() method is available in java.util package.
  • getDisplayLanguage() method is used to display the name of the language defined for this locale and the displayed name will localize as per based on the default locale when possible.
  • getDisplayLanguage(Locale lo) method is used to display the country name for the locale and the displayed name will be localized as per based on the given parameter (lo) when possible.
  • These methods may throw an exception at the time of displaying the name of the language.
    NullPointerException: This exception may throw when the given parameter is null exists.
  • These are non-static methods and it is accessible with the class object and if we try to access these methods with the class name then also we will get an error.

Parameter(s):

  • In the first case, getDisplayLanguage() – It does not accept any parameter.
  • In the second case, getDisplayLanguage(Locale lo)
    Locale lo – represents the locale whose localized behavior depends on the displayed name.

Return value:

In both the cases, the return type of the method is String – it displays name for this locale language.

Example:

// Java program to demonstrate the example 
// of getDisplayLanguage() method of Locale

import java.util.*;

public class GetDisplayLanguageOfLocale {
    public static void main(String[] args) {
        // Instantiates Locale
        Locale lo1 = Locale.getDefault();
        Locale lo2 = new Locale("JAPANESE", "JAPAN");

        // Display Locale
        System.out.println("lo1: " + lo1);
        System.out.println("lo2: " + lo2);

        // By using getDisplayLanguage() method is
        // to return the name for this locale language

        String lang1 = lo1.getDisplayLanguage();
        System.out.println("lo1.getDisplayLanguage(): " + lang1);

        // By using getDisplayLanguage(locale) method is
        // to return the name for this locale will
        // be localized by the given locale

        String lang2 = lo1.getDisplayLanguage(lo2);
        System.out.println("lo1.getDisplayLanguage(lo2): " + lang2);
    }
}

Output

lo1: en_US
lo2: japanese_JAPAN
lo1.getDisplayLanguage(): English
lo1.getDisplayLanguage(lo2): English



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.