Difference between Groovy def and Java Object?

前端 未结 2 956
野的像风
野的像风 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.

    0 讨论(0)
  • 2021-02-13 21:47

    You can experiment with groovy in the groovy web console http://groovyconsole.appspot.com/

    Your initial groovy date example works.

    0 讨论(0)
提交回复
热议问题