6/22 How come in Java, I can do the following:
str = str + "Hello world";
instead of
str = new(str + "Hello world");
\_ Because you are not telling the new keyword
what type of class to create. The + operator
has been overloaded so that you can cat strings
together.
\_ java doesn't support operator overload
\_ It's actually a very simple modification of the parser grammar
and a few additional AST (abstract syntax tree) nodes. The two
turn out identical code anyway and is was done simple because
they can and makes it more convenient to use (though it makes
the grammar syntax less consistent).
\_ See the JLS under the section for java.lang.StringBuffer
(20.13) for how they suggest you compile the string +
operator; it's instructive even if you aren't writing a
compiler. -brg
\_ you mean "as well as", not "instead of".
Why = because programmers are lazy, and sometimes stupid.
\_ better short cut and work than longer and not works, stupid j
\_ #F, programmers, especially those in the INDUSTRY, are
always stupid. They always do short cut that work but
are lousy implementations. Then they leave the company
and the new hires get to debug their code. That is INDUSTRY
\_ shut up kchang.
\_ This is a bit unfair. Industry programmers have this thing
called a 'deadline'. They're not stupid. Apathetic and
under a lot of pressure is more like it. No more stupid
than a 'non-industry' programmer whatever that's supposed
to mean.
\_ yeah, and grad students don't have deadlines
\_ "Professor? I need a few more weeks." "Ok."
"Boss? I need a few more weeks." "Tell me
again how much I pay you?" Hardly the same.
Don't fool yourself into thinking academics
work as hard as industry folks. I've done
both for a few years each and academic work is
like being on vacation in comparison.
\_ Why is this a religious issue in the first place?
Strings are treated in Java more like base types
in the syntax even though they are instances of
the String class because of the prevalence of
Strings. One can think of them as char*. Versions
of C++
String templates offered this as an overloaded operator before
there was Java. Anyway, if you don't like overloaded operators,
don't use them. williamc
\_ Why is this a religious issue in the first place? Strings are\
treated in Java more like base types in the syntax even though they are instanc\
es of the String class because of the prevalence of Strings. One can think of th\
em as char*. Versions of C++
String templates offered this as an overloaded operator before there was Java. A\
nyway, if you don't like overloaded operators, don't use them. williamc
\_ You could but you should use StringBuffer when doing
string manipulations (i.e., StringBuffer.append()).
JVM coverts String objects to StringBuffer objects when
you perform a concatenation on two Strings and that slows
your program down. |