Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Groovy: Determine type of an object

Creating an object called obj of type java.util.ArrayList which implements the java.util.List interface.

instanceof checks if obj is the subclass of some class.

getClass returns the exact type of an object.

def obj = ["question", 42, 'answer']

println obj.getClass()   // class java.util.ArrayList

println (obj instanceof List)                   // true
println (obj instanceof java.util.ArrayList)    // true

println (obj.getClass() == List)                // false
println (obj.getClass() == java.util.ArrayList) // true

timestamp: 2018-06-01T07:30:01 tags:

  • instanceof
  • getClass