We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,318 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

what's mean by object reference in java

Hi budy can you explain anyone which i have writen in the article title

5
Contributors
5
Replies
1 Day
Discussion Span
10 Months Ago
Last Updated
6
Views
kathir100
Newbie Poster
3 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

String str = "asd";

The String: "asd" is referred to by the object reference variable: str.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

which basically means that "asd" is stored somewhere in your memory.
if you then perform:
str = "new asd";

this doesn't mean that the object "asd" doesn't exist anymore, but you don't have a reference to it anymore. now str will be a reference to "new asd" while "asd" remains on the same position in your memory, ready to either be referenced to again, or to be cleaned up.

stultuske
Industrious Poster
4,489 posts since Jan 2007
Reputation Points: 1,377
Solved Threads: 627
Skill Endorsements: 25

stultuske, I thought once there is no reference to an object anymore, the object is destroyed... in that case, how would "asd" be referenced again? by recreating it? or am I missing something?

bibiki
Posting Whiz
375 posts since Nov 2009
Reputation Points: 44
Solved Threads: 31
Skill Endorsements: 0

Strings are handled differently.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

When your object has no remaining references it can be garbage collected. Because you have no remaining references there is no way ever to get it again (excluding very technical/obscure stuff with garbage collection internals).
As Norm says Strings are highly optimised in Java, so depending on how "asd" is created Java may keep that string in memory and re-use that memory later if you create another "asd" object - but this is strictly an internal optimisation and has no effect on how your program runs, other than maybe to make it faster.

JamesCherrill
... trying to help
Moderator
8,667 posts since Apr 2008
Reputation Points: 2,636
Solved Threads: 1,476
Skill Endorsements: 33

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.0703 seconds using 2.71MB