SFDC Stop - Always the latest about Salesforce


Full Tutorial Series with videos, free apps, live sessions, salesforce consulting and much more.


Telegram logo   Join our Telegram Channel

Thursday 25 April 2024

Building Blocks of Apex: Variables & Data Types Explained Simply - Salesforce Apex Tutorial Part 2 to 4 | Code Snippet

Hello Trailblazers,

In this post, I'm sharing the code snippet for Coding with Confidence: The Fun Way to Learn Salesforce Apex tutorial series part 2 to 4 below:
// * Variables & Data Types in Apex

/*

? What is a variable?
* A variable is simply a named storage/memory location that contains a value. In apex, all variables have a "data type".
! Each variable is by default initialized to null

? What is a Data Type?
* Data type is a keyword associated with a variable. It basically represents the type of data which that variable will store.
* List of primitive data types:
* 1. Boolean
* 2. Integer
* 3. Decimal
* 4. Double
* 5. Long
* 6. Date
* 7. Datetime
* 8. Time
* 9. Id
* 10. String
* 11. Object
* 12. Blob

* These are the OOTB or fundamental data types available in apex programming language. Let's talk about each one of them in detail:

? What is a Boolean data type?
* A variable of Boolean data type can store only one of these two values:
* 1. True
* 2. False

* Example of Boolean data type:
*/

/*
one
two
three

*/

// comment

// * In order to declare a variable, we follow the format: <Data Type><space><Variable name><;>
Boolean isQualified;
System.debug('Value of isQualified boolean variable is: ' + isQualified);

isQualified = false; // * Equals (=) means Assignment
System.debug('Value of isQualified boolean variable is: ' + isQualified);

isQualified = true;
System.debug('Value of isQualified boolean variable is: ' + isQualified);

// isQualified = 123;

/*

? What is an Integer data type?
* An Integer data type can store a 32-bit number with no decimal point. It can store value between -2,147,483,647 to 2,147,483,647

* Example of Integer data type:

*/

Integer sum;
System.debug('Value of sum Integer variable is: ' + sum);

sum = 1;
System.debug('Value of sum Integer variable is: ' + sum);

sum = 10;
System.debug('Value of sum Integer variable is: ' + sum);

sum = 2147483647;
System.debug('Value of sum Integer variable is: ' + sum);

sum = -2147483647;
System.debug('Value of sum Integer variable is: ' + sum);

// sum = false;

YouTube Tutorials

You can check out the tutorials below as well:

Building Blocks of Apex: Variables & Data Types Explained Simply - Salesforce Apex Tutorial Part 2

Apex Gotchas! Common Variable Errors (and Writing Comments) - Salesforce Apex Tutorial Part 3

Apex Integer Data Types: Understanding Limits & Avoiding Errors - Salesforce Apex Tutorial Part 4

Check out the tutorials and let me know your feedback in the comments down below.

Happy Trailblazing!!

No comments:

Post a Comment