September 26, 2016 by LanceShi
2.2 Variable types and conversions
In our last post, we mentioned that Apex is a strong typed language, which means each variable must have a type and that cannot be changed. In this post, I will show you a number of frequently used variable types in Apex and a few basic operations. Here comes the code:
String greetings = 'Hello'; Integer i = 10; //i is a natural number which doesn't contain a decimal point Decimal dec = 12.53; // Decimal number is a real number which contains a decimal point Double doubleNumber = 32.13; //Double number is also a real number. But it is fixed length Boolean areWeLearning = true; Date currentDate = Date.today(); Datetime dt = Datetime.now();
The above code contains all the types I want you to understand now. They are basic variable types in Apex. There are some variable types for sure. But let’s don’t worry about them for now.
A bit explanation
String is a text variable which holds the information of a text. Strings are enclosed in two single quotes(‘) in Apex. Please note Apex only takes single quote. Double quote(“) is not used to enclose strings in Apex.
Integer means a natural number variable. It can be both positive and negative.Integers have a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647. You won’t easily exceed it now. But when you get more used to programming, please keep in mind don’t exceed that.
Decimal and Double are both real numbers. They both can hold numbers with decimal points. The difference is Decimal is a flexible precision and Double is fixed precision, which impacts how much space it would using. If you don’t understand the difference. It is totally fine.
A Boolean variable only holds two values – true or false. And of course, it can also be null, like any variable else, which means no value.
Date and Datetime variables means what it means.
When a value is assigned to a variable, if the value is not of the type of the variable, a type-conversion will be taken place.
There are basically three scenarios:
- The variable cannot accept value. For example, Integer i = ‘Hello’. This will result in an error.
- The variable can be converted and assigned without losing any information. For example, Decimal d = 11; d will be 11.0 in this case. An implicit conversion automatically take place.
- The variable can be converted. But there will be a risk in losing certain information. We will need explicit type-conversion in this case.
Below is the code:
//Integer i = 'Hello'; //This will result in error Double d = 11; //No issue Decimal realNumber = 12.32 Integer i = (Integer)realNumber; System.debug(i);
i will display as 12 in the above case.
The highlighted line is an example of explicit type-conversion. The grammar is ([Type])value. You put your converted to type before the value you want to covert. The conversion might have some risk of losing information. So the explicit conversion is to tell the compiler that I am fully aware of the risk, but I know what I am doing, so let me convert it anyway.
Define some more variables of each time, and do a little bit type conversion by yourself!