what is the diffarance between object and variable

Recommended Answers

All 4 Replies

A variable is a symbol denoting a quantity or symbolic representation. A variable often represents an unknown quantity that has the potential to change.

An object is an individual unit of run-time data storage that is used as the basic building block of programs. These objects act on each other, as opposed to a traditional view in which a program may be seen as a collection of functions, or simply as a list of instructions to the computer. Each object is capable of receiving messages, processing data, and sending messages to other objects. Each object can be viewed as an independent little machine or actor with a distinct role or responsibility.

Oh, and this was easy to find by searching Wikipedia.

And when it comes to Java a variable is NEVER an object.
A variable (either a static or instance data member, technically) is always either a primitive or a reference to an object on the heap, never that object itself.

An object variable's value is the given object's address in the dynamic storage area. A normal (or primitive type) variable itself has a value.

Example:

Object c;
c = new Object();
int a = 3;
System.out.println(c);
System.out.println(a);

Output would be:
(Some memory address)
3

Note : This wouldnt be the case if a toString() method is available in Object, but the default implementation in Object prints the memory address in Dynamic Storage.

that's incorrect. The toString() method for an object can print anything, depending on what the person implementing the method decided on.
In the case of Object (and thus by default) it prints the class name followed by the hashCode of the object instance, not its memory address.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.