Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

What is the difference between object and primitive types?

object Primitive types
0
Posted

What is the difference between object and primitive types?

0

Variables with object types contain references to object values. This means that after a := b the variable ‘a’ refers to the same object as variable ‘b’. Therefore changes of the object value that ‘a’ refers to, will effect variable ‘b’ as well (and vice versa) because both variables refer to the same object. For primitive types a different logic is used. Variables with primitive types contain the value itself. This means that after a := b both variables are still distinct and changing one variable has no effect on the other. If ‘a’ and ‘b’ are declared to have type ‘aType’ which contains the integer field ‘property’ you can do the following: b.property := 1; a := b; b.property := 2; Everything boils down to the question: What value does ‘a.property’ have now. • When ‘aType’ is an object type a.property has the value 2 because ‘a’ and ‘b’ both refer to the same object. • When ‘aType’ is a primitive type a.property has still the value 1 because ‘a’ and ‘b’ are distinct objects.

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123