Difference between Groovy def and Java Object?

前端 未结 2 955
野的像风
野的像风 2021-02-13 20:45

I\'m trying to figure out the difference between

Groovy:

def name = \"stephanie\"

Java:

Object name = \"stephanie\"
         


        
2条回答
  •  情话喂你
    2021-02-13 21:38

    Per se, there is not much difference between those two statements; but since Groovy is a dynamic language, you can write

    def name = "Stephanie"
    println name.toUpperCase() // no cast required
    

    while you would need an explicit cast in the Java version

    Object name = "Stephanie";
    System.out.println(((String) name).toUpperCase());
    

    For that reason, def makes much more sense in Groovy than unfounded use of Object in Java.

提交回复
热议问题