Understanding the info Types and variables in Apex programming language

Last updated on Nov 24 2021
Abha Kulkarni

Table of Contents

Understanding the info Types and variables in Apex programming language

The Apex language is strongly typed so every variable in Apex is going to be declared with the precise data type. All apex variables are initialized to null initially. it’s always recommended for a developer to form sure that proper values are assigned to the variables. Otherwise, such variables when used, will throw null pointer exceptions or any unhandled exceptions.
Apex supports the subsequent data types −
• Primitive (Integer, Double, Long, Date, Datetime, String, ID, or Boolean)
• Collections (Lists, Sets and Maps) (To be covered in Chapter 6)
• sObject
• Enums
• Classes, Objects and Interfaces (To be covered in Chapter 11, 12 and 13)
In this chapter, we’ll check out all the Primitive Data Types, sObjects and Enums. we’ll be watching Collections, Classes, Objects and Interfaces in upcoming chapters since they’re key topics to be learnt individually.
Primitive Data Types
In this section, we’ll discuss the Primitive Data Types supported by Apex.
Integer
A 32-bit number that doesn’t include any percentage point . the worth range for this starts from -2,147,483,648 and therefore the maximum value is up to 2,147,483,647.
Example
We want to declare a variable which can store the number of barrels which require to be shipped to the customer of the chemical processing plant.

Integer barrelNumbers = 1000;
system.debug(' value of barrelNumbers variable: '+barrelNumbers);

The System.debug() function prints the worth of variable in order that we will use this to debug or to urge to understand what value the variable holds currently.
Paste the above code to the Developer console and click on on Execute. Once the logs are generated, then it’ll show the worth of variable “barrelNumbers” as 1000.
Boolean
This variable can either be true, false or null. repeatedly , this sort of variable are often used as flag in programming to spot if the actual condition is about or not set.
Example
If the Boolean shipmentDispatched is to be set as true, then it are often declared as −

Boolean shipmentDispatched;
shipmentDispatched = true;
System.debug('Value of shipmentDispatched '+shipmentDispatched);

Date
This variable type indicates a date. this will only store the date and not the time. For saving the date along side time, we’ll got to store it in variable of DateTime.
Example
Consider the subsequent example to know how the Date variable works.

//ShipmentDate are often stored when shipment is dispatched.
Date ShipmentDate = date.today();
System.debug('ShipmentDate '+ShipmentDate);

Long
This is a 64-bit number without a percentage point . this is often used once we need a variety of values wider than those provided by Integer.
Example
If the corporate revenue is to be stored, then we’ll use the info type as Long.

Long companyRevenue = 21474838973344648L;
system.debug('companyRevenue'+companyRevenue);

Object
We can refer this as any data type which is supported in Apex. for instance , Class variable are often object of that class, and therefore the sObject generic type is additionally an object and similarly specific object type like Account is additionally an Object.
Example
Consider the subsequent example to know how the bject variable works.

Account objAccount = new Account (Name = 'Test Chemical');
system.debug('Account value'+objAccount);

Note − you’ll create an object of predefined class also , as given below −

//Class Name: MyApexClass
MyApexClass classObj = new MyApexClass();

This is the category object which can be used as class variable.
String
String is any set of characters within single quotes. It doesn’t have any limit for the number of characters. Here, the heap size is going to be wont to determine the number of characters. This puts a curb on the monopoly of resources by the Apex program and also ensures that it doesn’t get overlarge.
Example

String companyName = 'Abc International';
System.debug('Value companyName variable'+companyName);

Time
This variable is employed to store the actual time. This variable should be declared with the system static method.
Blob
The Blob may be a collection of Binary data which is stored as object. this may be used once we want to store the attachment in salesforce into a variable. This data type converts the attachments into one object. If the blob is to be converted into a string, then we will make use of the toString and therefore the valueOf methods for an equivalent .
sObject
This is a special data type in Salesforce. it’s almost like a table in SQL and contains fields which are almost like columns in SQL. There are two sorts of sObjects – Standard and Custom.
For example, Account may be a standard sObject and the other user-defined object (like Customer object that we created) may be a Custom sObject.
Example

//Declaring an sObject variable of type Account
Account objAccount = new Account();

//Assignment of values to fields of sObjects
objAccount.Name = 'ABC Customer';
objAccount.Description = 'Test Account';
System.debug('objAccount variable value'+objAccount);

//Declaring an sObject for custom object APEX_Invoice_c
APEX_Customer_c objCustomer = new APEX_Customer_c();

//Assigning value to fields
objCustomer.APEX_Customer_Decscription_c = 'Test Customer';
System.debug('value objCustomer'+objCustomer);

Enum
Enum is an abstract data type that stores one value of a finite set of specified identifiers. you’ll use the keyword Enum to define an Enum. Enum are often used as the other data type in Salesforce.
Example
You can declare the possible names of compound by executing the subsequent code −

//Declaring enum for Chemical Compounds
public enum Compounds {HCL, H2SO4, NACL, HG}
Compounds objC = Compounds.HCL;
System.debug('objC value: '+objC);

Apex – Variables

Java and Apex are similar during a lot of the way . Variable declaration in Java and Apex is additionally quite an equivalent . we’ll discuss a couple of examples to know the way to declare local variables.

