Data types in Apex

Apex supports the following data types.

  • A primitive, such as an Integer, Double, Long, Date, Datetime, String, ID, Boolean, among others.
  • An sObject, either as a generic sObject or as a specific sObject, such as an Account, Contact.
  • A collection, including:
    • A list
    • A set of primitives
    • A map
  • A typed list of values, also known as an enum
  • User-defined Apex classes
  • System-supplied Apex classes

Collection:

List:-Lists in Apex give ordered collection of objects.Its like array who give the ordered collection.List can contain duplicate.

Syntax for creating a list:-

List<Datatype> list_name =new List<datatype>();

Set:- Its stores the Set of Integer Values. unordered and no Duplicates are allowed.

Syntax for creating a list:-

Set<Datatype> set_name = new set<datatype>();

Map:- It is a collection of key and value pairs.where as key is primitive datatype and value can include primitives,sObjects,Apex object.you can use Map when you want to quicly find value by using key.

Syntax of creating a list:-

Map<key_Datatype,value_Datatype> map_name = new map< key_Datatype,value_Datatype>();

sObject:-

In salesforce Apex is integrated with database.An sObject is any object that can be stored in the force.com database. Each Salesforce record is represented as an sObject before it is inserted into Salesforce.

If you have create custom object in salesforce, then use API name of that custom object in Apex.

Creating sObject variables:-

Account a = new Account(Name=’Bank’);

You can add other fields for Account and can add records in it.

Account a = new Account(Name=’Bank’, Phone='(435)000-1212′, NumberOfEmployees=100);

Leave a comment

Design a site like this with WordPress.com
Get started