0% found this document useful (0 votes)
37 views

Data Types in Apex

Uploaded by

rjohar369
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Data Types in Apex

Uploaded by

rjohar369
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Data Types in Apex

Data type:

Data types are used to define what kind of data a variable can hold.

Apex data types are categorized into:

1.Primitive data types.

2.Non-primitive data types.

Primitive data types:

Integer:A 32-bit number that doesn’t include a decimal point. Integers have a
minimum value of -2,147,483,648 and a maximum value of 2,147,483,647.

Example: Integer count = 100;

Long: A 64-bit number that doesn’t include a decimal point. Longs have a
minimum value of -263 and a maximum value of 263-1. Use this data type when
you need a range of values wider than the range provided by Integer.

Example: Long largeNumber =123456789L;

Double: A 64-bit number that includes a decimal point. Doubles have a minimum
value of -263 and a maximum value of 263-1.

Example: Double price = 9.99;

Blob: Stores binary data such as files or attachments.

Example: Blob image = Blob.valueOf('Sample image data');

N.Veera Raghavamma
Decimal:Stores decimal numbers with high precision, commonly used for
currency calculations.

Example: Decimal totalAmount = 100.25;

Boolean:Stores true/false values.

Example: Boolean isActive = true;

Date: Stores a date without time.

Example: Date today = Date.today();

Time:Stores a time of day without a date.

Example:Time startTime = Time.newInstance(8, 30, 0, 0);

Datetime:Stores both date and time.

Example: Datetime currentDateTime = Datetime.now();

String:Stores a sequence of characters (text).

Example: String name = 'John';

ID:Stores Salesforce object IDs (15-character or


18-character strings).

Example: Id recordId = '001xx000003DGbqAAG';

N.Veera Raghavamma
Non-primitive data types:

● Collections

Apex supports three main types of collections for storing multiple values in a single
Variable.

List: An ordered collection of elements that can be of any data type.

Example:

List<String> names = new List<String>{'John', 'Jane', 'Doe'};

Set: An unordered collection of unique elements, which can be of any data type.

Example:

Set<Integer> uniqueNumbers = new Set<Integer>{1, 2, 3};

Map: A collection of key-value pairs, where each key is unique, and both keys and
values can be any data type.

Example:

Map<String, Integer> nameToAge = new Map<String, Integer>{'John'


=> 30, 'Jane' => 2};

parameterized typing:

Apex, in general, is a statically-typed programming language, which means users must


specify the data type for a variable before that variable can be used.

Example:

List<String> my LIst=New List<String> ();

N.Veera Raghavamma
● sObject Data Types

sObject: Represents any Salesforce object (both standard and custom objects) in
Apex. All Salesforce records (e.g., Account, Contact) are treated as sObject types.

Example:

Account acc = new Account(Name = 'Ms Corporation');

The sObject type is the parent class for all standard and custom objects.

Standard sObjects: Examples of standard Salesforce objects:

○ Account acc;
○ Contact con;
○ Lead lead;
○ Opportunity opp;

Custom sObjects: Objects that you define in Salesforce to store specific business
data.

MyCustomObject__c customObj = new MyCustomObject__c(Name =


'Custom Record');

● Enums:

Enum:

Represents a fixed set of named constants, allowing predefined values. Enums are
commonly used to represent discrete values.

N.Veera Raghavamma
Example:

public enum Status {


NEW, IN_PROGRESS, COMPLETE
}

Status currentStatus = Status.NEW;

● Classes, Interfaces, and Objects:

Apex is object-oriented, so you can define custom classes and


interfaces:

Classes: Define custom classes that can contain properties,


methods, and constructors.

Example:

public class MyClass {

public String name;

public MyClass(String name)

this.name = name;

MyClass obj = new MyClass('Sample');

N.Veera Raghavamma
Interfaces: Defines methods without implementation that a class
must implement.

public interface MyInterface

void myMethod();

Comparison of Primitive types and Non-Primitive types:

Feature Primitive Data Types Non-Primitive Data Types

Definition Basic data types built into More complex data types
the language built from primitive types or
user-defined.

Examples in Apex Integer, Double, List, Set, Map,


Boolean, String, Date sObject, custom classes

Value or Reference Directly holds the value Holds a reference to the


(value type). actual data (reference
type).

Inheritance Does not support Supports inheritance and


inheritance. polymorphism.

Custom Definition Cannot be defined by Can be created by users


users (custom objects, custom
classes)

Memory Storage Stored directly in memory. Stored as references to


objects in the heap

Size and Flexibility Fixed Size. Very in Size and can be


dynamically resized.

N.Veera Raghavamma

You might also like