Why “new�

  • Thread starter Lawrence D'Oliveiro
  • Start date
L

Lawrence D'Oliveiro

Java copied C++ in using the word “new†to indicate class instantiation on
the heap. (Of course, Java is different in not having any other kind of
class instantiation.)

But what purpose does this word serve? Java defines no meaning for
“classname(args)†different from “new classname(args)â€. How many hundreds of
times a day do Java programms write something like

classname varname = new classname(args);

compared to, dare I say it, Python:

varname = classname(args)

I’m surprised Java never added a shorter form, say

classname varname(args);

which is of course straight out of C++.
 
A

Arne Vajhøj

Java copied C++ in using the word “new†to indicate class instantiation on
the heap. (Of course, Java is different in not having any other kind of
class instantiation.)

But what purpose does this word serve? Java defines no meaning for
“classname(args)†different from “new classname(args)â€. How many hundreds of
times a day do Java programms write something like

classname varname = new classname(args);

compared to, dare I say it, Python:

varname = classname(args)

Java is very inspired from C++ syntax wise.

When the rest of the language is C++ like it would
have confused people not to use C++ syntax for
object creation as well.

Consistent style.

And BTW the use of new is not just a C++ thing - other
language use a new keyword (with different syntax) for
object creation: Pascal, VB, Perl.
I’m surprised Java never added a shorter form, say

classname varname(args);

which is of course straight out of C++.

But with a different semantic than Java, so it
would really be confusing.

Arne
 
D

Daniele Futtorovic

Java copied C++ in using the word “new” to indicate class instantiation on
the heap. (Of course, Java is different in not having any other kind of
class instantiation.)

But what purpose does this word serve? Java defines no meaning for
“classname(args)” different from “new classname(args)”. How many hundreds of
times a day do Java programms write something like

classname varname = new classname(args);

compared to, dare I say it, Python:

varname = classname(args)

I’m surprised Java never added a shorter form, say

classname varname(args);

which is of course straight out of C++.

Are you trolling?
 
L

Lawrence D'Oliveiro

Java is very inspired from C++ syntax wise.

When the rest of the language is C++ like it would
have confused people not to use C++ syntax for
object creation as well.

Consistent style.

In that case, they should have kept the word “virtual†as well.
And BTW the use of new is not just a C++ thing - other
language use a new keyword (with different syntax) for
object creation: Pascal, VB, Perl.


But with a different semantic than Java, so it
would really be confusing.

You already have that confusion, when every method declaration in Java is
effectively virtual, unlike C++.
 
M

Mike Schilling

Lawrence D'Oliveiro said:
In that case, they should have kept the word “virtual†as well.


You already have that confusion, when every method declaration in Java is
effectively virtual, unlike C++.

That's not true, you know.
 
O

Owen Jacobson

Java defines no meaning for “classname(args)†different from “new
classname(args)â€.

Not so. The former is parsed as a method call; the latter as a
new-instance expression. Nothing, save good sense, prevents you from
naming a method the same thing as a class (either the class containing
it or some other class entirely).

A better comparison would've been to languages like Python, where class
creation is little more than another kind of function call (both
syntactically and semantically), or to Smalltalk, where instance
creation is defined somewhat independently for each class in the system
via a class method (and "core" library classes like Object provide the
real machinery via virtual class methods, something absent from Java).

-o
 
L

Lawrence D'Oliveiro

Private methods are not virtual, nor are static methods.

Private methods can still be virtual—see pages 189-190 of the C++ spec for
an example. And I can’t see any prohibition in section 10.3 (“Virtual
functionsâ€) against them being static.

Like I said, in Java, effectively all methods are virtual.
 
J

Joshua Cranmer

Private methods can still be virtual—see pages 189-190 of the C++ spec for
an example. And I can’t see any prohibition in section 10.3 (“Virtual
functionsâ€) against them being static.

Java private and static methods are not virtual.
 
L

Lew

Private methods can still be virtual—see pages 189-190 of the C++ spec for
an example. And I can’t see any prohibition in section 10.3 (“Virtual
functionsâ€) against them being static.

Reference to the C++ spec sheds absolutely no light whatsoever on Java.
Like I said, in Java, effectively all methods are virtual.

Wrong. Private and static methods are not virtual.

--
Lew
Ceci n'est pas une fenêtre.
..___________.
|###] | [###|
|##/ | *\##|
|#/ * | \#|
|#----|----#|
|| | * ||
|o * | o|
|_____|_____|
|===========|
 
J

Joshua Cranmer

Java copied C++ in using the word “new†to indicate class instantiation on
the heap. (Of course, Java is different in not having any other kind of
class instantiation.)

