Skip to content
Related Articles

Related Articles

UUID fromString() Method in Java with Examples
  • Last Updated : 27 Dec, 2018

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

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :