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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

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

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

The specified String is: 58e0a7d7-eebc-11d8-9669-0800200c9a66
The UUID from specified String: 58e0a7d7-eebc-11d8-9669-0800200c9a66


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.