Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

String Object

  Asked By: Gail    Date: Jun 14    Category: Java    Views: 523
  

If u add a String to an existing Strig object, will it create a new location in
memory?
How?

e.g
String s ="a";
s = s+"b";

Are they two objects?

Please let me know how they will manage in memory?

Share: 

 

9 Answers Found

 
Answer #1    Answered By: Chione Massri     Answered On: Jun 14

First step : string  s ="a"; line create  a new string object  and load JVM.
Object reference is s

Second step : s =s+"b"; line create a other new string Object and load JVM.
Object reference is s. But, first step object isn't a reference.
Garbage Collector remove it.

 
Answer #2    Answered By: Haya Yoshida     Answered On: Jun 14

In response to the reply on using a StringBuffer instead of a '+' operator,
Why not use the concat(String str) method in the String Object api?

This would work the same, no?

String a="";
for(int i=0;i<20000;i++){
a = a.concat(i);
}

Please let me know which is better. I am a little curious now. Or is it
that a '+' operator and a concat method are exactly the same as in your
code1 example and the code2 example it still the better choice?

 
Answer #3    Answered By: Geneva Morris     Answered On: Jun 14

The concat method is creating a new String object, which wastes memory. By
using a StringBuffer and the append method, you save it from creating (and
eventually destroying) an object. It may not seem like much, but do it a couple
hundred times a second and the wasted cycles will start to pile up.

 
Answer #4    Answered By: Fabia Ferrrari     Answered On: Jun 14

U 'll be right, i have never used concat before. so i decided to test with
2million
the same operations at once. And the result;


"+" operand java assembly codes will be as follows cost:more than 2 minutes

public synchronized class test extends Object
{
Method public void <init>()
>> max_stack=1, max_locals=1 <<
0 aload_0
1 invokenonvirtual #1 <Method Object.<init>():void>
4 return
Line number table:
pc line
0 1
Method public static void main(String[])
>> max_stack=2, max_locals=2 <<
0 ldc #2 <String "test">
2 astore_1
3 new #3 <Class StringBuffer>
6 dup
7 invokenonvirtual #4 <Method StringBuffer.<init>():void>
10 aload_1
11 invokevirtual #5 <Method StringBuffer.append(String):StringBuffer>
14 ldc #6 <String "1">
16 invokevirtual #5 <Method StringBuffer.append(String):StringBuffer>
19 invokevirtual #7 <Method StringBuffer.toString():String>
22 astore_1
23 return
Line number table:
pc line
0 6
3 7
23 9
}


String.concat() method will be as follows cost: more than 1minutes

public synchronized class test extends Object
{
Method public void <init>()
>> max_stack=1, max_locals=1 <<
0 aload_0
1 invokenonvirtual #1 <Method Object.<init>():void>
4 return
Line number table:
pc line
0 1
Method public static void main(String[])
>> max_stack=2, max_locals=2 <<
0 ldc #2 <String "test">
2 astore_1
3 aload_1
4 ldc #3 <String "1">
6 invokevirtual #4 <Method String.concat(String):String>
9 astore_1
10 return
Line number table:
pc line
0 6
3 7
10 9
}



with stringbuffer object  cost:235 millis

public synchronized class test extends Object
{
Method public void <init>()
>> max_stack=1, max_locals=1 <<
0 aload_0
1 invokenonvirtual #1 <Method Object.<init>():void>
4 return
Line number table:
pc line
0 1
Method public static void main(String[])
>> max_stack=3, max_locals=3 <<
0 new #2 <Class StringBuffer>
3 dup
4 ldc #3 <String "test">
6 invokenonvirtual #4 <Method StringBuffer.<init>(String):void>
9 astore_1
10 aload_1
11 ldc #5 <String "1">
13 invokevirtual #6 <Method StringBuffer.append(String):StringBuffer>
16 pop
17 aload_1
18 invokevirtual #7 <Method StringBuffer.toString():String>
21 astore_2
22 return
Line number table:
pc line
0 6
10 7
17 8
22 10
}

 
Answer #5    Answered By: Anuja Shah     Answered On: Jun 14

String is not a mutable object.

String s ="a";
s = s+"b";

when u compile the code above, "+" operator will be changed by your compiler;

String s=new String("a");
StringBuffer sbimp = new StringBuffer(s);
sbims.append("b");
s=sbims.toString();

so it's not recomended to use "+" operator so much.

for ex:

code1
------------------------------
String a="";
for(int i=0;i<20000;i++){
a=a+i;
}



code2
-------------------------------------------
String a="";
StringBuffer sb="";
for(int i=0;i<20000;i++){
sb.append(i);
}



code2 will be 1000 times faster cause first code means.

String a="";
for(int i=0;i<20000;i++){
String a=new String("a");
StringBuffer sbimp = new StringBuffer(a);
sbims.append("b");
a=sbims.toString();
}

 
Answer #6    Answered By: Emma Campbell     Answered On: Jun 14

I have very little knowladge regarding it. I heared
String is an Immutable class thus objects are
unchangable. So whenever an attempt is made to modify
a String object, a new object  is created in a new
memory location. But I don't know how can I create  my
own immutable class and what is the usage of creating
such classes? I'll be greatful if anybody of the group
can answer.

 
Answer #7    Answered By: Kellie Bishop     Answered On: Jun 14

As far as creating your own class with an immutable object, I'm not sure if
there's a way to lock a class member variable after it's been initialized by the
constructor other than to keep it private and not provide any methods to change
the value.

Immutable objects are generally useful if you want data that can't be changed
when passed into a function. For instance, if you have a regular mutable object
that's passed into a function, the function can alter that object  before it
finishes. There's really nothing particularly exciting about the concept from a
programmer's perspective, but it's probably really useful if you're designing a
language and need object wrappers for primitive types.

 
Answer #8    Answered By: Mona Wagner     Answered On: Jun 14

Sure... Just don't include any methods that change the state of the object
and make your class final. There is no real magic behind strings - except
for the overloaded + operator (grumble grumble... too bad Sun apparently
thought that we were to stupid to do our own operator overloading... grumble
grumble....). It just doesn't include any methods to change it's internal
state...

eg:

final class C1
{
private int value;

public C1(int v)
{
this.value = v;
}


/* Rather than doing:

public C1 add(int val)
{
this.value += val;
}

do...
*/


public C1 add(int val)
{
return new C1(this.value + val);
}
}

 
Answer #9    Answered By: Eloise Lawrence     Answered On: Jun 14

String is an inmutable object, so its contents
cannot be changed. When you do the following:

String s = "a";
s = s + "b";

is the same that

s = new String(s + "b");

So, the first object  is not referenced anymore, and
will be cleaned by the garbage collector.

 
Didn't find what you were looking for? Find more on String Object Or get search suggestion and latest updates.




Tagged: