How to Convert a String to URL in Java? Last Updated : 15 May, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In Java, a string is a sequence of characters. A URL, which is short for Uniform Resource Locator, refers to a web resource that specifies its location on a computer network and provides the mechanism for retrieving it. To represent a URL in Java, we use the java.net.URL class. In this article, we will learn how to convert a normal string to a URL in Java. Convert a String to a URL in JavaConvert a String to a URL in Java Using the URL Class Constructor that accepts a String Parameter as an Input and Returns a URL as an Output. Let's understand How we can utilize the URL class constructor to convert a string to a URL in Java with the help of an example: Java Program to Convert a String to a URLThe following program demonstrates how we can convert a string to a URL in Java: Java // Java Program to Convert a String to a URL import java.net.URL; // Driver Class public class StringToURLConverter { // Main Function public static void main(String[] args) { try { // Define the string you want to convert to URL String urlString = "https://www.example.com"; // Create a new URL object by parsing // the string representation URL url = new URL(urlString); // Print the URL object System.out.println("URL: " + url); } catch (Exception e) { // Handle any exceptions that might // occur during URL parsing e.printStackTrace(); } } } OutputURL: https://www.example.com Complexity of the above method:Time Complexity: O(N), where N is the length of the string.Auxiliary Space: O(1), as no extra space is used. Explanation of the above program:We have imported the java.net.URL class which will be used to convert the string to URL.Declared the string which we want to convert to URL inside the try block.Created a new URL object url to store the converted string.Converted the string to URL using the URL constructor.Returned the url as an output. Comment More infoAdvertise with us Next Article How to Convert a String to a Path in Java? G gaurav472 Follow Improve Article Tags : Java Java Programs Java-URL Java Examples Practice Tags : Java Similar Reads How to Convert a String to a UUID in Java? A Universally Unique Identifier (UUID) is a 128-bit label utilised for information in computer systems. The term Globally Unique Identifier (GUID) is also used especially in Microsoft systems. UUIDs are regularised by the Open Software Foundation (OSF) as part of the Distributed Computing Environmen 2 min read How to Convert a String to a Path in Java? In Java, we can convert a String representation of a file or directory path into a path object using the Paths utility class. It is a part of the java.nio.file package. The Paths class provides a method called get(String file path location) that converts a sequence of strings representing a path int 2 min read Convert a String to an InetAddress in Java InetAddress is one of the Java classes which is available in java.net package. The InetAddress class contains a lot of built-in methods for handling networking-related functions in Java. In our case we need to convert a String to an InetAddress means IP Address. Normally an IP Address format looks l 4 min read Java Program to Get Components of a URL Java Networking: As java programs are written to be executed across multiple devices whereby multiple devices are meant computers present in remote locations. So both devices can communicate be present in the same locations or different locations connected with some network. For java networking 'net 3 min read Java Program to Get Connected to a Web Server In today's interconnected world, connecting to web servers is a fundamental skill for software developers. Whether you're building a web application that needs to fetch data from external sources, testing a web service, or even performing web scraping for research, knowing how to interact with web s 4 min read Java Program to Create String from Contents of a File A File is a computer resource which is deployed to store different types of data such as text, image, video, to name a few. It is basically a collection of data bound to a single entity. While using your computer, it becomes essential to be able to deal with files and in this article we will be lear 6 min read Like