Home » Java programming language

Java Locale getDisplayName() Method with Example

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

Locale Class getDisplayName() method

Syntax:

    public final String getDisplayName();
    public String getDisplayName(Locale lo);
  • getDisplayName() method is available in java.util package.
  • getDisplayName() method is used to display the name for the locale and all the display values will be combined into a single string which would be returned by the methods getDisplayCountry() , getDisplayLanguage() , getDisplayVariant().
  • getDisplayName(Locale lo) method is used to display the name for the locale and all the display values will be combined into a single string that would be returned by the methods getDisplayCountry(Locale lo), getDisplayLanguage(Locale lo), getDisplayVariant(Locale lo).
  • These methods may throw an exception at the time of displaying the name of the locale.
    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, getDisplayName() – It does not accept any parameter.
  • In the second case, getDisplayName(Locale lo)
    Locale lo – represents the locale.

Return value:

In both the cases, the return type of the method is String – it displays name for the locale but does not return any value.

Example:

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

import java.util.*;

public class GetDisplayNameOfLocale {
    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 getDisplayName() method is
        // to return the name for this locale 
        String name1 = lo1.getDisplayName();
        System.out.println("lo1.getDisplayName(): " + name1);

        // By using getDisplayName(locale) method is
        // to return the name for this locale will
        // be localized by the given locale
        String name2 = lo1.getDisplayName(lo2);
        System.out.println("lo1.getDisplayName(lo2): " + name2);
    }
}

Output

lo1: en_US
lo2: japanese_JAPAN
lo1.getDisplayName(): English (United States)
lo1.getDisplayName(lo2): English (United States)


Comments and Discussions!

Load comments ↻





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