Re: basic question about New operator

From: Chris Smith (cdsmith_at_twu.net)
Date: 03/16/04


Date: Tue, 16 Mar 2004 14:40:52 -0700

Mythic Wave wrote:
> Hi,
>
> I'm not too experienced with Java and have a question about the 'new'
> operator. Is this always required? What is the difference and the
> advantages / disadvantages between the following 3 sets of statements?

First of all, do you understand the basics of references and objects in
Java? If not, it's best to start there.

That said, new is *always* used to create a new object directly.
However, there are ways that you can get other things to create objects
for you. In that case, you won't use new because it's not your code
that is creating the object.

As for the differences:

> 1)
> String str1; // Is the default constructor assumed here or is the object not
> instantiated until it is assigned to "mystring"?
> str1 = "mystring";

There's no constructor at all here. You are obtaining, from the Java
virtual machine, a reference to a String object. Of course, the VM may
use a constructor to build the String object before giving it to you...
(or it may not; since it's the virtual machine, it doesn't always have
to follow language rules). However, *you* aren't creating any objects;
only asking the VM for a reference to an object.

> 2)
> String str1 = new String("mystring");

Here, you are first asking the VM for an object, and then directly
creating an object of your own. You're using the 'new' keyword for that
second part. However, the object you are creating is going to be
identical to the one you got from the VM, so there's really no reason to
create the extra object; but it won't do any more harm except taking up
a bit of extra memory.

> 3)
> String str1 = new String();
> str1 = "mystring";

Here, you are directly creating an object of your own, and then asking
the virtual machine for an object. After you ask the virtual machine
for the object, you're throwing away your only way of finding that first
object you created... so eventually it will go away. You once again
didn't need to create it at all, but it won't do any harm except making
more work for the garbage collector.

-- 
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation


Relevant Pages

  • Re: How java passes object references?
    ... Which everybody knows, it doesn't in Java. ... The reason I think this is a useful clarification is that when you got to the part about how passing by reference might work, it seems you went off track at least partly because you didn't understand the nature of the above. ... is a pointer pointing at the memory block. ... Assignments to local variables, or even to class members, do not allocate memory. ...
    (comp.lang.java.programmer)
  • Re: The Java no pointer big fat lie!
    ... > reference types, it wouldn't be a good comparison. ... and what you can't - and there is a big difference in Java to C and C++. ... You cannot change it in the language itself, ... Thus I would distinguish pointers from ...
    (comp.lang.java.programmer)
  • Re: call by reference
    ... We started learning Java about one month ago, and they just don't believe me when i try to explain that there is a confusion when passing reference types as an argumetn to a function. ... The actual parameter must be an L-value. ...
    (comp.lang.java.programmer)
  • Re: programming concepts > specific languages
    ... >actually changes the target of the reference passed to 'function2'. ... Java from a source that was not precise about terminology. ... if I have a class MyObject and write ... I thought primitive types in particular behave in the ...
    (comp.programming)
  • Re: no pointer in Java => my problem
    ... Does Java pass objects by reference or by value? ... f could also modify the StringBuffer by appending " World" for instance. ...
    (comp.lang.java.programmer)