code_junkie

Groovy: what's the purpose of “def” in “def x = 0”?

Nov 11th, 2011
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. def x = 0
  2. def y = 5
  3.  
  4. while ( y-- > 0 ) {
  5. println "" + x + " " + y
  6. x++
  7. }
  8.  
  9. assert x == 5
  10.  
  11. x = 1
  12. assert x == 1
  13. assert this.binding.getVariable("x") == 1
  14.  
  15. def y = 2
  16.  
  17. assert y == 2
  18.  
  19. try {
  20. this.binding.getVariable("y")
  21. } catch (groovy.lang.MissingPropertyException e) {
  22. println "error caught"
  23. }
  24.  
  25. x = 1
  26. def y = 2
  27.  
  28.  
  29. public bar() {
  30. assert x == 1
  31.  
  32. try {
  33. assert y == 2
  34. } catch (groovy.lang.MissingPropertyException e) {
  35. println "error caught"
  36. }
  37. }
  38.  
  39. bar()
  40.  
  41. // Groovy imports java.io.* and java.util.* automatically
  42. // but not java.nio.*
  43.  
  44. import java.nio.channels.*
  45.  
  46. class Foo {
  47. public void bar() {
  48. FileChannel channel = new FileInputStream('Test.groovy').getChannel()
  49. println channel.toString()
  50. }
  51. }
  52.  
  53. new Foo().bar()
  54.  
  55. // Groovy imports java.io.* and java.util.* automatically
  56. // but not java.nio.*
  57. class Foo {
  58. public void bar() {
  59. def channel = new FileInputStream('Test.groovy').getChannel()
  60. println channel.toString()
  61. }
  62. }
  63.  
  64. new Foo().bar()
Add Comment
Please, Sign In to add comment