Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #25771 > unrolled thread

Why “new”?

Started byLawrence D'Oliveiro <ldo@geek-central.gen.new_zealand>
First post2011-02-05 13:53 +1300
Last post2011-02-05 02:09 +0100
Articles 14 — 8 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Why “new”? Lawrence D'Oliveiro <ldo@geek-central.gen.new_zealand> - 2011-02-05 13:53 +1300
    Re: Why ?new?? Owen Jacobson <angrybaldguy@gmail.com> - 2011-02-05 10:46 -0500
    Re: Why “new”? Lawrence D'Oliveiro <ldo@geek-central.gen.new_zealand> - 2011-02-05 19:29 +1300
      Re: Why “new”? Lew <noone@lewscanon.com> - 2011-02-05 09:22 -0500
      Re: Why “new”? Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-02-05 10:41 -0500
    Re: Why “new”? Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-02-04 23:43 -0500
      Re: Why “new”? Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-02-05 09:31 -0400
    Re: Why “new”? Daniele Futtorovic <da.futt.news@laposte.net.invalid> - 2011-02-05 18:43 +0100
    Re: Why “new”? Arne Vajhøj <arne@vajhoej.dk> - 2011-02-04 20:05 -0500
      Re: Why “new”? "Mike Schilling" <mscottschilling@hotmail.com> - 2011-02-04 17:33 -0800
        Re: Why “new”? Lawrence D'Oliveiro <ldo@geek-central.gen.new_zealand> - 2011-02-05 14:53 +1300
      Re: Why “new”? Lawrence D'Oliveiro <ldo@geek-central.gen.new_zealand> - 2011-02-05 14:24 +1300
    Re: Why “new”? Lew <noone@lewscanon.com> - 2011-02-04 23:47 -0500
    Re: Why “new”? Daniele Futtorovic <da.futt.news@laposte.net.invalid> - 2011-02-05 02:09 +0100

#25771 — Why “new”?

FromLawrence D'Oliveiro <ldo@geek-central.gen.new_zealand>
Date2011-02-05 13:53 +1300
SubjectWhy “new”?
Message-ID<iii71v$3ud$1@lust.ihug.co.nz>
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++.

[toc] | [next] | [standalone]


#25810 — Re: Why ?new??

FromOwen Jacobson <angrybaldguy@gmail.com>
Date2011-02-05 10:46 -0500
SubjectRe: Why ?new??
Message-ID<2011020510462785394-angrybaldguy@gmailcom>
In reply to#25771
On 2011-02-05 10:41:20 -0500, Joshua Cranmer said:

> On 02/05/2011 01:29 AM, Lawrence D'Oliveiro wrote:
>> In message<iii71v$3ud$1@lust.ihug.co.nz>, Lawrence D'Oliveiro wrote:
>> 
>>> 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>;
> 
> 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

[toc] | [prev] | [next] | [standalone]


#25824

FromLawrence D'Oliveiro <ldo@geek-central.gen.new_zealand>
Date2011-02-05 19:29 +1300
Message-ID<iiiqnu$ev4$1@lust.ihug.co.nz>
In reply to#25771
In message <iii71v$3ud$1@lust.ihug.co.nz>, Lawrence D'Oliveiro wrote:

> 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> {}

[toc] | [prev] | [next] | [standalone]


#26014

FromLew <noone@lewscanon.com>
Date2011-02-05 09:22 -0500
Message-ID<iijmfr$bh3$1@news.albasani.net>
In reply to#25824
On 02/05/2011 01:29 AM, Lawrence D'Oliveiro wrote:
> In message<iii71v$3ud$1@lust.ihug.co.nz>, Lawrence D'Oliveiro wrote:
>
>> 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|
|_____|_____|
|===========|

[toc] | [prev] | [next] | [standalone]


#26143

FromJoshua Cranmer <Pidgeot18@verizon.invalid>
Date2011-02-05 10:41 -0500
Message-ID<iijr30$7a4$3@news.eternal-september.org>
In reply to#25824
On 02/05/2011 01:29 AM, Lawrence D'Oliveiro wrote:
> In message<iii71v$3ud$1@lust.ihug.co.nz>, Lawrence D'Oliveiro wrote:
>
>> 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>;

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.
-- 
Beware of bugs in the above code; I have only proved it correct, not 
tried it. -- Donald E. Knuth

[toc] | [prev] | [next] | [standalone]


#25864

