Apex Programming

Data Types Overview

Apex Data Types Overview:

Apex  Data Types supports various data types, including a data type specific to Sales force-the sObject type.

  • A primitive, such as an Integer, Double, Long, Date, Datetime, String, ID, Boolean, among others.
  • An sObject either as a generic sObject or as a specific sObject, such as an Account, Contact, or MyCustomObject_c (you’ll learn more about sObjects in a later unit.)
  • A collection, including:
  • A list (or array) of primitives, sObjects, user defined objects, objects created from Apex classes, or collections
  • A set of primitives
  • A map from a primitive to a primitive, sObject, or collection
  • A typed list of values, also known as an enum
  • User-defined Apex classes
  • System-supplied Apex classes

 

 

Primitive Data Types and Variables

Apex has a number of primitive data types

  • They store text values such as a name or an address.
  • Boolean: Boolean values hold true or false values and you can use them to test whether a certain condition is true or false
  • Time, Date and Datetime: Variables declared with any of these data types hold time, date, or time and date values combined.
  • Integer, Long, Double and Decimal: Variables declared with any of these data types hold numeric values.
  • Null variables: Variables that you don’t assign values to.
  • Enum: An enumeration of contant values.

String

Use the string data type when you need to store text values, such as a name or an address. Strings are sets of characters enclosed in single quotes. For example, ‘I am string’. You can create a string and assign it to a variable simply by executing the following:

String my Variable= ‘I am string.’;

The previous example creates an instance of the Stringclass, represented by the variable myVariable, and assigns it a string value between single quotes.

You can also create strings from the values of other type, such as dates, by using the String static method valueOf(). Execute the following:

Date myDate= Date.today();

String myString= String.valueOf(myDate); System.debug(myString);

The + operator acts as a concatenation operator when applied to strings. The following results in a single string being created:

System.debug( ‘I am a string’+ ‘ cheese’);

The== and != operators act as a case insensitive comparisons. Execute the following to confirm that both the comparisons below return true:

String x = ‘ I am a string’;

String y = ‘I AM A STRINH’;

String z = ‘Hello!’;

System.debug (x==y);

System,debug (x !=z);

 

Lert’s take a look at what each method of String does.

  • The endsWithmethod returns true because the string ends with the same string as that in the argument.
  • The lenghtmethod returns the length of the string.
  • The substringmethod produces a new string strating from the character specified in the first index argument, counting from zero, through the second argument.
  • The replaceAllmethod replaces each substring that matches a regular expression with the specified replacement.

 

Boolean and Conditional Statements

Declare a variable with the Boolean data type when it should have a true or falsevalue. String operators return a Boolean value based on the result of the string comparison. You can also simple create a variable and assign it a value:

Boolean isLeapYear = true;

There are a number of standard operators on Booleans. For example, all of these statements evaluate to false:

Boolean iAmFalse = !true;

Boolean iAmFalse2 = iAmFalse && true;

Boolean iAmFalse3 = iAmFalse l l false;

 

Time, Date, and Datetime

There are three data types associated with dates and times. The Time data type stores times (hours, minutes, second and milliseconds). The Date data type stores dates (year, month and day). The Datetime data type stores both dates and times.

Each of these classes has a newInstance method with which you can construct particular date and time values. For example, execute the following:

Date myDate = Date.newinstance(1960, 2, 17);

Time myTime = Time.newInstance(18, 30, 2, 20); System.debug(myDate);

System.debug(myTime);

Output is:

1960-02-17 00:00:00

18:30:02.020Z

The Date data type does hold a time, even though it’s set to 0 by default. You can also create dates and times from the current clock:

 

Datetime myDateTime = datetime.now();

Date today = Date.today();

The date and time classes also have instance method for converting from one format to another. For example:

Time t = DateTime.now().time();

Datetime has the addHours, addMinutes, dayOfYear, timeGMT methods and many others. Execute the following:

Date myToday  = Date.today();

Date myNext30 = myToday.addDays(30);

System.debug(‘myToday = ‘+ myToday); System.debung(‘myNext30=  ‘+ myNext30);

Integer, Long, Double and Decimal

To store numeric values in variables, declare your variables with one of the numeric data types: Integer, Long, Double And Decimal.

Integer: A 32-bit number that doesn’t include a decimal point

Long: A 64-bit number that doesn’t include a decimal point.

Double: A 64-bit number that includes a decimal point.

Decimal:

A number that includes a decimal point. Decimal is an arbitrary precision                                              number. Currency fields are automatically assigned the type Decimal.

Execute the following to create variables of each numeric type.

Integer i = 1;

Long I = 2147483648;

Double d = 3. 14159;

Decimal dec = 19.23;

You can use the valueOfstatic method to cast a string to a numeric type. For example, the following creates an Integer from string ‘10’, and then adds 20 to it.

Integer countMe = Inyeger.valueof(‘10’) + 20;

Null Variables

If you declare a variable and don’t initialize it with a value, it will be null. Null means the absence of a value. You can also assign nullto any variable declared with a primitive type. For example, both of these statements result in a variable set to null:

Boolean x = null;

Decimal d;

nums

Use enumerations (enums) to specify a set of constants. Define a new enumeration by using the enumkeyword followed by the list of identifiers between curly braces. Each value in the enumeration corresponds to an Integer value, starting from zero and incrementing by one from left to right. Beacuse each value corresponds to a constant, the identifiers are in upper case. For example, this example defines an enumeration called Season that contains the four seasons:

public enum Season {WINTER, SPRING, SUMMER, FALL}

The Integer value of WINTERis0, SPRING1, SUMMER2, FALL3.

Once you define your enumeration, you can use the new enum type as a data type for declaring variables.

Public enum Season {WINTER, SPRING, SUMMER, FALL}

Season s = Season.SUMMER;

If (s == Season.SUMMER( {

System.debug(s);

} else {

System.debug(‘Not summer.’);

}

Comments

Comments are lines of text that you add to your code to describe what it does.

Apex has two forms of comments.

The first uses the // token to mark everything on the same line to the right of the token as comment.

The second encloses a block of text, possibly across multiple lines, between the /* and */ tokens.

Hits: 269

Share Post

By Himanshu Rana

My Name is Himanshu Rana, 23 Years young, born and grow up in Ghaziabad, India. A High Spirited Salesforce Admin, Developer and a Blogger. I currently work at Wakencode Technologies,

Leave a Reply

Your email address will not be published. Required fields are marked *