Is there any way to compare two primitives to find out if they are not only the same value but actually the same object?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
The operator you’re looking for is ===. It doesn’t work on pure primitive entities, but it does work on references to those entities. The opposite test is !==.
Object a = 5, b = 5; if(a === b) { System.debug('A and B are the same object.'); } else { // This will be output System.debug('A and B are not the same object.'); } Object a = 5, b = a; if(a === b) { // This will be output System.debug('A and B are the same object.'); } else { System.debug('A and B are not the same object.'); }
If you try to use Integer directly, for example, you’ll get a compile error:
Integer a = 5, b = 5; if(a === b) { // compile error, "equality only allowed for reference types"
Method 2
Have you looked at the new Type methods?
Will this accomplish what you are looking to do?
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0