Re: Difference between String variable and String Class definition



On Mon, 27 Mar 2006 13:41:45 -0800, vahid.xplod wrote:

hi ,
can anyone tell me what is difference between String variable and
String class definition.
for example :

String variable ---> String = "java";
String Class ---> String = new String("java");

in two example above, i want to know that every two definition are same
as each other about memory allocation or not ?

The first line allocates memory for the literal "java". It then points the
variable to this location.
The second line allocates space for the literal. It then creates a new
String class. It copies the string from the first place to the new place
in memory. Finally, it points the variable to that location.
All you do in the second example is create more work for the computer
and use up more memory (until the next garbage collection).

Consider what
String var - new String(new String(new String("java")))
would do.
....
A series of memory allocations would occur with "java" being copied
from one place to the next.

.



Relevant Pages

  • Re: remove first line in a textbox (readonly)
    ... The array returned is necessarily a copy of the actual data; at best, the array needs to be created and initialized for each call to the getter, and it's entirely possible all of the string instances are created as well. ... magic of memory allocation work here, my old school c training tells ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Doubts on Defining and declaring variables
    ... 1.Dim iage ... and allocate memory for each of them ... My understanding:-Still No memory allocation done at this ... Now since string is assigned to iage,So will ...
    (microsoft.public.scripting.vbscript)
  • Re: MASD syntax to load the current address in C.A at runtime
    ... so my memory on A=PC is not as good. ... CODE object, the CRC on the CODE object will change. ... easier to store individual pieces as the time string was built. ...
    (comp.sys.hp48)
  • Re: Fast string operations
    ... Looping: I thought looping over arrays in managed code was "slow" ... array handling and such. ... The problem with TrimHelper is that it always returns a new string instance. ... The customer perceives this as a memory leak. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Discovering variable types...
    ... >- but I suppose MS expect us to use wrappers ... memory allocations for your variables from disk as well. ... >They most certainly are of fixed size, changing the size of a String ... >>me to keep buffer size and current postion right in the memory block. ...
    (comp.lang.pascal.delphi.misc)