How to Convert a String to URL in Java? Last Updated : 15 May, 2024 Comments Improve Suggest changes 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 URL in Java? 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 How to Convert a String to a Character in Java? String and char are fundamental and most commonly used datatypes in Java. A String is not a primitive data type like char. To convert a String to char in Java, we have to perform character-based operations or have to process individual characters.In this article, we will learn how to convert a Strin 3 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 Convert a String to a ByteBuffer in Java In Java, ByteBuffer can be used to perform operations at the Byte level one more thing is this class provides different types of methods for reading writing, and manipulating bytes in a structured way only. In this article, we will learn about String to ByteBuffer in Java. Java Program to Convert St 4 min read Like