Groovy 变量


当前第2页 返回上一页

下面是一个变量声明的例子 -

class Example { 
   static void main(String[] args) { 
      // x is defined as a variable 
      String x = "Hello";
		
      // The value of the variable is printed to the console 
      println(x);
   }
}

当我们运行上面的程序,我们会得到以下结果 -

Hello

变量命名

变量的名称可以由字母,数字和下划线字符组成。 它必须以字母或下划线开头。 大写和小写字母是不同的,因为Groovy,就像Java是一种区分大小写的编程语言。

class Example { 
   static void main(String[] args) { 
      // Defining a variable in lowercase  
      int x = 5;
	  
      // Defining a variable in uppercase  
      int X = 6; 
	  
      // Defining a variable with the underscore in it's name 
      def _Name = "Joe"; 
		
      println(x); 
      println(X); 
      println(_Name); 
   } 
}

当我们运行上面的程序,我们会得到以下结果 -

5 
6 
Joe 

我们可以看到x和X是两个不同的变量,因为区分大小写,在第三种情况下,我们可以看到_Name以下划线开头。

打印变量

您可以使用println函数打印变量的当前值。下面的示例显示了如何实现这一点。

class Example { 
   static void main(String[] args) { 
      //Initializing 2 variables 
      int x = 5; 
      int X = 6; 
	  
      //Printing the value of the variables to the console 
      println("The value of x is " + x + "The value of X is " + X);  
   }
}

当我们运行上面的程序,我们会得到以下结果 -

The value of x is 5 The value of X is 6 

标签:Groovy

返回前面的内容

相关阅读 >>

Groovy 概述

Groovy 日期和时间

Groovy xml

Groovy 字符串

Groovy 基本语法

Groovy 循环

Groovy 运算符

Groovy 映射

Groovy 注释

Groovy 单元测试

更多相关阅读请进入《Groovy》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...