Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMatt Lassiter
Courses Plus Student 10,947 PointsValue Types Vs Reference - Reference advantages
So I understand the advantage of using a value type over a reference type. However, I also know that classes are an important thing to understand and learn to utilize. What kind of advantages does a reference type have over value? Is the advantage simply that it actually effects the original item or is there more to it than that?
Thanks in advance to everyone who participates!
2 Answers
Allan Clark
10,810 PointsYes the main advantage of Reference type will be the ability to work with the exact same Object. Classes are like Object factories, they create the Object and store it and a reference to it in memory. For example lets say we had a Car Class.
var myCar = Car(make: "Mazda", model: "RX8", color: "black cherry")
//because this is reference type we can now send this particular Car Object
//as a method parameter.
changePaintJob(carToPaint: myCar, newColor: "blue mica")
The method is accepting the reference to the same myCar Object and doing whatever that method does on that same Object, resulting in only one Car Object in memory that had its Color changed. If this was done as a struct and Car was sent in as a Value type only copies of the values will be sent, not the Object itself. Value type would result in 2 cars in memory, the original still black cherry, and the copy of those values that then gets changed to blue mica.
Hope this helps. Please let me know if I can clear any specifics up.
Allan Clark
10,810 PointsVery accurate
Matt Lassiter
Courses Plus Student 10,947 PointsAllan Clark!! You are the man. Much appreciated.
Alfredo Elizondo
1,583 PointsIn the video Amit says using Value types are much better , but im having the impression with this example that using reference types is better due to memory usage, am i right?
Allan Clark
10,810 PointsWell one is not necessarily better than the other, just different tools for different uses. But in my experience you will use reference type more often than value type.
Matt Lassiter
Courses Plus Student 10,947 PointsMatt Lassiter
Courses Plus Student 10,947 PointsThis was excellent, thank you! So in a very simplified fashion - reference types are very important to use if the function of whatever needs to happen depends on the original object being modified, and not simply copied? Is that an accurate statement?