[5243] in java-interest
Re: Is 'new' really necessary?
daemon@ATHENA.MIT.EDU (Mete Senocak)
Sat Feb 3 01:42:20 1996
Date: Fri, 02 Feb 1996 21:19:00 -0800
From: Mete Senocak <msenocak@arachnid.microweb.com>
To: kkobashi@ix6.ix.netcom.com
Cc: java-interest@java.sun.com
Hi Kerry:
kkobashi@ix6.ix.netcom.com wrote:
>
> 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 d = new Date(CurrentTime); // Java compiler likes
> (b) Date d(CurrentTime); // Java compiler doesn't like
> (c) Date d; // Java compiler likes
>
No, it's not redundant. It works the same way as malloc() in C. It allocates
memory for the "new" object. When you type "Date d;", you only tell the
compiler what type of data you are introducing, but no physical memory is
allocated yet. First with "Date d = new (Date(CurrentTime);" the compiler
allocates memory space for the "new" instance of the class Date, and that new
instance is an object with the name "d". Now you can store data in it and/or use
its instance methods.
I hope this makes the thing a bit clearer for you.
Best regards,
-Mete
-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com