[5228] in java-interest

home help back first fref pref prev next nref lref last post

Re: Is 'new' really necessary?

daemon@ATHENA.MIT.EDU (Arthur van Hoff)
Fri Feb 2 17:07:35 1996

Date: Fri, 2 Feb 1996 09:33:23 -0800
From: Arthur.Vanhoff@Eng.Sun.COM (Arthur van Hoff)
To: kkobashi@ix6.ix.netcom.com
Cc: java-interest@java.Eng.Sun.COM


Hi Kerry,

> Can someone tell me why the 'new' keyword was used in Java?
> It appears to me that its only use is as a token to
> denote that a class instanciation is coming up next.
> It seems redundant in that regard.

It is my fault. Despite it being somewhat redundant, I suggested that
it was changed to the way it is because it makes heap allocations more
explicit. Apart from strings and string concatenation, all heap allocations
in Java are accompanied by the "new" keyword in the code. There are no 
suprises like in C++ where the compiler allcoates temporary objects
when needed, which can make your programs very inefficient. In Java you
know what is going on, because you write the code.

> Given:
>    (a) Date d = new Date(CurrentTime);      // Java compiler likes
>    (b) Date d(CurrentTime);                 // Java compiler doesn't like
>    (c) Date d;                              // Java compiler likes
> 
> Statement (a) uses the 'new' keyword. The disadvantage
> is that it messes up C++ programmers to thinking that a dynamic
> variable is being created (yes, Java doesn't have pointers but some
> habits are hard to break). 

I'm not sure if I understand. It does allocate a heap allocated
object.

> In addition, one has to type the class
> symbol again, and the equal character.

But you can write:

	Foo f = new Bar();
	
as long as Bar is compatible with Foo.
	
> Statement (b) is far more terse and natural for C++ programmers.

I disagree. In C++ these objects are stack allocated and not heap
allocated, the sematics are totally different.

> Statement (c) needs to be put in contrast to the following:
>    Date d = new Date();
> 
> I hope you see my point. An instanciation is an instanciation.
> Why must I think about a "new" object?

Despite it being more verbose, I think it is more clear to use (a).
Anyway, it is too late to change.

Have fun,

	Arthur van Hoff
	
-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post