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

String and StringBuffer  RSS feed

 
Yogesh Dhond
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not been able to understand the differnce between String and StrinBuffer.
I Know that, Strings are immutable,but still one can use concatenation operator and get the results?
Also wats the difference between creating a String with "new" and assigning some string?
 
Chetan Raju
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using concatenation operation on String objects are not transparent to the user. As you said, Strings are immutable. That is once a String object is created, it cannot be changed. This is the case even in case of concatenation. The only thing is this is not known to the user.

String str1 = "hello ";
String str2 = "world ";
str1 = str1+str2;

in the above code, str1+str2 is actually creating a new object (with hello world) and then assigning it to reference variable str1. The original string object referred to by str1 (which is hello) is not changed but lost because no reference is pointing to it.
 
Srinivas Kalvala
Ranch Hand
Posts: 257
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Lets take in this way ...

String k="Hello"; // One String in pool
String j="Hai"; // Two Strings in pool
String kk=k+j; // Now Three Strings ...
String k=k+"Din"; // Now Three Strings ...

The String "Hello" pointed by k, is can't be altered , so new String HelloDin has been made and the String will reference to k. The Hello will GC. collected.

StringBuffer, will not create new Strings, but will modify the String.

The main reason behind making String as immutable is, to make the Data handling every effective in program runtime, by using the flyweight desing pattern.

Just have some hands on work on these , and will get more information.
 
Yogesh Dhond
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply!!!
but still not clear the differnce betwn
String s1= "Hello Everbody";
and
String s1 =new String();
 
Biswajit Paria
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
quote
-------------------------
Thanks for your reply!!!
but still not clear the differnce betwn
String s1= "Hello Everbody";
and
String s1 =new String();
--------------------------------


Now I will show you how it is different!

Let's create
String s1 = "Hello";
String s2 = "Hello";
till now there will be one String object in pool located at stack memory area of JVM.

Now,create
String s3 = new String("Hello");
String s4 = new String("Hello");

Here two String Object will be created and will be located at Heap memory area of JVM.

Let me know if it not clear yet.

Regards,
Biswajit.
**************************************
The only joy in the world is to begin !
 
Yogesh Dhond
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your answer
I am very clear about it now!!!
Thank you everybody for posting the reply here !!

:-)
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Biswajit Paria:
Let's create
String s1 = "Hello";
String s2 = "Hello";
till now there will be one String object in pool located at stack memory area of JVM.


you are right that there will be just one String object in the pool, but it won't be on the stack. The String object pool lives in a special area of the heap called the "permanent generation".


Now,create
String s3 = new String("Hello");
String s4 = new String("Hello");

Here two String Object will be created and will be located at Heap memory area of JVM.


That code actually creates *three* string objects - one in the pool for the literal used in the constructors, and two located in the "normal" heap.

Let me know if it not clear yet.

Regards,
Biswajit.
**************************************
The only joy in the world is to begin ![/QB]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!