Variables and Primitive Data Types

Summary

This post will cover topics below:

  • Declare variables.
  • Print a message to the screen.
  • Concatenate.
  • Cast data types.
  • Constant variables.
  • Further info of primitive data types.
  • Exercises.

Introduction

Variables 

Is a container to store values and We can reuse it again later. We also can update or change their value.

Values can be  a number, date, time, and string.

Primitive data types

To define a variable, it has to have a data type. Primitive data types are data type available in any programming languages.

These are common primitive data types:

  • String
  • Boolean
  • Time, Date, and DateTime
  • Integer, Long
  • Double, Decimal

Declare Variables

Syntax:

  • datatype variableName;
  • datatype variableName = value;

Examples:


Integer age; //declare Integer variable;
Integer name = 'Kenny'; //declare String variable and assign value

Print message to the screen

Syntax:

  • System.debug(‘string_message’);

Example:


    System.debug('Hello World'); // Result: Hello World 
    

Concatenate

  • Combine two variables into one.
  • String data type commonly uses this.

Syntax:

variable1+ variable2

Example:


String name = 'Kenny'; //declare name as string
String hobby = 'Salesforce Anthusiast'; //declare hobby as string
System.debug('Hello, my name is '+name+ ' and my hobby is '+hobby); //print to screen
    

Cast data types

Convert data type to another data type. There are 2 ways to cast data types. The ways are:

  • valueof() – converts existing data type to another data type.
  • (dataTypes) convert data types exclude String to another data type.

Syntax:

  • datatype variable1 = datatype.valueof(variable2);

Example using (dataTypes):


Integer num1 = 20, num2 = 3; //declare 2 Integer variables
Decimal result = (Decimal)num1/num2; //perform calculation and cast to Decimal
System.debug('Result of division is ' +result); //result: 6.6666666667


Example using valueof():


String num2 = String.valueof(20); //Cast number to String
System.debug('result is ' +num2); //print result is 20

Example with implicit casting


double tax = 23 / 0.06;
System.debug(tax); //383.333333...
System.debug('Sales tax ' +(Integer)(tax * 100) / 100.0 ); //383.33

Constant Variable

Variables that have permanent data that never changes.

Syntax:

final variable_type var_name = value;

Example:


final Integer num1 = 1532; //define constant value
System.debug('value is ' +num1); //print - 1532
num1 = 20; //update value to 20
System.debug('New value is' +num1); //print - FinalException error
    

Enums

  • Set of constant value.
  • Define using enum keyword

Example


public enum seasons {WINTER, SPRING, SUMMER}
seasons s = seasons.WINTER;
System.debug(s);
    

Further Info Primitive Data Types

String

  • Data type to store text value.
  • Declare a variable with the String without assign a value, it has a null value.

Example


//declare 2 String variables
String name = 'Kenny', hobby; 
//print - name Kenny and my hobby is null
System.debug('name '+name+ ' and my hobby is ' +hobby); 

Boolean

  • data type that stores True or False value.
  • Declare a variable with Boolean without assign a value, it has a null value.
Example:
 

Boolean isTrue = True; //declare Boolean variable with value of True 
System.debug('value of Boolean is ' +isTrue); //print - True

Integer, Long

  • data type to store numeric value with no decimal.
  • Integer is 32-bit and Long is 64-bit. So, Long can hold bigger value.
  • Declare a variable with Integer without assign a value, it has a null value.
Example:

Integer intLength = 10, intWidth = 20, areaResult; //define variables
areaResult = intLength * intWidth; //calculate the area
System.debug('the area is: ' +areaResult); //print - 200


Double, Decimal

  • Data type to store numeric value includes decimal value.

Example:


Decimal radius = 2.5, pi = 3.1415, areaCircle; //declare variables and their value
areaCircle = pi * radius * radius; //calculate the area
System.debug('The area is ' +areaCircle); //print - 19.634375

Example:


Integer num1 = 20.5; //Assign Decimal into Integer
System.debug('num1 is ' +num1); //throws an error.
//solution 
Integer num1 = (Integer) 20.5; //cast from double to Integer
System.debug('num1 is ' +num1);  //res = 20
    

Exercises:

  • FahrenheitToCelcius
  • Define Fahrenheit variable with value of 100.
  • Perform calculation with formula – (5.0 / 9) * (fahrenheit – 32)
  • Print result to screen. 

Compute Average

  • Define (n) numbers or more into variable with data types of Integer or Double
  • Perform average calculation – average = (num1, num2, num3) / 3

Sales Tax

Display sales tax for the purchase amount

  • Enter purchase amount into variable
  • Sales tax is 6 %
  • perform calculation to display sales tax on the screen with only 2 digits decimal.

Interest calculation

  • Define a balance $1500 into variable
  • Define annual interest rate of 1.8%
  • Comput interst rate of the next montly payment with formula:
  • interest = balance * (annualInterest / 1200)
  • Display result on the screen.
Future Investment value
Define investment amount, annual interest rate, and number of years and displays the result using formula:
  • futureInvestmentVal = investmentAmount * (1 + monthlyInterestRate) pow(numofYears * 12)
  • Hint: use Math.pow(a, b)

Calculate BMI

Put your weight in Kilogram  and height in centimeter. Calculation is weight in kilograms divide by the square of your height in meters.

 

Leave a Reply