Home » Java programming language

Java Locale getDisplayCountry() Method with Example

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

Locale Class getDisplayCountry() method

Syntax:

    public final String getDisplayCountry();
    public String getDisplayCountry(Locale lo);
  • getDisplayCountry() method is available in java.util package.
  • getDisplayCountry() method is used to display the country name for this Locale.
  • getDisplayCountry(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 country.
    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, getDisplayCountry() – It does not accept any parameter.
  • In the second case, getDisplayCountry(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,

  • In the first case, it displays the country name for this locale.
  • In the second case, it displays the country name for the locale based on the given parameter.

Example:

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

import java.util.*;

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

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

    }
}

Output

lo1: en_US
lo2: japanese_JAPAN
lo1.getDisplayCountry(): United States
lo1.getDisplayCountry(lo2): United States





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.