Why “new�

  • Thread starter Lawrence D'Oliveiro
  • Start date
A

Arved Sandstrom

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

This here is for me the best reason to have/keep the "new" operator in
Java - documentation. I'm satisfied that it was originally picked
because some other languages used it for object creation in similar
circumstances, so it's familiar, which is not a bad reason. But I really
don't care why, it hasn't caused me any anguish since 1998. And it is
helpful as an indicator, as you say.

AHS
 
A

Arved Sandstrom

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> {}
Well, no, you probably couldn't fudge it. In fact, it would take you
like 2 minutes with an editor and shell/command prompt to find out that
two Java source files with

public class GenericClass<X, Y> { }

and

public class ShortName extends GenericType<X, Y> { }

don't compile. Something like

public class ShortName extends GenericClass<String, Integer> { }

does work (akin to a "specialization"). Perhaps you can discover why
your general idea had a problem.

AHS
 
L

Lew

That’s where the term “virtual†comes from.

No, that is completely mistaken. C++ adopted the term because it used the
technique. Smalltalk had it before C++.
Without understanding the C++ spec, you cannot understand what “virtualâ€
means.

What a bullshit thing to say. Surely you aren't serious. You could, for
example, understand the Java spec. Or you could understand the Smalltalk
spec. Or you could understand
http://en.wikipedia.org/wiki/Dynamic_dispatch

It remains that in Java, static and private methods are not virtual, a point
you seem to want to refute by talking about the C++ spec. That's like trying
to understand a Porsche by reading a Ford repair manual.

In any event, you're wasting your time trying to refute a point that happens
to be factual and verifiable.

Without understand what a "virtual" method is, you won't understand the Java spec.

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

Lew

jlp said:
from wikipedia : http://en.wikipedia.org/wiki/Virtual_function
so private methods can be virtual

Not in Java, they can't.
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.

So you contradict yourself. Make up your mind.

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

Lew

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

Again, wrong. The concept pre-dates C++ and is defined in several languages
both before and since. C++ definitions are completely irrelevant to Java. In
case you haven't noticed, THEY'RE DIFFERENT LANGUAGES!

Next you'll try to make the claim that Java "references" aren't what they are
because the term means something different in C++. It's a stupid argument.

THEY'RE DIFFERENT LANGUAGES! You cannot use C++ definitions for Java. You
can use industry terminology, of course, and "virtual function" is not limited
to C++. You've been given two Wikipedia links that explain that. You might
want to consider actually reading them and correcting your wrong-headed notions.

Smalltalk had virtual methods years before C++. C++ didn't invent the idea.

Simply stating the same wrong idea over and over doesn't make it true, bub.

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

Lew

Ah, so you know as little about C++ as you do about Java.

And doomed to remain that way since he won't read references, won't listen to
reason and won't accept logic.

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

Lew

How many hundreds of times a day do Java programm[er]s 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>;


OOOhh! You're right! That's simply TERRIBLE! I sure hope you didn't sprain
a finger with all that typing!
because Java has no typedefs like C++:

typedef GenericClass<Type1, Type2>
ShortName;

Unnecessary feature and one of limited benefit.
Or you could probably fudge it:

class ShortName extends GenericClass<Type1, Type2> {}

As long as 'Type1' and 'Type2' are actual types, as your nomenclature hints,
and not type parameters. 'ShortName' is not a typedef, though, it's a new type.

I think you'll just have to put up with typing Ctrl-V or letting your IDE
autocomplete all those horrible, finger-spraining, documentation-improving,
safety-guaranteeing, extremely low-overhead, helpfully redundant type
declarataions. Maybe you should wear a finger brace to relieve the strain.

Or spend a year tarring roofs to get a sense of perspective.

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

Arne Vajhøj

(map #(map (partial dotp %) (transpose m2)) m1)

Functional languages let you have that cake and eat it too. :)

Well - it is not particular readable.

Arne
 
A

Arved Sandstrom

Well - it is not particular readable.

Arne

You don't really end up "having that cake" with functional languages
anyway, not for matrix operations you don't (*). Not any more than you
do with Java or C# or C++. The languages that understand
vectors/matrices as first class types (APL/J, Matlab and counterparts,
etc) or that have have better support for matrix-type operations on
arrays (FORTRAN 90 with MATMUL intrinsic, or SUM on slices, for example)
are obviously best if this is what a person is concentrating on.

