instanceof in Apex
If we need to verify at runtime whether an object is actually an instance of a particular class use instanceof keyword. The instanceof keyword can only be used to verify if the target type in the expression on the right of the keyword is a viable alternative for the declared type of the expression on the left.
You could have something which is passed as Object type and using instanceof you can determine if it is an instance of your class or other data types
below code will help understand the use instanceof keyword.
Open developer console and run below code and observe the logs
InstanceofController.getType(new Account());
INstanceofController.getType(true);
INstanceofController.getType(Id.valueOf('001xa000003DIlo'));
InstanceofController.getType(Date.newInstance(2020, 12, 31));
InstanceofController.getType(DateTime.newInstance(2020, 12, 31,0,0,0));
InstanceofController.getType('Clever Fox');
InstanceofController.getType(Blob.valueof('Clever Fox'));
InstanceofController.getType(12345);
Long myLong = 4271990;
InstanceofController.getType(myLong);
InstanceofController.getType(new InstanceofController());
Debug logs:Reference:
Iterative Logic (must read)
Salesforce Developer Guide, Salesforce-System Namespace
Useful
ReplyDelete