博客
关于我
Groovy In Action 笔记 (3) -- 基本数据类型
阅读量:619 次
发布时间:2019-03-13

本文共 1635 字,大约阅读时间需要 5 分钟。

Groovy中所有类型均为引用类型。即使是Java中的8种Primitive Type,groovy中背后使用的都是对应的包装类。没有boxing,unboxing操作.

一下为一些基本类型操作

//Integerdef age = 10println("age++ " + (age++)) // age++ 10, 先输出,再自增assert age == 11println("++age " + (++age)) // ++age 12,先自增,再输出assert age == 12assert 12.toString() == age.toString() // 可直接对常量使用方法assert 12.8.toInteger() == 12 // 可直接对常量使用方法//floatdef weight_f = 1.2fdef weight_F = 1.2Fassert weight_f.class.name == Float.nameassert weight_F.class.name == Float.name//doubledef weight_d = 1.2ddef weight_D = 1.2Dassert weight_d.class.name == Double.nameassert weight_D.class.name == Double.name//longdef meter_l = 1000Lassert meter_l.class.name == Long.name//bigIntegerdef universe_age_g = 1000Gassert universe_age_g.class.name == BigInteger.name//bigDecimaldef decimal_default = 1.2  ///注意默认为 bigDecimal类型,和Java不同def decimal_b= 1.2gassert decimal_default.class.name == BigDecimal.nameassert decimal_b.class.name == BigDecimal.name// operatorsdef compare_a = 20def compare_b = 10assert compare_a > compare_bassert compare_b < compare_aassert (compare_a <=> compare_b) == 1  // <=> 相当于 compareTo函数assert 5 / 2 == 2.5 //默认bigDecimalassert 3 % 2 == 1 //取余数 a.mod(b)assert 5.intdiv(2) == 2 //整除assert compare_a >> 2 == 5 //右移两位,相当于除以4assert compare_a << 2 == 80 //左移两位,相当于乘以4//动态数据类型变化 --- Groovy中所有类型均为 引用类型。 即使是Java中的8种Primitive Type,groovy中背后使用的都是对应的包装类。没有boxing,unboxing操作def type_variant = 100assert type_variant.class.name == Integer.nametype_variant = 100fassert type_variant.class.name == Float.nametype_variant = 100dassert type_variant.class.name == Double.nametype_variant = "STRING"assert type_variant.class.name == String.name

 

转载地址:http://napaz.baihongyu.com/

你可能感兴趣的文章
MySql的连接查询
查看>>
mysql的配置文件参数
查看>>
MySQL的错误:No query specified
查看>>
mysql监控工具-PMM,让你更上一层楼(上)
查看>>
mysql监控工具-PMM,让你更上一层楼(下)
查看>>
MySQL相关命令
查看>>
mysql社工库搭建教程_社工库的搭建思路与代码实现
查看>>
Warning: Can't perform a React state update on an unmounted component. This is a no-
查看>>
mysql笔记 (早前的,很乱)
查看>>
MySQL笔记:InnoDB的锁机制
查看>>
mysql第一天~mysql基础【主要是DDL、DML、DQL语句,以及重点掌握存存引擎、查询(模糊查询)】
查看>>
mysql第二天~mysql基础【查询排序、分页查询、多表查询、数据备份与恢复等】
查看>>
MySQL简介和安装
查看>>
MySQL简单查询
查看>>
MySQL管理利器 MySQL Utilities 安装
查看>>
MySQL篇(管理工具)
查看>>
mysql类型转换函数convert与cast的用法
查看>>
mysql系列一
查看>>
MySQL系列之数据类型(Date&Time)
查看>>
MySQL系列之数据类型(Date&Time)
查看>>