For any other language, including functional languages, you'd best use a
tuned library for linear algebra from the gitgo. And I wouldn't mind
seeing proper bounds checking in that little code snippet from Ken.

AHS

* "transpose" is hard to screw up in any language; Haskell has a
List.transpose function actually.
 
J

Joshua Cranmer

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

That is very arrogant of you. Especially since the JLS only uses the
word "virtual" (outside of the phrase "virtual machine") in §15:

The invocation mode, computed as follows:

If the compile-time declaration has the static modifier, then the
invocation mode is static.
Otherwise, if the compile-time declaration has the private
modifier, then the invocation mode is nonvirtual.
Otherwise, if the part of the method invocation before the left
parenthesis is of the form super . Identifier or of the form
ClassName.super.Identifier then the invocation mode is super.
Otherwise, if the compile-time declaration is in an interface, then
the invocation mode is interface.
Otherwise, the invocation mode is virtual.

[ ... ]

If class S contains a declaration for a non-abstract method named m with
the same descriptor (same number of parameters, the same parameter
types, and the same return type) required by the method invocation as
determined at compile time (§15.12.3), then:

If the invocation mode is super or interface, then this is the
method to be invoked, and the procedure terminates.
If the invocation mode is virtual, and the declaration in S
overrides (§8.4.8.1) X.m, then the method declared in S is the method to
be invoked, and the procedure terminates.


I might add that, in the JLS usage, private methods cannot be virtual
since any method with a private modifier is either static or nonvirtual.
 
J

Joshua Cranmer

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

Dynamic method dispatch has existed since Simula (long before C++ or
even C), which was, incidentally, the first language to use virtual, to
my knowledge.

It doesn't matter how *C++* defines it if you're talking about *Java*.
C++ defines an int as follows:

Plain ints have the natural size suggested by the architecture of the
execution environment; the other signed integer types are provided to
meet special needs.

So, obviously, Java ints must act the same way, by your logic. But they
don't.
 
J

Joshua Cranmer

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

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

And Java 7's Project Coin introduced the diamond notation for those
people who really can't type (or adhere strictly to 80-character line
widths):
GenericClass<Type1, Type2> x = new GenericClass<>();

But even then, it really doesn't matter that much since you can easily
get a macro in any competent IDE to fill in the LHS of that expression
for you if you really, really, really want to save keystrokes.
 
O

Owen Jacobson

And Java 7's Project Coin introduced the diamond notation for those
people who really can't type (or adhere strictly to 80-character line
widths):
GenericClass<Type1, Type2> x = new GenericClass<>();

But even then, it really doesn't matter that much since you can easily
get a macro in any competent IDE to fill in the LHS of that expression
for you if you really, really, really want to save keystrokes.

Note that this is only useful when the initialization expression has
exactly the type parameters of the declaration. In cases like

Set<? extends Number> numbers = new HashSet<Integer>();

the <> shorthand (and almost any other proposed shorthand, including,
unfortunately, inference by flow analysis) is of no use.

-o
 
L

Lars Enderin

2011-02-05 15:07, Lew skrev:
No, that is completely mistaken. C++ adopted the term because it used
the technique. Smalltalk had it before C++.

I am sure Bjarne Stroustrup took "virtual" from SIMULA (67), which
preceded Smalltalk.He based C++ on C and SIMULA. I have heard him talk
about that.
 
D

Daniele Futtorovic

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.

I was waiting for a "no" from the OP before even trying to address their
question. Funny thing it didn't come.
 
A

Arne Vajhøj

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

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

Well that does not compile.
because Java has no typedefs like C++:

typedef GenericClass<Type1, Type2>
ShortName;

Well - if you want to minimize the number of characters in the
source code, then Java is definitely not the language for you.

Arne
 
A

Arne Vajhøj

In that case, they should have kept the word “virtual†as well.

They chose to change the default on that one.
You already have that confusion, when every method declaration in Java is
effectively virtual, unlike C++.

They are not.

Stuff like that is covered in the Java tutorial which I think you
should start by reading before asking so many questions.

Arne
 
A

Arne Vajhøj

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

Understanding the C++ spec will not help you much for
understanding Java.

Arne
 
A

Arved Sandstrom

Well that does not compile.


Well - if you want to minimize the number of characters in the
source code, then Java is definitely not the language for you.

Arne

Yeah...and neither is C++.

AHS
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top