import java.net.URI; public class URIPortNumberExample { public static void main(String[] args) { try { URI uri = new URI("https://www.example.com:8080/index.html"); int port = uri.getPort(); System.out.println("Port number: " + port); } catch (Exception ex) { ex.printStackTrace(); } } }
import java.net.URI; public class URIDefaultPortExample { public static void main(String[] args) { try { URI uri = new URI("https://www.example.com"); int defaultPort = uri.toURL().getDefaultPort(); System.out.println("Default port number: " + defaultPort); } catch (Exception ex) { ex.printStackTrace(); } } }In this example, the getDefaultPort() method is used to get the default port number of the "https" scheme. The output will be "Default port number: 443". Package library: java.net.