How do you use URL/URI with a simple code example?

In Java, working with URLs and URIs is straightforward thanks to the `java.net` package. Below is a simple example of how to create and use URL and URI classes.

import java.net.URL; import java.net.URISyntaxException; public class UrlUriExample { public static void main(String[] args) { try { // Creating a URL object URL url = new URL("https://www.example.com"); System.out.println("URL: " + url); System.out.println("Protocol: " + url.getProtocol()); System.out.println("Host: " + url.getHost()); // Creating a URI object URI uri = url.toURI(); System.out.println("URI: " + uri); } catch (Exception e) { e.printStackTrace(); } } }

java URL URI java.net package programming example