[5229] in java-interest
Re: Is 'new' really necessary?
daemon@ATHENA.MIT.EDU (David Geary)
Fri Feb 2 17:34:39 1996
Date: Fri, 2 Feb 1996 09:36:07 -0700
From: David.Geary@Central.Sun.COM (David Geary)
To: java-interest@java.Eng.Sun.COM, kkobashi@ix6.ix.netcom.com
> 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.
>
> Given:
> (a) Date a = new Date(CurrentTime); // Java compiler likes
> (b) Date b(CurrentTime); // Java compiler doesn't like
> (c) Date c; // 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).
Actually, that's exactly what's happening: new Date(CurrentTime)
instantiates an object on the heap and assigns (the object reference)
a to it.
Java may not have pointer syntax, or the ability to access individual
memory locations, but each Java object is really a reference to an
object, so in some sense, _everything_ in Java is a pointer. There
are no local (stack-based) objects in Java.
Realize that (c) above creates a null REFERENCE to a Date, and does
not create a local Date object, as it would in C++. Also note that
the following:
Date a = new Date(CurrentTime);
Date c;
c = a;
is equivalent to the following C++ code:
Date& a = *(new Date(CurrentTime));
Date& c = 0;
c = a;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
David Geary "You and I have nothing more to say
geary@rmtc.Central.Sun.COM I will do more than talking some day"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com