aspose file tools*
The moose likes Cattle Drive and the fly likes String storage Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "String storage" Watch "String storage" New topic
Author

String storage

Betty Reynolds
Ranch Hand

Joined: Feb 16, 2000
Posts: 111
I keep forgetting that declarations in Java are just references (as Paul keeps reminding me). Therefore, I tend to declare my variables outside of a loop, when actually a declaration inside would have no impact on storage. I'm am constantly reminded to make my declarations immediately before I use them (sorry Paul, it will sink in someday).
However, in the case of strings, I'm confused. Wouldn't it better to declare the storage for the string outside of a loop first. For example:
String someString = new String();
while ( some condition)
{.
.
.
someString = String.valueOf( some variable )
.
.
}
This is probably a dumb question, but I'm looking for some illumination on how best to set up storage for strings, or if that is even necessary.
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20582
    ∞

This will do you no good.
Strings are immutable. So when you say String s = new String(); you just created a string that contains nothing and can never be changed. Then, when you say s = "moo"; you threw out your empty string and have a whole new string.
StingBuffer is a bit different. You can change that one all you want. StringBuffer s = new StringBuffer(); s.append("moo"); There is only one StringBuffer object.

permaculture Wood Burning Stoves 2.0 - 4-DVD set
Betty Reynolds
Ranch Hand

Joined: Feb 16, 2000
Posts: 111
Thanks Paul. That was illuminating. When to use String vs. Stringbuffer is beginning to make sense.
However, I'm still puzzled by another point. Is it really preferable in the the java world to make your declarations inside loops (e.g. ints, doubles, etc). I know they are just memory addresses, but aren't you generating an address each time the statement is encountered versus one time if they are declared before the loop?
Admittedly, if there are a large number of statements inside the loop, you sacrifice clarity, maintainability, etc., if you make your declarations before, but I find it hard to believe that this is more important than efficiency (in terms of storage).
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
Java is both a compiled and an interpreted language, and to get a grip on issues like this it sometimes helps to think of what the compiler does as well as imagining the code being interpreted as it runs. An uninitialized variable declaration doesn't really have a use in the running code, it's more of a hint to the compiler to reserve a name for some storage. As such it doesn't really exist in the code at all, but is used to generate details of things like stack frames and compiler checks on use of variables outside their scope. Consider the two simple examples below:
<pre>
private void doit()
{
int x;
for (int i = 0; i < 10; ++i)
{
x = i;
}
}
</pre>
and
<pre>
private void doit()
{
for (int i = 0; i < 10; ++i)
{
int x;
x = i;
}
}
</pre>
I hope you can see that the assignment of i to x takes place every time round the loop, but the hint to the compiler that x is an int could be in either place with no difference (except in the scope of the variable x.)
Although an initialized variable may seem different, really it's
just a simplified syntax for an uninitialized declaration followed by an assignment (in almost all cases, but "final" variables are a little different.) It's safe to assume that the declaration part takes place right at the start of the method, while the assignment part must be done where required. As another example:
<pre>
private void doit()
{
for (int i = 0; i < 10; ++i)
{
String s = "hello " + i;
}
}
</pre>
is equivalent to (except for scope, as mentioned above)
<pre>
private void doit()
{
String s;
for (int i = 0; i < 10; ++i)
{
s = "hello " + i;
}
}
</pre>
but not equivalent to
<pre>
private void doit()
{
String s = "hello ";
for (int i = 0; i < 10; ++i)
{
s = s + i;
}
}
</pre>
To summarize: Declarion of a variable and assignment of a value to it may be considered as two distinct steps. You should chose where in you code to do each of these steps based on different criteria.
Declarion of a variable should be placed as close to its use as possible. This helps to improve readability and prevent inadvertent name clashes.
Initialization of a variable should be done as soon as possible, after the variable has been declared and all the values needed to initialize it are available. This helps to improve performance by reducing duplicated processing in loops
In many cases these two forces lead to declaration and initialization occurring at the same place in the code. In these situations, use the combined declaration and initialization syntax


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20582
    ∞

When I first started programming decades ago, efficiency was the most important thing. You had so little memory, every byte counted.
Today, readability is the most important thing. As programs become huge and complex, you need to be looking out for the geek that fills your shoes somewhere down the road. But! If you can squeeze in something is also efficient along the way, that never hurt anyone.
Let's look at code sample 1:

And now look at sample 2:


I think there is no argument that code sample 2 is more readable - BUT! it also turns out to be more efficient. In code sample 1, six integers are given space on the stack. In code sample 2, even if both conditions evaluate to true, there are only three integers on the stack at any time.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String storage