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?
Thanks,
Brian
1)
String str1; // Is the default constructor assumed here or is the object not
instantiated until it is assigned to "mystring"?
str1 = "mystring";
2)
String str1 = new String("mystring");
3)
String str1 = new String();
str1 = "mystring";
Chris Smith - 16 Mar 2004 22:40 GMT
> 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.

Signature
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Steve Horsley - 16 Mar 2004 23:34 GMT
> Hi,
>
[quoted text clipped - 9 lines]
> instantiated until it is assigned to "mystring"?
> str1 = "mystring";
Here you declare a reference to a String. At that time, the reference is not
assigned to refer to any string at all. Then you assign it to refer to a
compile-time constant, which existed before you ever referred to it, and
which you did not create yourself.
> 2)
> String str1 = new String("mystring");
Here you create a new String, using a compile-time constant as the source
of the string contents. This is pretty-much a waste of time, and you are
probably better off just saying:
String str1 = "mystring";
This avoids creating a duplicate String.
> 3)
> String str1 = new String();
> str1 = "mystring";
This creates a new empty string and assigns str1 to refer to it.
It then immediately overwrites the value of str1 to refer to a
compile-time constant String instead. The empty string you
created is lost and will eventually be garbage collected. (Unless
the compiler optimized the first line away to nothing, in which
case a String was never constructed.)
Try to understand the difference between an Object and a
reference to an Object. This line:
String str1;
does not create a String. It creates a (unassigned) reference
to a String. Conversely, this line:
new String("ABC" + "123");
creates a new string (optimizer permitting) but immediately
forgets it because no lasting reference to it is stored -
you cannot retrieve the string in any way - it has no handle.
Steve
Mythic Wave - 17 Mar 2004 00:47 GMT
Thanks for the answers. Actually I'm a very experienced C++ programmer who
is just trying to firm up the basics of Java.
One more question -
In C++ it is always necessary to use delete after new. I know this is not
necessary in Java. However I think there is an analog to delete in Java.
Why and when would it be used?
Brian
> > Hi,
> >
[quoted text clipped - 46 lines]
>
> Steve
Christophe Vanfleteren - 17 Mar 2004 01:19 GMT
> Thanks for the answers. Actually I'm a very experienced C++ programmer
> who is just trying to firm up the basics of Java.
[quoted text clipped - 6 lines]
>
> Brian
Please don't toppost.
There is no analog to delete in Java. You can't control the heap / garbage
collection in any way.
There is sort of an analog to a destructor however, which is the finalize
method. The main differences with a destructor however are:
- only the JVM itself calls the finalize method.
- it calls this when the object becomes eligible for garbage collection
- you can not be sure that this method will ever get called
- there is a performance penalty when you use finalizers

Signature
Kind regards,
Christophe Vanfleteren
Chris Smith - 17 Mar 2004 01:37 GMT
> Thanks for the answers. Actually I'm a very experienced C++ programmer who
> is just trying to firm up the basics of Java.
[quoted text clipped - 4 lines]
> necessary in Java. However I think there is an analog to delete in Java.
> Why and when would it be used?
No, there is no analog to delete in Java.

Signature
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Roedy Green - 17 Mar 2004 01:33 GMT
>String str1 = new String("mystring");
This "new" is bad form. It generates a clone of the string needlessly.
see http://mindprod.com/jgloss/newbie.html
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Tom - 17 Mar 2004 14:56 GMT
> >String str1 = new String("mystring");
>
> This "new" is bad form. It generates a clone of the string needlessly.
>
> see http://mindprod.com/jgloss/newbie.html
There is one case where it isn't bad. When you are trying to
demonstrate to someone the difference between using the equals method
and the == operator it is helpful to create two separate but equal
Strings from the same source so they can see for themselves that ==
doesn't evaluate as true, but the equals method does. For that reason
alone, I'm glad it is in the API, but I confess that I can't think of
any other reason to use that particular constructor.
Roedy Green - 17 Mar 2004 01:34 GMT
>String str1 = new String();
>str1 = "mystring";
This is just a waste of effort. Why assign a value then immediately
replace it. Further you could use "" instead of new String() to avoid
cluttering the literal pool.
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Chris Smith - 17 Mar 2004 04:48 GMT
> >String str1 = new String();
> >str1 = "mystring";
>
> This is just a waste of effort. Why assign a value then immediately
> replace it. Further you could use "" instead of new String() to avoid
> cluttering the literal pool.
Noting that performance is negligible for creation of a single blank
string object that will only live for a few microseconds, the biggest
reason NOT to do the above is that you're falsely causing definite
assignment to a variable. That prevents the calculator from catching an
attempt to use an uninitialized variable. If you intend to overwrite
the value later, then initializing to "" is just as bad as initializing
to 'new String()'.
Of course, it was just demo code anyway.

Signature
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Tom - 17 Mar 2004 05:55 GMT
> Hi,
>
[quoted text clipped - 16 lines]
> String str1 = new String();
> str1 = "mystring";
Keep in mind that your examples are using Strings. Strings are NOT
run-of-the-mill objects. For example, you can't do anything like your
first example with any other kind of object (that I am aware of).
For anything BUT Strings, the answer is pretty much 'yes' you need the
new operator. There are some exceptions, but none that a beginner is
likely to encounter for awhile.