Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create an empty java.util.UUID object?

Tags:

I don't know why I can't find the answer to this, but I need to pass a blank UUID object in one of my functions to represent a lack of UUID. What is the UUID analagous form of

val x: ""  

, which would be an empty string. I'm essentially trying to get an empty UUID. I tried

UUID.fromString("") 

but received an error, as you need a valid UUID string.

EDIT: I am implementing this in Scala.

like image 608
jeffrey Avatar asked Oct 29 '14 17:10

jeffrey


People also ask

Can UUID be empty?

You can't use an empty String , as it does not conform to the UUID format, described here. But the usage of that would be arbitrary, and it would be much better to signify the absence or lack of a UUID with Option . Thanks!

Can UUID be null in Java?

You can create a nil UUID from a hex string of zeros in the canonical format.


1 Answers

Let me preface this by saying it would be much better to use Option[UUID] instead, with None representing an empty UUID.

You can't use an empty String, as it does not conform to the UUID format, described here.

You could use

UUID.fromString("00000000-0000-0000-0000-000000000000") 

Which would be the same as

new UUID(0L, 0L) 

But the usage of that would be arbitrary, and it would be much better to signify the absence or lack of a UUID with Option.

like image 129
Michael Zajac Avatar answered Sep 21 '22 12:09

Michael Zajac