FromJoshua Cranmer <Pidgeot18@verizon.invalid>
Date2011-02-04 23:43 -0500
Message-ID<iiikhp$7s4$1@news.eternal-september.org>
In reply to#25771
On 02/04/2011 07:53 PM, Lawrence D'Oliveiro wrote:
> 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.

-- 
Beware of bugs in the above code; I have only proved it correct, not 
tried it. -- Donald E. Knuth

[toc] | [prev] | [next] | [standalone]


#25990

FromArved Sandstrom <asandstrom3minus1@eastlink.ca>
Date2011-02-05 09:31 -0400
Message-ID<bjc3p.10257$lO1.8539@newsfe11.iad>
In reply to#25864
On 11-02-05 12:43 AM, Joshua Cranmer wrote:
> On 02/04/2011 07:53 PM, Lawrence D'Oliveiro wrote:
>> 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.
[ 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
-- 
We must recognize the chief characteristic of the modern era - a 
permanent state of what I call violent peace.
-- James D. Watkins

[toc] | [prev] | [next] | [standalone]


#26011

FromDaniele Futtorovic <da.futt.news@laposte.net.invalid>
Date2011-02-05 18:43 +0100
Message-ID<iik281$30a$1@news.eternal-september.org>
In reply to#25771
On 05/02/2011 05:47, Lew allegedly wrote:
> Lawrence D'Oliveiro allegedly wrote:
>>> 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++.
>
> Daniele Futtorovic wrote:
>> 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.

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

-- 
DF.

[toc] | [prev] | [next] | [standalone]


#26020

FromArne Vajhøj <arne@vajhoej.dk>
Date2011-02-04 20:05 -0500
Message-ID<4d4ca255$0$23765$14726298@news.sunsite.dk>
In reply to#25771
On 04-02-2011 19:53, Lawrence D'Oliveiro wrote:
> 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

[toc] | [prev] | [next] | [standalone]


#26032

From"Mike Schilling" <mscottschilling@hotmail.com>
Date2011-02-04 17:33 -0800
Message-ID<iii9dq$3pr$1@news.eternal-september.org>
In reply to#26020

"Lawrence D'Oliveiro" <ldo@geek-central.gen.new_zealand> wrote in message 
news:iii8s4$4tt$1@lust.ihug.co.nz...
> In message <4d4ca255$0$23765$14726298@news.sunsite.dk>, Arne Vajhøj wrote:
>
>> 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.
>>
>>> 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.
>
> You already have that confusion, when every method declaration in Java is
> effectively virtual, unlike C++.

That's not true, you know. 

[toc] | [prev] | [next] | [standalone]


#26115

FromLawrence D'Oliveiro <ldo@geek-central.gen.new_zealand>
Date2011-02-05 14:53 +1300
Message-ID<iiiaia$5t9$1@lust.ihug.co.nz>
In reply to#26032
In message <iii9dq$3pr$1@news.eternal-september.org>, Mike Schilling wrote:

> "Lawrence D'Oliveiro" <ldo@geek-central.gen.new_zealand> wrote in message
> news:iii8s4$4tt$1@lust.ihug.co.nz...
>>
>> You already have that confusion, when every method declaration in Java is
>> effectively virtual, unlike C++.
> 
> That's not true, you know.

No, I don’t know. Please explain.

[toc] | [prev] | [next] | [standalone]


#26225

FromLawrence D'Oliveiro <ldo@geek-central.gen.new_zealand>
Date2011-02-05 14:24 +1300
Message-ID<iii8s4$4tt$1@lust.ihug.co.nz>
In reply to#26020
In message <4d4ca255$0$23765$14726298@news.sunsite.dk>, Arne Vajhøj wrote:

> 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.
> 
>> 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.

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

[toc] | [prev] | [next] | [standalone]


#26102

FromLew <noone@lewscanon.com>
Date2011-02-04 23:47 -0500
Message-ID<iiikos$s44$1@news.albasani.net>
In reply to#25771
Lawrence D'Oliveiro allegedly wrote:
>> 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++.

Daniele Futtorovic wrote:
> 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|
|_____|_____|
|===========|

[toc] | [prev] | [next] | [standalone]


#26156

FromDaniele Futtorovic <da.futt.news@laposte.net.invalid>
Date2011-02-05 02:09 +0100
Message-ID<iii7vl$t6u$1@news.eternal-september.org>
In reply to#25771
On 05/02/2011 01:53, Lawrence D'Oliveiro allegedly wrote:
> 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?

-- 
DF.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web