- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
A bad edit of a quote, more than likely. It's just text, after all.
Admin
No - I'd do it the same way whether I'd imported a class or an interface (or a static import, having thought about it just seems like a little bit of a hack to get round the multiple inheritence problems.. I shall have to look up the reasons for it) - I always use: InterfaceName.ConstantName or ClassName.ConstantName.
I think it's always good practice to use the classname when referencing a static variable, just like I always use this.variableName when to distinguish from local variables (I know some people use the leading underscore.. but I
just can't get to grips with that). Plus - it triggers the code insight (my goodness I'm getting lazy)...
I can see it's nice for that - on the other hand, if a column name changes you could end up using a constant 'NAME' to refer to column 'FULLNAME' which could get nasty.
Admin
No I wouldn't leave it named incorrectly. I would refactor it. Even without an IDE, you can comment out the declaration, find all the uses of it and fix it. If you just use a literal String, you have to search for the text which tends to be less reliable and less focused. The point isn't that you never have change the rest of the code (I kind of suggested that, sorry), it's getting the compiler to catch my errors.
Admin
However (getting back to the second half of the WTF), if they had been assigned literal values, such as
a = "This";
b = "is";
c = "a test.";
... then it is safe to assume that the compiler would perform constant folding on them in the case of concatenation; in fact, if a, b, and c are not reassigned elsewhere, and s is never reassigned, then compiler should eliminate the variables entirely and simply use the literal "This is a test." wherever there is a subsequent reference to s.
In the case where only two of the values were constant, then if the constants are successive, such as in
a = Math.random();
b = " is a ";
c = "random number.";
String s = a + b + c;
Then it would (presumably) substitute the two literals and concatenate that to the non-literal:
String s = a + " is a random number.";
Only if all of the literals are disjoint, or all the values are non-literal, would it perform the whole series of cancatenations as written at runtime.
On the other hand (as I understand it), by explicitly using a Stringbuffer and the append() method, the compiler doesn't get the option to do that; since it is a method invocation rather than a built-in operator expression, the compiler cannot assume that the result of the operations will be a constant, even when the arguments are constant (consider for a moment the case of a method that has a side effect such as printing to the console). This is true even though the compiler uses the same method internally for performing non-literal concatenation at runtime.
(As an aside, constant folding is one of the reasons why arguments for functional programming: if the compiler knows that the result of a function will be fixed for a given constant argument, then it pre-compute the operation and substitute the function call with the constant result.)
Comments and corrections welcome.
Admin
While I agree that the above is possible, I'm not so sure you can count on this behavior. The JLS states that literals must be concatenated at compile time. I don't believe it requires compilers to analyze non-literals to determine if they never change. (If a, b, and c are final, then I think it will concatenate at compile time but I'm not sure off the top of my head.)
An aggressive optimizing compiler might be likely to do as you describe.
Admin
I showed my 7 month old son that, and even he started crying. Perhaps he saw the tears in my eyes first, but I'm not so sure
Admin
What, dubwai? You think that "VB programmers" have never heard of swapping the values of two things (usually memory locations or registers) using XOR? Maybe some of them haven't, but some of us have. Some of us used to write IBM System/390 assembler code, anyway.
Admin
int var2 = 123;
// Swap them using XOR'ing :)
var1 ^= var2;
var2 ^= var1;
var1 ^= var2;
// Et voila - swapped them - that wasn't too hard now was it? :)
VB coders are prolly scratching their head now - but well ..."
Admin
That wasn't my post. Again.
Admin
and anonymous methods as well. nullable types. And http://research.microsoft.com/comega/ has stuff going on that will be hard to reach by Java. then again, you never know when MS is going to use it.
Still some delphi features I'm missing in C# though, like delegates by interface, and strictly typed types:
http://www.netindustry.nl/blog/2005/04/wheres-in-c.html
Wiebe
Admin
Where did it go wrong? [6][:#]
Admin
.NET does the same thing as java, in that it performs the concatenation at compile time instead of at runtime when possible, i.e. when the strings are literals or constants as opposed to, say, return values from method calls.
Admin
why this wasnt put into a stored procedure in the first place....?
Admin
According to a recent article I read (I wish I could find the link, but it was an interview with some Java expert), using the plus sign isn't actually that bad, since Java 1.6 now uses the StringBuilder class automatically. The article said that doing it the semantically right way was best in the long run because future implementation changes wouldn't leave your code behind.