But what purpose does this word serve? Java defines no meaning for
“classname(args)†different from “new classname(args)â€. How many hundreds of
times a day do Java programms write something like

classname varname = new classname(args);

compared to, dare I say it, Python:

varname = classname(args)

I’m surprised Java never added a shorter form, say

classname varname(args);

which is of course straight out of C++.

First off: `new' is a good visual indicator when quickly scanning code.

Saving keystrokes is not all that important--if it were, then you'd end
up with wonderfully terse programming languages like APL. I don't care
if I can write a matrix multiplication in a single line of code, I won't
be able to understand it 6 months later.

The Python way of doing things makes sense only because classes are
basically glorified functions. The same syntax in Java is syntactically
identical to a function call--so now you get syntactic ambiguity you
resolve at semantic time. Oh joy [1].

The last form shares the problem I mentioned earlier--it fails to stand
out suitably. There is also the minor point that it only is really
usable during variable declaration. The C++ form only makes sense as an
extension of the variable declaration, particularly about the
distinction between stack- and heap-storage variables.

To make the long story short: the current syntax isn't particularly
verbose (until you get generics), and shorter versions have problem.

[1] One point to make: `a b(c);' is a very syntactically ambiguous
phrase in C++. It can be either a function prototype declaration or a
declaration of a variable b of type a with an arg c. C++ has been,
justifiably in my opinion, accused of being very difficult to parse.
 
L

Lew

Daniele said:
Are you trolling?

Unanswerable question about language design motivation: check.
Comparison to other languages making them seem better: check.
Denigration of language feature ("I'm surprised that..."): check.
Rejection of factual statements that contradict poster: check.
Obfuscated reasoning with shifting context: check.
/Ad hominem/ attacks: nope.
Defensive stance: nope.
Arrogant or aggressive tone: nope.

--
Lew
Ceci n'est pas une fenêtre.
..___________.
|###] | [###|
|##/ | *\##|
|#/ * | \#|
|#----|----#|
|| | * ||
|o * | o|
|_____|_____|
|===========|
 
K

Kevin McMurtrie

"Mike Schilling said:
Private methods are not virtual, nor are static methods.

And HotSpot may change methods between static and virtual at runtime
too. It's an expensive optimization but it's how Java sometimes
achieves performance comparable to C. Without it, it would never come
close.
 
M

Mike Schilling

Lawrence D'Oliveiro said:
Private methods can still be virtual—see pages 189-190 of the C++ spec for
an example. And I can’t see any prohibition in section 10.3 (“Virtual
functionsâ€) against them being static.

Like I said, in Java, effectively all methods are virtual.

You're either trolling or simply not paying attention. In either case, I'm
done.
 
L

Lew

Kevin said:
And HotSpot may change methods between static and virtual at runtime
too. It's an expensive optimization but it's how Java sometimes
achieves performance comparable to C. Without it, it would never come
close.

I get what you mean, but I'm pretty sure "static" is the wrong word there.
"static" means "belongs to the class, not an instance". Virtual methods
always belong to an instance, so they can never be static.

--
Lew
Ceci n'est pas une fenêtre.
..___________.
|###] | [###|
|##/ | *\##|
|#/ * | \#|
|#----|----#|
|| | * ||
|o * | o|
|_____|_____|
|===========|
 
L

Lawrence D'Oliveiro

How many hundreds of times a day do Java programms write something like

classname varname = new classname(args);

And this just gets worse with generic types, e.g.

GenericClass<Type1, Type2> x = new GenericClass<Type1, Type2>;

because Java has no typedefs like C++:

typedef GenericClass<Type1, Type2>
ShortName;

Or you could probably fudge it:

class ShortName extends GenericClass<Type1, Type2> {}
 
L

Lawrence D'Oliveiro

Reference to the C++ spec sheds absolutely no light whatsoever on Java.

That’s where the term “virtual†comes from.
Wrong. Private and static methods are not virtual.

Without understanding the C++ spec, you cannot understand what “virtualâ€
means.
 
J

jlp

Le 05/02/2011 07:45, Lawrence D'Oliveiro a écrit :
That’s where the term “virtual†comes from.


Without understanding the C++ spec, you cannot understand what “virtualâ€
means.
from wikipedia : http://en.wikipedia.org/wiki/Virtual_function
so private methods can be virtual

In Java, all non-static methods are by default "virtual functions." Only
methods marked with the keyword final, which cannot be overridden, along
with private methods, which are not inherited, are non-virtual.
 
L

Lawrence D'Oliveiro

Virtual methods always belong to an instance, so they can never be static.

Considering that “virtual†is a concept defined by C++, and C++ doesn’t
define it that way...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top