Why do we need objects if there are variables? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why do we need objects if there are variables?

14th Oct 2021, 4:02 PM
โ€Žุฃู†ุฏุฑูŠู‡โ€Ž
โ€Žุฃู†ุฏุฑูŠู‡โ€Ž - avatar
13 Answers
+ 3
Its purpose is to store a value at a memory location. Data object is a general term for a region of data storage that can be used to hold values. The C standard uses just the term object for this concept. One way to identify an object is by using the name of a variable.
14th Oct 2021, 5:00 PM
Arun Jamson
Arun Jamson - avatar
+ 3
Mr_Developer Do you mean what's the purpose of objects when you can get the job done with just primitive variables?
14th Oct 2021, 6:05 PM
Tim
Tim - avatar
+ 2
The majorย differenceย lies in how we initialize and useย variables and objects.ย Variableย is a named storage of some data type(like int, float etc). Andย Objectsย areย variablesย of data types that are user defined. So we say that anย objectย is an instance of a class. Similarly, what is the difference between an object and an object variable? You can think of it in non-computing terms - anย objectย is a thing, whereas aย variableย is something that points at the thing (not strictly correct, but it's a bit like this).ย Objectย is group ofย variablesย which help us identify a entity uniquely.
16th Oct 2021, 7:41 AM
Chaitanya
Chaitanya - avatar
+ 2
objects are user defined variable in simple terms where we can store different types of data like structure and union
16th Oct 2021, 10:01 AM
Chaitanya
Chaitanya - avatar
+ 1
If I am not wrong variables are only the objects. Are you telling about the object of class passed through constructor?
14th Oct 2021, 4:18 PM
Atul [Inactive]
+ 1
I mean why are they needed. After all, an object is a specific subject in a class, for example, a class, a class, and an object, Andrey, Vanya, for example ...
14th Oct 2021, 6:39 PM
โ€Žุฃู†ุฏุฑูŠู‡โ€Ž
โ€Žุฃู†ุฏุฑูŠู‡โ€Ž - avatar
+ 1
Mr_Developer [Edit] apologies for the long answer Supposed we have 3 arrays: string[] weapons = { "sword", "staff", "bow", "rifle", "shield" }; int[] damages = { 20, 25, 15, 30, 10 }; string[] types = { "melee", "ranged", "ranged", "ranged", "melee" }; This kind of design could work, but each time you need to look for certain weapon, you need access all 3 of them to get their information, what's even worse is the readability. An object represents a unique clone of the class, let's say when you need to search for the shield, you wouldn't have to look for the arrays, you only need the name or the id of the shield, the readability is better and everything is well organised. In the future if you wish to add more properties (Price, Id, IsHalloween, IsChristmas, etc.) to Weapon, it'd be easier to work with a class instead of a bunch of arrays. Hopefully this helps and I hope this would help you understand the use of class and when to use them ๐Ÿ‘ https://code.sololearn.com/cn5CJTF6cVby/?ref=app
14th Oct 2021, 7:32 PM
Tim
Tim - avatar
+ 1
Can anyone tell me if my answer is correct or not?
15th Oct 2021, 6:54 AM
Atul [Inactive]
+ 1
Terel Schmitt You can use example using object instead of array?
15th Oct 2021, 4:33 PM
โ€Žุฃู†ุฏุฑูŠู‡โ€Ž
โ€Žุฃู†ุฏุฑูŠู‡โ€Ž - avatar
+ 1
Atul [Inactive], An object is a lot like a variable. But I asked why this is needed if the variables already exist.
15th Oct 2021, 4:36 PM
โ€Žุฃู†ุฏุฑูŠู‡โ€Ž
โ€Žุฃู†ุฏุฑูŠู‡โ€Ž - avatar
+ 1
See variables are primitive objects only. Now if you see some concepts of collection . In that case a single object is created and the data are stored in it So in that case variables won't be working out
15th Oct 2021, 6:06 PM
Atul [Inactive]
+ 1
This point can be described in more detail: an object is a group of variables that help us to uniquely identify an entity.
16th Oct 2021, 8:38 AM
โ€Žุฃู†ุฏุฑูŠู‡โ€Ž
โ€Žุฃู†ุฏุฑูŠู‡โ€Ž - avatar
+ 1
Do words of different data types mean passing multiple arguments into one object?
16th Oct 2021, 10:07 AM
โ€Žุฃู†ุฏุฑูŠู‡โ€Ž
โ€Žุฃู†ุฏุฑูŠู‡โ€Ž - avatar