基础变量知识:
salesforce如果简单的说可以大概分成两个部分:Apex,VisualForce Page.
其中Apex语言和java许多的语法相似,这里总结的是一些简单的Apex的变量等知识。
有如下几种常用的基本变量Integer,String,Decimal,Double,Long,Boolean,ID。
集合常用的对象:List
时间日期常用对象:Datetime,Time,Date。
其他:Object,sObject(与数据库相关)
与JAVA一个最大的区别是:Apex中基本对象的初始值均为null。
代码:
```
Integer a;
a += 1;
System.debug(a);
```
在java中此种写法是可以的,因为int类型初始值为0,a+=1以后则a变成1.但是在Apex中因为a初始值为null。
所以a+=1在运行时会抛出NullPointerException
当然,有一点比较有意思的事情,代码:
```
Integer a;
System.debug(a+'1');
```
此种方法输出的结果则为null1。起始这也不奇怪,因为Apex也是基于java拓展的,如果看java编程思想了解底层的null的toString()方法处理也就知道了,当执行输出操作时,一个变量为null时,他的toString方法则返回'null'字符串。
一、基本变量:
1、Integer
Integer表示一个32位整数的对象,取值范围为-2^31 -- 2^31.
Integer主要有两个方法:
```
Integer goodsCount = 1;
System.debug('将Integer值转成String: ' + goodsCount.format());
Integer goodsCount = Integer.valueOf('1');
```
2、Long
Long类型表示一个64位整数的对象,取值范围为-2^63--2^63-1.
Integer类型可以直接转换成Long类型,Long类型在不超过范围情况下可以通过intValue()方法转成Integer类型。
以下为Long类型部分主要方法:
```
Integer num = 123;
Long code = num;
//Integer类型可以直接转成Long类型
System.debug('Long类型转成String类型:' + code.format());
System.debug('将Long类型转成Integer类型:' + code.intValue());
Long codeLong = Long.valueOf('123');
```
3、ID
ID类型可以用任何一个符合规则的18位字符表示,如果你设置ID字符为15位,则将字符自动扩展成18位。不符合规则的ID字符在运行时则运行时异常。
以下为ID的主要方法:
```
String idStr = '111111111111111111';
ID id = ID.valueOf(idStr);
Boolean isEquals = id.equals(idStr);
```
4、Decimal
简单的来说,Decimal变量的意思为包含小数点的32位数就是Decimal,很像java中的float类型变量。
以下为Decimal的部分主要方法用来了解Decimal的功能:
```
Decimal priceDecimal = -4.50;
System.debug('小数的绝对值为:' + priceDecimal.abs());
System.debug('priceDecimal除以10小数点保留两位小数:' + priceDecimal.divide(10,2));//-0.45
System.debug('将priceDecimal转换成Double类型'+ priceDecimal.doublevalue());
System.debug('Decimal转成String类型' + priceDecimal.format());
System.debug('将Decimal转成Integer类型' + priceDecimal.intValue());
System.debug('将Decimal转成Long类型' + priceDecimal.longValue());
System.debug('priceDecimal平方值为:' + priceDecimal.pow(2));
System.debug('priceDecimal数字总数为:'+ priceDecimal.precision());
//2 -4.5有4和5
System.debug('priceDecimal四舍五入Long类型值为:'+ priceDecimal.round());
System.debug('priceDecimal小数点后的位数为:' + priceDecimal.scale());
System.debug('设置priceDecimal的小数为2位'+ priceDecimal.setScale(2));
System.debug('移除priceDecimal小数点后的0以后的值为:'+ priceDecimal.stripTrailingZeros());
System.debug('不使用科学记数法转换成String类型'+ priceDecimal.toPlainString());
Long priceL = 12345;
Double priceD = 123.456;
String priceS = '12345';
Decimal d1 = Decimal.valueOf(priceL);
Decimal d2 = Decimal.valueOf(priceD);
Decimal d3 = Decimal.valueOf(priceS);
```
5、Double
Double变量为包含小数点的64位数,很像 java中的Double类型变量。
Decimal类型变量可以直接转换成Double类型变量,Double类型在不超过范围情况下可以通过以下为Double的部分主要方法:
```
Double price = 34.5678;
String doubleString = '3.89';
System.debug('将字符串转换成Double' + Double.valueOf(doubleString));
Long priceLong = price.round();
System.debug('通过round方法将double转换成Long类型值为:' + priceLong);
Integer priceInteger = price.intValue();
System.debug('将double转换成Integer类型值为:' + priceInteger);
Long priceLongByLongValue = price.longValue();
System.debug('将double转换成Long类型值为:' + priceLongByLongValue);
```
未完待续。。。



