My understanding in "new" keyword

B

Bruce Sam

I think,if not use the "new" keyword,it will not create an object,only
create a reference.For example,"String str = "ab";" only create a
reference.But when I use "String str = new String("ab");",here will
create an string object and a reference str point to it.
Is it right?
 
H

hilz

Bruce Sam said:
I think,if not use the "new" keyword,it will not create an object,only
create a reference.For example,"String str = "ab";" only create a
reference.But when I use "String str = new String("ab");",here will
create an string object and a reference str point to it.
Is it right?


No quite.
in both cases you will get a reference to a String object.
String str1="ab";// gives you a reference to a string from the string pool.
if it does not already exist, it is created.
String str2="ab";// again will give you a reference to the same "ab" in the
line above.
String str3= new String("ab");// this will always creates a new String
object.

str1==str2 will give true because str1 and str2 are two references to the
same object (a sting in the string pool).
str1==str3 will give false because str3 is reference to a new string object
created using "new"
str1.equals(str3) will give true because the value of str1 is equal to str3

In general, you would rarely need to create a string like str3 was created.
you should almost always do it as in str1.
To compare strings, you will most probably want to use equals(...).
HTH
hilz
 
V

VisionSet

hilz said:
No quite.
in both cases you will get a reference to a String object.
String str1="ab";// gives you a reference to a string from the string pool.
if it does not already exist, it is created.

However, note that this is peculiar to Strings and typically objects are
only created with the new keyword. Strings have lots of shortcuts which are
potentially confusing to newbies, though speed up the code writing when you
understand them.
 
M

Michael Borgwardt

VisionSet said:
However, note that this is peculiar to Strings and typically objects are
only created with the new keyword.

Or through clone(). Or through Deserialization.
 
S

Starshine Moonbeam

Bruce said:
I think,if not use the "new" keyword,it will not create an object,only
create a reference.For example, String str = "ab";

Actually, for a String, it'll do exactly that. Repetition of that will
just create a new memory reference though. Unless you use new, the
string will only be created once. You don't need to ever
use String whatever = new String(); unless you want to. I'm not
sure if other classes do it (can't think of any) but I know String does
for sure. If you later instantiate say String str = new String("the
exact same thing"); and try to determine equivalency using == it comes
back false. Always use the equals method to determine equivalency of
strings.

boolean x = string1.equals(string2);

AHA! Primitive types. That's what I was thinking of. You don't need to
use new for the primitive types.

int x = 4;

is the same as

Integer xx = new Integer(4);

x == xx; // returns true

StringBuffer you have to use new for.
 
G

Grant Wagner

Starshine said:
AHA! Primitive types. That's what I was thinking of. You don't need to
use new for the primitive types.

int x = 4;

is the same as

Integer xx = new Integer(4);

x == xx; // returns true

That's autoboxing in Java 1.5 (5.0, whatever). For the source:

public class IntTest {
public static void main(String[] args) {
int x = 4;
Integer xx = new Integer(4);
System.out.println(x == xx);
}
}

if you try to compile it prior to 1.5, you get:

IntTest.java:5: operator == cannot be applied to int,java.lang.Integer
System.out.println(x == xx);
^
1 error

Personally I find the autoboxing behavior distasteful. It seems to me it
will result in a whole generation of programmers that don't understand that
an -int- is not the same as an -Integer-. It may also result in a new
generation of programmers complaining about how "slow" Java is as you see
code like:

Integer ii = new Integer(0);
Integer jj = new Integer(0);
for (ii = 0; ii < 100000; ++ii) {
for (jj = 0; jj < 100000; ++jj) {
// do stuff
}
}
// WHY IS JAVA SO SLOW!
 
S

Starshine Moonbeam

Grant Wagner said:
Starshine said:
AHA! Primitive types. That's what I was thinking of. You don't need to
use new for the primitive types.

int x = 4;

is the same as

Integer xx = new Integer(4);

x == xx; // returns true

That's autoboxing in Java 1.5 (5.0, whatever). For the source:

public class IntTest {
public static void main(String[] args) {
int x = 4;
Integer xx = new Integer(4);
System.out.println(x == xx);
}
}

if you try to compile it prior to 1.5, you get:

IntTest.java:5: operator == cannot be applied to int,java.lang.Integer
System.out.println(x == xx);
^
1 error

Personally I find the autoboxing behavior distasteful. It seems to me it
will result in a whole generation of programmers that don't understand that
an -int- is not the same as an -Integer-. It may also result in a new
generation of programmers complaining about how "slow" Java is as you see
code like:

Integer ii = new Integer(0);
Integer jj = new Integer(0);
for (ii = 0; ii < 100000; ++ii) {
for (jj = 0; jj < 100000; ++jj) {
// do stuff
}
}
// WHY IS JAVA SO SLOW!

Could you go in to a little more detail? Never heard the term autoboxing
before. Plus, I'm not seeing how int isn't the same as Integer stuff =
new Integer(number)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,689
Members
48,796
Latest member
Greg L.

Latest Threads

Top