0% found this document useful (0 votes)
1 views3 pages

Object data type

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views3 pages

Object data type

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Object data type

The Object data type in Apex is a generic or base type that can hold any data type,
whether it's a primitive type, collection, custom object, or standard object. It’s like a
catch-all container that can store any kind of value or reference.

Key Characteristics of the Object Data Type:

Generic Type:

1. Object is the parent type of all other data types in Apex.


2. This means any data type, including primitives (like Integer, String,
Boolean), Salesforce records (like Account, Contact), and custom classes, can
be stored in a variable of type Object.

Implicit Casting:

1. Apex automatically casts specific types to Object. When you assign a value to an
Object type variable, Apex will implicitly convert it to Object.
2. E

Object myObject = 'Hello World'; // String stored in an Object


Object numObject = 10; // Integer stored in an Object

Type Casting:

1. When you retrieve a value from an Object variable and need to use it as a specific
type, you must explicitly cast it back to its original type.
2. E

Object obj = 100;


Integer myNumber = (Integer)obj; // Casting back to Integer

Flexibility:

1. Since Object can hold any type, it's useful for handling dynamic or unknown data
types. For example, if you’re writing generic methods that should work with
multiple types, you can use Object as the input parameter.

Example of Using Object Type


public class ObjectExample {

public void demonstrateObjectType() {


// Object can hold any type of data
Object objString = 'Apex Programming'; // Holds a String
Object objInteger = 123; // Holds an Integer
Object objBoolean = true; // Holds a Boolean
Object objDate = Date.today(); // Holds a Date

// Output values
System.debug('Stored String: ' + objString);
System.debug('Stored Integer: ' + objInteger);
System.debug('Stored Boolean: ' + objBoolean);
System.debug('Stored Date: ' + objDate);

// Casting back to specific types


String str = (String)objString; // Cast Object back to String
Integer num = (Integer)objInteger; // Cast Object back to Integer
Boolean flag = (Boolean)objBoolean; // Cast Object back to Boolean
Date today = (Date)objDate; // Cast Object back to Date

System.debug('After casting: String = ' + str + ', Integer = ' + num + ',
Boolean = ' + flag + ', Date = ' + today);
}
}

Practical Use Cases of the Object Type:

Generic Method: You can write methods that can accept any type of
parameter by using Object as the parameter type. This makes the method
flexible for multiple use cases.

Example of a generic method:

public void printObject(Object input) {


System.debug('Input value: ' + input);
}

// Call with different types


printObject('Hello'); // Pass a String
printObject(25); // Pass an Integer
printObject(Date.today()); // Pass a Date

Dynamic Behavior in Lists or Maps: Sometimes, you may need to store or


pass data with mixed types in collections (like List<Object> or Map<String,
Object>). Object allows for such flexibility.

Example:

Map<String, Object> dynamicMap = new Map<String, Object>();


dynamicMap.put('Name', 'John Doe'); // String
dynamicMap.put('Age', 30); // Integer
dynamicMap.put('Active', true); // Boolean

System.debug('Map values: ' + dynamicMap);

// Access and cast back the values


String name = (String)dynamicMap.get('Name');
Integer age = (Integer)dynamicMap.get('Age');
Boolean isActive = (Boolean)dynamicMap.get('Active');

System.debug('Name: ' + name + ', Age: ' + age + ', Active: ' + isActive);

Key Points to Remember:


 Object is the base type for all other data types in Apex.
 It’s useful when you don’t know the type of the data in advance or need to handle different
types in the same method or collection.
 You can assign any data type to an Object variable.
 When you need to perform operations specific to a data type (e.g., string operations or
number operations), you must cast the Object back to its original type.

When to Use Object:

 Generic code: When writing flexible or reusable methods, Object allows you to accept and
work with any type.
 Dynamic data handling: When working with APIs, integrations, or scenarios where the data
types are not fixed in advance.

You might also like