Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

difference between object and object reference.  RSS feed

 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String s = new String("Vijay");
string s2;
s=s2;
System.out.println(s+""+s2="gone");

In above what is object and what is reference .
what will be o/p of this program.
[ August 06, 2007: Message edited by: dinesh tahiliani ]
 
Red Smith
Ranch Hand
Posts: 136
1
Netscape Opera Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Objects are what gets created with the "new" (in this case some String with a value of "Vijay") and the references are all the variables that have a type of some Class (s and s2) and can potentially refer an object (a instantiation or creation of that Class).

That code as written doesn't compile for me. I get an error on the assignment
of s2 to s:



I guess you can't assign a reference of "null" to another variable.

If you do the opposite:

s2 = s;

Then your output is "Vijay Vijay" because both variables then reference the same String object that was created with the value "Vijay".

[ August 06, 2007: Message edited by: Red Smith ]
[ August 06, 2007: Message edited by: Red Smith ]
 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An object, is an instance of a given class in memory. The object will have some state associated with it.

An "object reference" is a varaible that acts as a handle/pointer/marker to a given object so that its methods and fields maybe manipulated.

at least thats how I view it.

The reason you are getting the error assiging s2 to s is because you CAN NOT use an unitialised variable.

the following would also thorw and error:


you can of course if you so wish assign null to an object.

G
[ August 06, 2007: Message edited by: Gavin Tranter ]
 
nans nav
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
difference between object and object reference.
The first thing you need to be aware is that Object holds memeory and it is created using new keyword.
The Object reference is nothing but reference to that memory. It only points to memory that object holds.
As per your example
String s = new String("Vijay");// line 1
string s2;// line 2
s=s2;
System.out.println(s+""+s2);

here one object will be created at line 1 i.e. new String("vijay").
and two reference will be created at line 1 and 2 i.e String s, s2.
But here you cant use s2 unless it is not initialised. so you will get error. To overcome this initialise s2. Then both s and s2 will point to Object referred by s2.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gavin Tranter:
you can of course if you so wish assign null to an object.


No you can't. You can assign null to an object reference.

(I am only being so picky because this thread is about the difference between object and object reference)
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Red Smith:
I guess you can't assign a reference of "null" to another variable.


You can assign null to any object reference.

The problem with the original posted code is that "s2" is a local variable (one declared within a method). Local variables do not have any value until they are assigned one, and the compiler will barf if you try to read the value before one has been assigned. Contrast this to fields (declared within a class), which do have a default value when not explicitly assigned a value; for instance, all fields that are object references are null by default.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!