Thursday, February 5, 2026
HomeLanguagesJavaUUID fromString() Method in Java with Examples

UUID fromString() Method in Java with Examples

The fromString() method of UUID class in Java is used for the creation of UUID from the standard string representation of the same.

Syntax:

public static UUID fromString(String UUID_name)

Parameters: The method takes one parameter UUID_name which is the string representation of the UUID.

Return Value: The method returns the actual UUID created from the specified string.

Exception: The method throws IllegalArgumentException if an invalid UUID_name is passed.

Below programs illustrate the working of fromString() method:

Program 1:




// Java code to illustrate fromString() method
  
import java.util.*;
  
public class UUID_Demo {
    public static void main(String[] args)
    {
  
        // Get the string
        String UUID_name
            = "5fc03087-d265-11e7-b8c6-83e29cd24f4c";
  
        // Displaying the UUID
        System.out.println("The specified String is: "
                           + UUID_name);
  
        // Creating the UUID
        UUID UUID_1
            = UUID
                  .fromString(UUID_name);
  
        // Displaying the UUID
        System.out.println("The UUID from"
                           + " specified String: "
                           + UUID_1);
    }
}


Output:

The specified String is: 5fc03087-d265-11e7-b8c6-83e29cd24f4c
The UUID from specified String: 5fc03087-d265-11e7-b8c6-83e29cd24f4c

Program 2:




// Java code to illustrate fromString() method
  
import java.util.*;
  
public class UUID_Demo {
    public static void main(String[] args)
    {
  
        // Get the string
        String UUID_name
            = "58e0a7d7-eebc-11d8-9669-0800200c9a66";
  
        // Displaying the UUID
        System.out.println("The specified String is: "
                           + UUID_name);
  
        // Creating the UUID
        UUID UUID_1
            = UUID
                  .fromString(UUID_name);
  
        // Displaying the UUID
        System.out.println("The UUID from"
                           + " specified String: "
                           + UUID_1);
    }
}


Output:

The specified String is: 58e0a7d7-eebc-11d8-9669-0800200c9a66
The UUID from specified String: 58e0a7d7-eebc-11d8-9669-0800200c9a66
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32479 POSTS0 COMMENTS
Milvus
126 POSTS0 COMMENTS
Nango Kala
6849 POSTS0 COMMENTS
Nicole Veronica
11980 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12066 POSTS0 COMMENTS
Shaida Kate Naidoo
6988 POSTS0 COMMENTS
Ted Musemwa
7222 POSTS0 COMMENTS
Thapelo Manthata
6936 POSTS0 COMMENTS
Umr Jansen
6919 POSTS0 COMMENTS