Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String

 
free hoo
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please any one explain what a reference means .
when written
String str = new String();
str is a reference to a new object , but what does str contain.
what if i write
String str="abc";
now what str contains.
freehoo
 
Eric Edwards
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by free hoo:
please any one explain what a reference means .
when written
String str = new String();
str is a reference to a new object , but what does str contain.
what if i write
String str="abc";
now what str contains.
freehoo

when written
String str = new String();
at this point str contains NULL--nothing.
String str="abc";
now what str contains.
at this point str contains "abc"
 
Paul Selby
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String str;
str is NULL
String str = new String()
str refers to a String, which is empty ""
str = "abc"
str refers to a String containing "abc"
You had asked "what a reference means", so I not sure what you mean by "contains". str is said to contain "abc", generally speaking. The nitpick would be that technically str is a reference variable and not an object, so it doesn't contain "abc" but refers to the object.
 
Allen Alchian
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Paul according to my understanding of strings. To get more specific, str will contain the memory address of where the string "abc" is found...that is what is meant by a reference.
 
Stephanie Grasson
Ranch Hand
Posts: 347
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there is a clear and simple explanation of references here:
http://www.javaranch.com/campfire/StoryCups.html
 
free hoo
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi everybody
thank you .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic