VOOZH about

URL: https://www.geeksforgeeks.org/java/url-getdefaultport-method-in-java-with-examples/

⇱ URL getDefaultPort() method in Java with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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
Output:
URL: https:// www.geeksforgeeks.org
Default Port: 443
Example 2: Default port of HTTP
Output:
URL: http:// www.geeksforgeeks.org
Default Port: 80
Example 3 Default port of FTP
Output:
URL: ftp:// www.geeksforgeeks.org
Default Port: 21
Comment