URL getDefaultPort() method in Java with Examples

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

URL: https:// www.geeksforgeeks.org
Default Port: 443

Example 2: Default port of HTTP

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

URL: http:// www.geeksforgeeks.org
Default Port: 80

Example 3 Default port of FTP

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

URL: ftp:// www.geeksforgeeks.org
Default Port: 21


My Personal Notes arrow_drop_up

Second year Department of Information Technology Jadavpur University

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.