title: What is the default value for Union in Java for Apache Avro?
body: <p>I'm trying to set the default value for a field (using Java) and I've got a nasty exception:</p>
  
  <blockquote>
    <p>Exception in thread "main" org.apache.avro.AvroTypeException: Invalid default for field first: "Andy" not a ["null","string"]</p>
    
    <p>at org.apache.avro.Schema.validateDefault(Schema.java:1542)</p>
  </blockquote>
  
  <p>Documentation of <a href="https://avro.apache.org/docs/1.8.1/spec.html#Unions" rel="nofollow noreferrer">Apache Avro: Unions</a> says :</p>
  
  <blockquote>
    <p>Unions, as mentioned above, are represented using JSON arrays. For
    example, ["null", "string"] declares a schema which may be either a
    null or string.</p>
    
    <p>(Note that when a default value is specified for a record field whose
    type is a union, the type of the default value must match the first
    element of the union. Thus, for unions containing "null", the "null"
    is usually listed first, since the default value of such unions is
    typically null.)</p>
  </blockquote>
  
  <p>Would anyone please tell me why the type of the default value must match the first element of the union ?</p>