HomeLanguagesJavaUUID toString() Method in Java with Examples

UUID toString() Method in Java with Examples

The toString() method of UUID class in Java is generally used to get the string representation of this UUID.

Syntax:

public String toString()

Parameters: This method does not take any parameter.

Return Value: This method returns a String value which is the string representation of this UUID.

Below programs illustrate the working of toString() method:

Program 1:




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


Output:

UUID: 58e0a7d7-eebc-11d8-9669-0800200c9a66
The string representation is: 58e0a7d7-eebc-11d8-9669-0800200c9a66

Program 2:




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


Output:

UUID: 5fc03087-d265-11e7-b8c6-83e29cd24f4c
The string representation is: 5fc03087-d265-11e7-b8c6-83e29cd24f4c
RELATED ARTICLES

2 COMMENTS

Most Popular

Dominic
32649 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7023 POSTS0 COMMENTS
Nicole Veronica
12164 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12255 POSTS0 COMMENTS
Shaida Kate Naidoo
7156 POSTS0 COMMENTS
Ted Musemwa
7420 POSTS0 COMMENTS
Thapelo Manthata
7114 POSTS0 COMMENTS
Umr Jansen
7118 POSTS0 COMMENTS