aspose file tools*
The moose likes Beginning Java and the fly likes How code takes buffer space Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How code takes buffer space" Watch "How code takes buffer space" New topic
Author

How code takes buffer space

Santosh Kumar
Greenhorn

Joined: Jan 03, 2002
Posts: 28
hi
can anybody tell me when code in java takes memory space and when not.Just have a look on this code and tell me.

1. public class Test
{
2. public static void main(String arg[])
{
3. StringBuffer sbLocal = new StringBuffer("1");

4. if(sbLocal.toString().trim().equals("1"))
{
5. System.out.println("equals");
}
}
}
now tell me about each level.
like at level 3 new variable created so it takes new space in buffer.
Tell me about statements
sbLocal.toString()
and sbLocal.toString().trim()
and sbLocal.toString().trim().equals("1")
whether every statement takes a new buffer space or not.
like as in sbLocal.toString() i am changing it to String type variable so does it store the value at new location or simply uses the value from StringBuffer variable location only.I am confused.
And once again i am doing trim() of that changed string ie (sbLocal.toString().trim()) so does it once again store the value at new location or simply uses the StringBuffer variable location value.
tell me in details about these things in detail.
thanks
santosh


Santoh Kumar<br /> India<br />SCJP 1.4 <br />SCWCD 1.4
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2848
    
  11

Generally speaking, you don't need to know when Java is creating new objects. It's good to have a general idea though, so you don't write grotesquely bloated code.
When you create the StringBuffer, a new object is created obviously. When you append stuff to the StringBuffer, it could force it to allocate a new internal array, and do an array copy. (If that's a big problem, you can construct the StringBuffer with a given initial capacity.) When you call stringBuffer.toString(), interestingly no new object is created (in most Java implementations.) However, if you then try to append something to the StringBuffer, the JVM have to create a String first to match the original "toString" call. That's true even if you are no longer using the String.
OK, we're just getting going. Any operation you do on a String creates a new String. For example, myString.trim() creates and returns a new String. I think that's true even if there's nothing to trim, but I'm not sure. All the methods for get substrings, or converting case, etc. also create brand new String objects.
Santosh Kumar
Greenhorn

Joined: Jan 03, 2002
Posts: 28
thank u for ur reply it was very good and satisfying answer for me.
but still As u said that StringBuffer whenever changed to string using toString() method it does not takes memory space then from where it takes the value on doing operation like equals to equate with some string shown below
sb.toString().equals("abc") where
sb is object of type StringBuffer.

basically how it works.
waiting for ur reply.....
santosh
india
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2848
    
  11

Basically, Strings are immutable. That means they can never change from their constructed value. Any time you think you are changing a String, you are actually making a new String.
StringBuffers of course don't have that property. You can change the contents as often as you like, and you will never get a new StringBuffer. As I mentioned before, you may get a new internal character array as the contents grow to large for the old one, but you have some control over that.
Most people use StringBuffers to build up a String, then use the toString() method and never use the StringBuffer. Therefore most JVMs make an optimization. When you call toString(), a String is created, but it shares the internal character array with the StringBuffer. Yes, you have a new String object, but most of the space it would take is shared with the StringBuffer. If you then try to change the contents of the StringBuffer, the two objects can no longer share the internal character array, so a copy must be made. The point is to defer the memory allocation as long as possible in the hope that it can be avoided entirely.
Santosh Kumar
Greenhorn

Joined: Jan 03, 2002
Posts: 28
Thank you.
first time such concept i have heard about StringBuffer and String sharing the same location untill some operation on StringBuffer.thats very efficient.
its very knowledgeable for me.
thank you once again.
i hope i will come with some new questions next time.
regards
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How code takes buffer space