2.1 Using variables to store information

The concept

There are always things we want to remember. We need to remember which tasks we need to work on. We need to keep track of grocery list we want to buy at the shopping centre. In those cases, if the information is hard for our brain to remember all of them, we write them down in a piece of page or track them down in the computer. So later we can find them.

Variables serve for this purpose in programming. It stores the data in a variable and later on we can reuse it. It gives the variable a variable name so later on we know how to retrieve it. Below is an example:

String greetings = 'Hello, ';
String myName = 'Lance'; //Replace it with your name
System.debug(greetings + myName);
greetings = 'Bonjour, ';
System.debug(greetings + myName);

Execute this code in your developer console. Your result should be similar to this:

A bit explanation

The grammar for defining and reusing variables is not hard in Apex. You need to specify the variable type, followed by the variable name. Then you can initialize the value of it. The initialization is not mandatory. But if you don’t initialize it, it will default to null, which means no value.

Apex is a strong type language, which means you have to specify a type to the variable. Here both variables’ type are String, which means a text variable. We will talk more about variable types in next post.

Followed is your variable’s name. Your variable name has to be unique. You can pretty much name your variable anything you want, as long as it is in one word. And make sure it doesn’t conflict with Apex reserved keywords.

Then you can initialize the value of your variable by using =.

You can change your variable’s value at any time later in your programme. And you can reuse it as many times as you want. But you can’t change your variable’s type. So,

String greetings = 'Hello';
//greetings = 123;
//The above line won't work because you can't assign an integer value into a String variable. 

You can’t assign some value of a different kind to a variable later on. As the above code shows.


Create two variables: firstName and lastName to hold your first and last name. Then display in debug log this sentence using both variables:

Hello, [Your Name]. Welcome to the world of Apex programming.

Next Post

2.2 Variable types and conversions

Subscribe to Sfdcinpractice

Subscribe to get the latest blogs and tutorials of sfdcinpractice. No spam, no trash, only the awesome posts from sfdcinpractice. 


  1. Munira Majmundar - March 9, 2017 @ 8:27 pm

    Hello Lance:

    Name of the string (data type) is myName so, should not the next sentence read as follows:
    System.debug(greetings + myName);

    String greetings = ‘Hello, ‘;
    String myName = ‘Lance’; //Replace it with your name
    System.debug(greetings + name);
    greetings = ‘Bonjour, ‘;
    System.debug(greetings + name);

    • LanceShi - March 9, 2017 @ 11:05 pm

      Thank you for pointing this out, Munira. You are right. It should definitely be myName. I have updated the post.

      I believe I have tested my code before posting it. But not quite sure what was happening during the time I wrote this. Sorry about that.

  2. Abhinav Singh - April 13, 2017 @ 2:38 pm

    I was trying to complete the quiz but I am not able to execute this code.

    string fname = ‘Abhinav’;
    string lname = ‘Singh’;
    System.debug(‘Hello,’ [fname+lname]’. Welcome to the world of Apex programming.’);

  3. Sujata Tiwari - April 20, 2017 @ 10:29 am

    Hi Abhinav,

    Please try this

    String firstName =’Sujata’;
    String lastName = ‘Tiwari’;
    System.debug(‘Hello,’ + ‘ ‘ +firstName + ‘ ‘ +lastName + ‘ ‘ + ‘Welcome to the world of Apex programming.’);

    //Please use ‘+’ sign which is used for concatenation purpose.

  4. Paolo - May 24, 2017 @ 1:33 pm

    this one works

    String FirstName =’Paolo’;
    String LastName = ‘Berretta’;

    system.debug(‘Hello,’+ ‘ ‘+FirstName + ‘ ‘ +LastName+’ ‘ +’Welcome to the world of Apex programming’ );

Leave a Reply

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