473,473 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

what are the differences between variable and an object

1 New Member
what are the differences between variable and an object
Feb 20 '14 #1
3 3780
Nepomuk
3,112 Recognized Expert Specialist
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:
Expand|Select|Wrap|Line Numbers
  1. public class Example {
  2.     public String name;
  3. }
Now you can create an instance of this class like so:
Expand|Select|Wrap|Line Numbers
  1. new Example();
This instance is called an object. You may access the object, e.g. like this:
Expand|Select|Wrap|Line Numbers
  1. (new Example()).name = "My Object";
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:
Expand|Select|Wrap|Line Numbers
  1. Example myExample;
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:
Expand|Select|Wrap|Line Numbers
  1. Example myExample;
  2. myExample = new Example();
or shorter:
Expand|Select|Wrap|Line Numbers
  1. Example myExample = new Example();
Now the variable and the object are bound; however the binding doesn't have to be exclusive. For example, look at this:
Expand|Select|Wrap|Line Numbers
  1. Example myFirstExample = new Example();
  2. Example mySecondExample = myFirstExample;
  3. myFirstExample.name = "first example";
  4. System.out.println(mySecondExample.name);
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:
Expand|Select|Wrap|Line Numbers
  1. // Create example and declare the variables to point to it
  2. Example myFirstExample = new Example();
  3. Example mySecondExample = myFirstExample;
  4. // Set the name
  5. myFirstExample.name = "first example";
  6. // Reassign the first example and set its name
  7. myFirstExample = new Example();
  8. myFirstExample.name = "new first example";
  9. // Print the names
  10. System.out.println(myFirstExample.name);
  11. System.out.println(mySecondExample.name);
The output will be:
Expand|Select|Wrap|Line Numbers
  1. new first example
  2. first example
This is because mySecondExample is pointing to the original value of myFirstExample which itself however is now pointing to a new value.

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:
Expand|Select|Wrap|Line Numbers
  1. final Example myConstant = new Example();
  2. myConstant.name = "constant"; // This is fine as you're changing the object, not the constant
  3. myConstant = new Example(); // This is not possible as you would be reassigning the constant
I hope that helps
Feb 20 '14 #2
Sherin
77 New Member
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.
Sep 8 '20 #3
UnKnownCode
1 New Member
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 heap
Sep 8 '20 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Robert Oschler | last post by:
What is the object returned by a call to mysql_fetch_object()? Is it an array, a class, what? I know I can access fields from the result object using the "->" operator, but how can I...
5
by: Eric Lilja | last post by:
Using a macro, can I change what type an object is being cast to? I know, the initial respone to question might be an instinctive "ugly, don't even think about it!" or "don't use macros at all",...
100
by: E. Robert Tisdale | last post by:
What is an object? Where did this term come from? Does it have any relation to the objects in "object oriented programming"?
2
by: ElanKathir | last post by:
Hi ! What is ViewState object ? What is use of that? and I wrote one Function in Server side and also Client Side. I want to run both one by one. Like Validation and Submit. Thanks &...
2
by: Michael Murschell | last post by:
In vb, how do I find out what type of control an object is? In other words, I want to find out if the Object LastName is a textbox or an image object.
4
by: recurr | last post by:
How to get what a variable is dimmed as? How to get what an object is declared as? If variable or object is Nothing? I want to do something like the following, but TypeOf and GetType doesn't...
1
by: robospy | last post by:
Hi What is variable aging? I was asked this question by some friend related to C discussions. Robo
2
by: sumsin | last post by:
What is type_info object?
5
by: bynbutterfly | last post by:
What is global object?(not global parameter) Please give me an example.
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.