Data Types in Apex: A Beginner’s Guide

When you start learning any programming language, one of the first concepts you’ll encounter is data types. If you’re new to programming, understanding data types is crucial because they define what kind of data a variable can hold and what operations can be performed on that data. In this article, we’ll break down what data types are, why they matter in strongly-typed languages like Apex, and how you can use them effectively.

What is a Data Type?

A data type defines the kind of value a variable can hold in a program. Think of it like a label that tells the computer whether a variable will store a number, text, or even more complex data like a list of items.

For example, in Apex, you might have variables like this:

Integer age = 25;
String name = 'John Doe';

Here:

  • Integer is the data type for age, meaning it will store a whole number.
  • String is the data type for name, meaning it will store a piece of text.

Why Data Types Are Important in Strongly-Typed Programming Languages

Apex is a strongly typed language, which means every variable must be declared with a specific data type. This is different from dynamically typed languages like JavaScript or Python, where the same variable can hold different types of data at different times.

In Apex, once a variable is assigned a data type, it can only hold values of that type. For example, if you declare a variable as an integer, it can only store whole numbers. This strict typing reduces the risk of errors because it forces you to keep track of what kind of data you’re working with.

How Data Types Affect Operations

The data type of a variable affects the kind of operations you can perform on it. For example:

  • You can add, subtract, multiply, or divide numbers if the variable is an Integer or Decimal.
  • You can concatenate (join) pieces of text if the variable is a String.

Let’s look at an example in Apex:

Integer x = 10;
Integer y = 5;
System.debug(x + y); // Outputs 15

String firstName = 'John';
String lastName = 'Doe';
System.debug(firstName + ' ' + lastName); // Outputs 'John Doe'

As you can see, you can perform different operations based on the data type. If you tried to add a string and a number together, Apex would throw an error because the operation doesn’t make sense.

Primitive vs Non-Primitive Data Types

In Apex, there are two main categories of data types: Primitive and Non-Primitive.

  1. Primitive Data Types: These are the basic building blocks in Apex, like numbers, text, and true/false values. They are simple and straightforward.
  2. Non-Primitive Data Types: These are more complex structures like objects, arrays, or collections. These allow you to store and manipulate more complicated data structures, like a list of accounts or custom objects.

For beginners, focusing on primitive data types is a great way to start your journey in understanding how data works in programming.

Types of Primitive Data Types in Apex

Let’s explore the different types of primitive data types in Apex and some common operations you can perform on them.

Integer: Used to store whole numbers.

  • Example: Integer age = 25;
  • Common operations: addition, subtraction, multiplication, division.
Integer a = 10;
Integer b = 5;
System.debug(a + b); // Outputs 15

Decimal: Used to store numbers with decimal points.

  • Example: Decimal price = 19.99;
  • Common operations: mathematical operations (addition, multiplication, etc.).
Decimal total = 99.95;
Decimal discount = 10.50;
System.debug(total - discount); // Outputs 89.45

String: Used to store text.

  • Example: String message = 'Hello World';
  • Common operations: concatenation (joining strings), length checking.
String greeting = 'Hello';
String name = 'Alice';
System.debug(greeting + ' ' + name); // Outputs 'Hello Alice'

Boolean: Used to store true or false values.

  • Example: Boolean isAvailable = true;
  • Common operations: logical operations like AND, OR, NOT.
Boolean isAdmin = true;
Boolean isActive = false;
System.debug(isAdmin && isActive); // Outputs false

Date and DateTime: Used to store date values.

  • Example: Date today = Date.today();
  • Common operations: calculating differences between dates, adding days.
Date birthdate = Date.newInstance(1990, 5, 15);
System.debug(birthdate.year()); // Outputs 1990

Time: Used to store a time of day without a date.

  • Example: Time meetingTime = Time.newInstance(14, 30, 0, 0);
  • Common operations: comparing times.
Time currentTime = Time.now();
System.debug(currentTime.hour()); // Outputs the current hour

Conclusion

Understanding data types is one of the foundational skills in programming, and it’s even more important in a strongly-typed language like Apex. By learning the differences between primitive and non-primitive data types, and how data types affect the operations you can perform, you’ll be well on your way to becoming proficient in Salesforce development.

Whether you’re manipulating numbers, handling text, or managing dates, knowing how to declare and use data types effectively will make your code more reliable and easier to maintain. Happy coding!