Datatypes and Variable in Apex

 

Hello friends, welcome to sfdc Amplified.

This article will cover:

  1. What is a variable?
  2. What is a datatype?
  3. Primitive data types

 

These are the data types which are predefined by the Apex. Salesforce supports the following Apex data types :

  1. Primitive data types
  2. Collections
  3. sObjects
  4. Enums

For the scope of this article we will learn primitive data type.

What is a Variable? 

It is a place to store information.Variables can be thought of as ‘containers’ for data. Each variable has to have a unique name, so that you can refer back to it in the program.

What is datatype?

Unlike humans, a computer does not understand difference between “1674” and “sfdcamplified.” When creating a variable, we also need to declare the data type it contains. This is because the program will use different types of data in different ways.

There are five basic type of variables. Basically, it gives answer to what kind of information are we going to store.

So lets begin understanding datatypes.

String 

  1. String is always surround with single quote
  2. Choose most appropriate data type. You cannot add two numbers if data type are string.
String sfdcname = 'SfdcAmplified';

Integer

It is a number without any decimal points. Do not use single quote with integer

Integer strairs = 10;

Integer sum = 2+ 5;

Decimal

You can store number with decimal points

Decimal prob = 0.67;

Note: When in confusion then use decimal to avoid data loss

Date

A value that indicates a particular day.

Date currentDate = Date.Today();

DateTime

A value that indicates a particular day and time

DateTime currentTime = DateTime.now();

Boolean

It gives true or false value

Boolean isClosed = True

Note: Boolean do not have quotes

How to create variable in apex

  1. Choose Datatype
  2. Choose variableName
String myName = 'Smriti';

I hope you enjoyed this article. For more of these articles stay tuned!

Get latest updates

If you like the Video then subscribe to Youtube Channel

If you like the Article then subscribe to the Blog

Together we can learn faster

You can join the Whatsapp group 

You can join Facebook group

You can join Twitter

You can join Instagram 

Did you enjoy this article?
Signup today and receive free updates straight in your inbox.
I agree to have my personal information transfered to MailChimp ( more information )
50% LikesVS
50% Dislikes