what are the differences between variable and an object
what are the differences between variable and an object
Collapse
X
-
Tags: None
-
The short answer:
A variable is a named non-constant handler which points to an object or primitive.
The longer answer:
Say you have a class that looks like this:
[code=java]public class Example {
public String name;
}[/code]
Now you can create an instance of this class like so:
[code=java]new Example();[/code] This instance is called an object. You may access the object, e.g. like this:
[code=java](new Example()).name = "My Object";[/code]
This however is not useful in many situations, as you will want to access an object more than once. This is where variables can come into play.
Basically, this is a variable:
[code=java]Example myExample;[/code]
You have declared, that there is a variable called myExample. You haven't declared it yet (meaning it does not yet point to an object).
To use them together, you normally do something like this:
[code=java]Example myExample;
myExample = new Example();[/code]
or shorter:
[code=java]Example myExample = new Example();[/code]
Now the variable and the object are bound; however the binding doesn't have to be exclusive. For example, look at this:
[code=java]Example myFirstExample = new Example();
Example mySecondExample = myFirstExample;
myFirstExample. name = "first example";
System.out.prin tln(mySecondExa mple.name);[/code]
The output will be "first example". The reason for this is, that myFirstExample and mySecondExample point to the same object.
This can be changed however; for example, look at this:
[code=java]// Create example and declare the variables to point to it
Example myFirstExample = new Example();
Example mySecondExample = myFirstExample;
// Set the name
myFirstExample. name = "first example";
// Reassign the first example and set its name
myFirstExample = new Example();
myFirstExample. name = "new first example";
// Print the names
System.out.prin tln(myFirstExam ple.name);
System.out.prin tln(mySecondExa mple.name);[/code]
The output will be:
This is because mySecondExample is pointing to the original value of myFirstExample which itself however is now pointing to a new value.Code:new first example first example
So, what about constant values?
Variables are so called fields; fields are generally things that can refer to objects or primitives. (A primitive is stuff like an integer or a boolean.) There is a second type of fields however: constants. Constants are marked with the final modifier, declared once and can thereafter not be changed. Here, an example:
[code=java]final Example myConstant = new Example();
myConstant.name = "constant"; // This is fine as you're changing the object, not the constant
myConstant = new Example(); // This is not possible as you would be reassigning the constant[/code]
I hope that helps -
A variable is nothing but a named stored location which holds known or an unknown value.
a variable is a place to hold data.
Simple variable can only hold one value (string, number, boolean etc).
Object is just a fancy name given to user defined variables. Having said that, objects are a bit special. Objects are variables : but they are instances of user defined types called as classes or structures (structs).
Object can hold pairs of variables and values,
also that object model can be used for another object just different values, and oop is based on that, using same code multiple times with diff value s without rewriting it.Comment
-
In fact, what you are talking about should be an object reference. The object is actually the part of new. The object reference is the name of the assigned object.
The difference between objects and variables:
The object is new, you can think that the new is an object, and the one that is not new is not an object. The object contains all the methods, variables, and constants in your new class, which can be referenced by the object and the new class name ( ) To call the methods, variables, and constants in this class. In terms of memory, objects are allocated on the heap and managed by the garbage collector.
A variable is actually a pointer, but the data type is specified for it. The data pointed to can only be of this data type. You can change the pointed value, and you can also change the pointed position. If you store basic type data , Then from the memory that the data is only stored in the stack, but the storage is a reference data type, then the reference is stored in the stack, and the data is stored in the heapComment
Comment