com.aspose.words.Document object comparison using ==

Hi there,
I have seen the condition if (srcDoc == destinationDoc) in the Adam’s code ImportDocumentDifferentStylesOnly shared in this thread Aspose.Words - There are too many styles in the document.

I just want to know when we use == operator for comparing the document objects actually what we are comparing. I mean is it comparing in-memory representation or reference or something else.

Thanks.

Hi Praneeth,


Thanks for your inquiry. No, it does not compare actual Word documents which are loaded in these two instances of Document class; instead it checks whether these two instances (srcDoc and mDstDoc) point to same location in memory or not. I hope, this helps.

Best regards,

Hi Awais,
Thanks for you reply. I’m not able to understand the check.
if (srcDoc == mDstDoc)
srcDoc = srcDoc.deepClone();

Could you please explain what this check is doing?
Thanks.

Hi Praneeth,


Thanks for your inquiry. Please consider the two lines of code below:

Document doc = new Document();
Document doc1 = doc;

In this case, comparing doc and doc1 using == operator would return true as these two variables point to same location in memory i.e. you can make changes to the Document instance in memory via either of these variables (doc and doc1). Now consider the following two lines of code:

Document doc = new Document();
Document doc1 = (Document)doc.deepClone(true);

In this case the condition doc == doc1 would return false as these two variables now point to separate Document instances in memory. I hope, this helps.

Best regards,