String productName = 'HCL';
Integer i = 0;
Set setOfProducts = new Set();
Map mapOfProductIdToName = new Map();

Note that each one the variables are assigned with the worth null.
Declaring Variables
You can declare the variables in Apex like String and Integer as follows −

String strName = 'My String'; //String variable declaration
Integer myInteger = 1; //Integer variable declaration
Boolean mtBoolean = true; //Boolean variable declaration

Apex variables are Case-Insensitive
This means that the code given below will throw a mistake since the variable ‘m’ has been declared twice and both are going to be treated because the same.
Integer m = 100;
for (Integer i = 0; i
Scope of Variables
An Apex variable is valid from the purpose where it’s declared in code. So it’s not allowed to redefine an equivalent variable again and in code block. Also, if you declare any variable during a method, then that variable scope are going to be limited thereto particular method only. However, class variables are often accessed throughout the category .
Example

//Declare variable Products
List Products = new List();
Products.add('HCL');

//You cannot declare this variable during this code clock or sub code block again
//If you are doing so then it'll throw the error because the previous variable in scope
//Below statement will throw error if declared in same code block
List Products = new List();

So, this brings us to the end of blog. This Tecklearn ‘Understanding the Data Types and variables in Apex Programming Language’ blog helps you with commonly asked questions if you are looking out for a job in Salesforce. If you wish to learn Salesforce and build a career in Salesforce domain, then check out our interactive, Salesforce Certification Training: Admin 201 and App Builder, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

Salesforce Certification Training: Admin 201 and App Builder

Salesforce Certification Training: Admin 201 and App Builder

About the Course

Salesforce Certification Training course will help you pass the Salesforce Administrator Exam (Admin 201) and the Salesforce App Builder (Dev 401) Exam. Concepts on Force.com Platform, AppExchange, SFDC Security Model, Service Cloud, Sales Cloud, Lightning App Builder, Salesforce Reports & Dashboard can be mastered in this Salesforce Training course. You can also configure the platform, manage users, find better ways to use the platform’s features, build applications with Salesforce Lightning, and more. Further, in this Salesforce certification training course, you will master App builder, Apex, Visualforce, etc.

Why Should you take Salesforce Admin 201 and App Builder Training?

• As per Indeed.com data, 200% global jump in Salesforce jobs since Jan 2016. Salesforce Certified Administrators earn an annual average salary of $87,000 but can go as high as $160,000 depending on their knowledge, skills, and experience.
• More than 200,000 companies worldwide use Salesforce platform. Salesforce leads the CRM market with 19.5 percent of market share – Forbes.
• The global CRM software market will reach US$40.26 billion in 2023, up from US$36.9 billion (2020) – Statista.

What you will Learn in this Course?

Salesforce Fundamentals
• Introduction to CRM concepts and Cloud computing
• Salesforce.com Overview and Fundamentals
• Understanding Salesforce Platform
Understanding Salesforce Platform
• Understanding Salesforce Terminologies and Introducing the force.com platform
• Understanding Salesforce Metadata and API
• Describe the capabilities of the core CRM objects in the Salesforce schema
• Identify common scenarios for extending an org using the AppExchange
• About Salesforce Certification
Introduction to Sales Cloud
• Sales Cloud
• Sales Process
• Sales Productivity Features
• Lead Management
• Lead auto response
• Lead assignment
• Web to lead
• Accounts and Contacts Management
• Opportunities
• Campaign Management
Security Model, User Management and Its Features
• Security Model Mind Map
• System Level or Org Level Security
• User Administration and Troubleshooting
• Permission Sets
• Profile Management
• User Actions
• Assigning Permission
• Session settings
• Activations
• Page layout assignment
• Tab setting
• Field level security
Object, Record and Field Level Features
• Custom Object
• Custom Field
• Data Types
• Relationship among Objects
• Working with App and Tabs
Data Handling and Processing
• Data Import and Export with Salesforce
• Insert, Update and Delete Data with Salesforce
• Export Data with UI
• Export Data using Data Loader Tool
Deployment
• SandBox
• Moving Data from SB to Production – Deployment
• Types of SandBox
• Change Sets
• Types of Change Sets
Application Cycle
• Milestones
• Sandboxes
• Change Sets
• Packages
Reports and Dashboards
Declarative Implementation in Salesforce
Salesforce Development and Apex Programming
• Apex Programming
• Apex Classes
• Apex Settings
• SOQL – Salesforce Object Query Language
• DML Commands
• Apex Class in Detail
• Apex Triggers
• Apex Testing
• Access Specifier in Salesforce
• Testing
Lightning in Salesforce
• Lightning Components
• Lightning Component Capabilities
• Lightning Components vs. Visualforce
Visual Force in Salesforce
• Standard Visualforce controller and controller extensions,
• Visualforce Page
• Understanding the MVC Pattern
• Tools for Visualforce Development
• Visual Force Components
WorkFlows in Salesforce
• Work Flows in Salesforce
• Types of Work Flows
• Work Flows Rules
About Preparation of Salesforce 201 and App Builder Certification exams

Got a question for us? Please mention it in the comments section and we will get back to you.

 

0 responses on "Understanding the info Types and variables in Apex programming language"

Leave a Message

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