• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

BufferString

 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at the following code:
StringBuffer test = new StringBuffer("nothing");
test.insert(3, "atall");
Now my questionn is if the test will refernce the same instance as after the construction. I know that if I will call
System.out.println(test);

I will see "notatallhing", so the test refers another instance, isnt'it?

 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, insert will return a new instance of test.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Angela, i don't think it will create a new instance . This is the definition of StringBuffer given in javadoc . Correct me if i am wrong.
A string buffer implements a mutable sequence of characters. A string buffer is like a String, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls like append() or insert ().
 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah i suppose you are right. It will just modify the string
and return the same instance as mentioned in the API


Returns:
a reference to this StringBuffer object.


 
Cristian Negresco
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
What is an instance?
When two instances are identical?
I think that only the reference remains the same, I mean the test, the "handle", but the instance to which it points has changed. It contains something else, isnt'it?, so it has changed.
So, what is an instance???
Thanks,
Cristian
 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Christein
Let me try to help you.
objects are known as instances of class.
for example in the following code:
class Box{
....
.....
Box mybox=new Box();
.....
....
}
mybox will be an instance of Box,or a Box object called mybox is created.Each time you create an instance of a class you are creating an object of a class(using new operator).
Also the statement
Box mybox=new Box();
can be written in two steps,
Box mybox;//declares reference to an object,
mybox=new Box();//allocates a Box object

so when I say
StringBuffer test=new StringBuffer("Christein");
an object or instance (named test)of StringBuffer class is created.after
test.insert(5,"al");
the o/p is
Chirstaltein
so we are not creating a new instance of the class,only the contents of the object gets modified.
Hope this helps.
 
Cristian Negresco
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the answer,
But I think I need more help...

So
1. "objects are known as instances of class." I'm OK with that.
2."so we are not creating a new instance of the class,only the contents of the object gets modified." I'm OK with that too.
In this to sentences is a contradiction, I think:
So, you are agree that the contents of the object are modified, so the object is modified!, Sorry...
I think that is rather a question of wrong spelling, I know that there is no other "new" object created and that the "test" refers to the same location of memory where the "object" was changed...
Let's put it in another way, let's asume that the code looks like that:
String test = new String("nothing");
test.concat("atall");
Only in this case the instance to wich "test" refers wasn't changed, simply because after line two a new String is created.
I still think that in THIS case(sorry for caps) the instance to which "test" refers wasn't changed and in the first one it was.
Perhaps I need more help.
Thanks,
Cristian
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One basic thing to know is that String instances are immutable, i.e. once created there is no way to change the value it contains. Every instance method called onto a String object will return ANOTHER String reference if the content of the object has to be changed. For instance,
String t = "String1";
String s = t.trim();
In this case, t and s refer TO THE SAME STRING object in memory since nothing had to be "changed" in the String "String1". When I say "changed", I do NOT mean that the String itself would have been changed if necessary but merely that if some changes were required then a new String would have been created and assigned to s.
In this example two distinct instances of String exist, because one space has to be removed from t and thus a new String had to be created, so both references refer to distinct memory area.
String t = "String1 ";
String s = t.trim();
Now the second basic thing to know is that a StringBuffer implements a mutable version of the String class, i.e. a StringBuffer, once created, may be changed by mutator methods, and if the method returns a StringBuffer object then it is the same StringBuffer object than the one on which the method has been invoked (in the case of an instance method), i.e this StringBuffer.
I really hope it helps because these concepts are very central to Java and anyone has to master them...
Let us know if you need more help
Val
 
Cristian Negresco
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
First of all thank you for your answer.
I am and I was agree with you, and I understood the priciple.
The main idea was that the instance is not changed but their content is. In my opinion that means the instance was changed.
I thinf that is rather a language problem, but I will accept this paradox.
Thank you all,
Cristian
 
It will give me the powers of the gods. Not bad for a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic