Enum – Variables Data Types – Apex Programming – Salesforce Certified Platform Developer I Study Guide

Enum: An enum in Salesforce Apex is an abstract data type that allows you to define a set of values that an identifier can take on. Each value in the enum is given a unique identifier, and you can use these identifiers to reference the values in your code. Enums are used to define a predefined set of values that a variable can take, making your code more readable and maintainable. Enums are often used to represent specific values within your Apex code.

Here’s an example of how you might define and use an enum in Salesforce Apex:

public enum Season {

WINTER, SPRING, SUMMER, FALL

}

Season currentSeason = Season.SUMMER; System.debug(‘The current season is ‘ + currentSeason);

You cannot change the data type of a variable after it has been defined. However, you can perform type casting and data type conversion operations.

Casting in Apex refers to the process of converting one data type to another. It allows you to treat an object as an instance of a different class or to convert a primitive data type to another primitive data type. Casting is useful when you need to perform operations or assignments that require a specific data type.

Explicit converting is when you manually convert one data type to another. It is used when there is a possibility of data loss or when you want to be explicit about the conversion.

Here’s an example of explicit conversion in Apex:

Double decimalNum = 10.53;

Integer intValue = (Integer) decimalNum;

System.debug( intValue);

In the example above, we explicitly convert the Double variable decimalNum to an Integer. This allows us to assign the value of decimalNum to intValue, but note that there may be a loss of precision.

Implicit converting, also known as automatic converting, is when the conversion happens automatically without the need for explicit syntax. It occurs when there is no possibility of data loss or when the conversion is well-defined.

Here’s an example of implicit converting in Apex:

Integer num = 10;

Long bigNum = num;

System.debug (num);

In the example above, we assign the value of the Integer variable num to the Long variable bigNum without explicitly converting it. This is possible because there is no loss of precision when converting from Integer to Long.

Converting from Integer to String:

Integer myInteger = 42;

String myString = String.valueOf(myInteger);

  • Explicit converting from Integer to String System.debug(myInteger);

Converting from String to Integer:

String myString = ’42’;

Integer myInteger = Integer.valueOf(myString);

  • Explicit converting from String to Integer System.debug(myString);

In general, variables should be declared outside of curly braces if they need to be accessible outside of the block in which they are declared.

Integer outsideVariable = 5; System.debug(outsideVariable); // Valid

However, there may be cases where it makes sense to declare variables inside curly braces, such as when you want to limit the scope of a variable to a specific block of code. Variables declared inside curly braces have limited visibility, known as block scope. They are only accessible within the specific set of curly braces where they are defined.

{

Integer insideVariable = 10; System.debug(insideVariable); // Valid

}

System.debug(insideVariable);

// Error: Variable does not exist: insideVariable

In Apex programming, the terminology of “casting” can be used in different contexts. Upcasting and downcasting refer to the conversion of object references between classes that are related through inheritance. Upcasting involves treating an object as an instance of its superclass, while downcasting involves treating an object as an instance of its subclass. On the other hand, casting of primitive data types involves converting primitive data types to other primitive data types, as demonstrated in our examples.