A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/url-getdefaultport-method-in-java-with-examples/ below:

URL getDefaultPort() method in Java with Examples

URL getDefaultPort() method in Java with Examples

Last Updated : 11 Jul, 2025

The

getDefaultPort()

function of

URL class

returns the default port of a specified URL. If the URL scheme or the URLStreamHandler for the URL do not define a default port number then the function returns -1.

Function Signature
public int getDefaultPort()
Syntax
url.getDefaultPort()
Parameter

This function does not require any parameter

Return Value

: The function returns

an Integer value

which is the default port of the specified URL. Below examples will illustrate the use of getDefaultPort() function:

Example 1

Default port of HTTPS

Java
// Java program to show the use
// of the function getDefaultPort()

import java.net.*;

class GFG {
    public static void main(String args[])
    {

        // url  object
        URL url = null;

        try {

            // create a URL
            url = new URL("https:// www.geeksforgeeks.org");

            // get the default port
            int default_port = url.getDefaultPort();

            // display the URL
            System.out.println("URL: " + url);

            // display the default port
            System.out.println("Default Port: "
                               + default_port);
        }

        // if any error occurs
        catch (Exception e) {

            // display the error
            System.out.println(e);
        }
    }
}
Output:
URL: https:// www.geeksforgeeks.org
Default Port: 443
Example 2:

Default port of HTTP

Java
// Java program to show the use
// of the function getDefaultPort()

import java.net.*;

class GFG {
    public static void main(String args[])
    {
        // url  object
        URL url = null;

        try {
            // create a URL
            url = new URL("http:// www.geeksforgeeks.org");

            // get the default port
            int default_port = url.getDefaultPort();

            // display the URL
            System.out.println("URL: " + url);

            // display the default port
            System.out.println("Default Port: "
                               + default_port);
        }

        // if any error occurs
        catch (Exception e) {

            // display the error
            System.out.println(e);
        }
    }
}
Output:
URL: http:// www.geeksforgeeks.org
Default Port: 80
Example 3

Default port of FTP

Java
// Java program to show the use
// of the function getDefaultPort()

import java.net.*;

class GFG {
    public static void main(String args[])
    {
        // url  object
        URL url = null;

        try {

            // create a URL
            url = new URL("ftp:// www.geeksforgeeks.org");

            // get the default port
            int default_port = url.getDefaultPort();

            // display the URL
            System.out.println("URL: " + url);

            // display the default port
            System.out.println("Default Port: "
                               + default_port);
        }

        // if any error occurs
        catch (Exception e) {

            // display the error
            System.out.println(e);
        }
    }
}
Output:
URL: ftp:// www.geeksforgeeks.org
Default Port: 21


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4