import java.net.*; public class URLDemo{ public static void main(String[] args) throws Exception{ // Create a URL object URL url = new URL("http://www.example.com"); // Get the default port for the URL int defaultPortNumber = url.getDefaultPort(); System.out.println("Default Port Number for the URL: " + defaultPortNumber); } }
import java.net.*; public class URLDemo{ public static void main(String[] args) throws Exception{ // Create a URL object URL url = new URL("https://www.example.com"); // Get the default port for the URL int defaultPortNumber = url.getDefaultPort(); System.out.println("Default Port Number for the URL: " + defaultPortNumber); } }In this example, the URL object is created for the "https://www.example.com" URL, which uses the secure HTTPS protocol. The getDefaultPort method is used to retrieve the default port number for the URL connection. Finally, the port number is printed on the console. These examples demonstrate the use of the getDefaultPort method in the URL class to retrieve the default port number for a URL connection. The java.net package is used to provide networking related classes and methods in Java programming language.