Re: basic question about New operator
From: Chris Smith (cdsmith_at_twu.net)
Date: 03/16/04
- Next message: Carl Howells: "Re: program stops"
- Previous message: Phil Holland: "Re: How do you get a JVM to work with WinXP SP1 + IE6.0 SP1?"
- In reply to: Mythic Wave: "basic question about New operator"
- Next in thread: Steve Horsley: "Re: basic question about New operator"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Carl Howells: "Re: program stops"
- Previous message: Phil Holland: "Re: How do you get a JVM to work with WinXP SP1 + IE6.0 SP1?"
- In reply to: Mythic Wave: "basic question about New operator"
- Next in thread: Steve Horsley: "Re: basic question about New operator"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|