How to Create URL Object in Java


In this example, let’s  see how to create a URL object using the protocol and host address and port, and the URL class has a corresponding constructor.

Source Code

package com.beginner.examples;

import java.net.MalformedURLException;
import java.net.URL;

public class CreateURLObject {

	public static void main(String[] args) {
		
		try {
			
			String protocal ="https";
			String host = "www.google.cn";
			String port = "80";
			
			URL url = new URL(protocal,host,port);
			
			System.out.println(url.toString());
			
		} catch (MalformedURLException e) {
			
			e.printStackTrace();
		}
		
	}

}

Output:

https://www.google.cn80

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments