Comparing Strings

Why:String a = "AAA";String b = "AAA";if (a == b) --> is truevs.String a1 = new String( "AAA");String b1 = new String("AAA");if (a1==b1) --> is falsewhat's the different?
[256 byte] By [Jyigala] at [2007-11-27 1:40:57]
# 1
Did this come up in your code? Why are you asking?
DrLaszloJamfa at 2007-7-12 0:55:48 > top of Java-index,Java Essentials,Java Programming...
# 2
http://www.leepoint.net/notes-java/data/strings/12stringcomparison.html
CaptainMorgan08a at 2007-7-12 0:55:48 > top of Java-index,Java Essentials,Java Programming...
# 3

== tests for Object equality, i.e. the a == b means a and b are references to the same object. Any time you specify "AAA" in your code the JVM will give you a reference to the same object. However using "new" will create a new Object each time, therefore a1 and b1 will not point to the same object, instead they will both have their own String objects with the value "AAA".

What you really want to check is that the value of a1 is equal to the value of b1. This is what the equals method is for. a1.equals(b1)

should return true.

Hope that makes sense.

JasonChoya at 2007-7-12 0:55:48 > top of Java-index,Java Essentials,Java Programming...
# 4
!!!!!Never use constructor for String class!!!!!
J@A@V@Aa at 2007-7-12 0:55:48 > top of Java-index,Java Essentials,Java Programming...
# 5

> !!!!!Never use constructor for String class!!!!!

And why's that? If I do, for example:

String test;

for(int iCounter=0; iCounter < 100; iCounter++)

test += String.valueOf(iCounter);

It will give me a nullpointerexception. While:

String test = new String();

for(int iCounter=0; iCounter < 100; iCounter++)

test += String.valueOf(iCounter);

will work fine. So please tell me, why shouldn't I use the constructor? Isn't it designed to be used? Otherwise, String should be an Abstract class, shouldn't it?

Nemesish3da at 2007-7-12 0:55:48 > top of Java-index,Java Essentials,Java Programming...
# 6

You certainly have to initialise a variable before you use it! Quite commonly:String test = "";

for(int iCounter=0; iCounter < 100; iCounter++) {

test += String.valueOf(iCounter);

}

In general you should preferString foo = "whatever";

to using the constructorString foo = new String("whatever");

To see why - and for a couple of examples where you wouldn't do this, have a read of http://mindprod.com/jgloss/interned.html

pbrockway2a at 2007-7-12 0:55:48 > top of Java-index,Java Essentials,Java Programming...
# 7
Thank you pbrockway2. :)I was going to say the ame thing.
J@A@V@Aa at 2007-7-12 0:55:48 > top of Java-index,Java Essentials,Java Programming...
# 8
good site :) could be helpful
Nemesish3da at 2007-7-12 0:55:48 > top of Java-index,Java Essentials,Java Programming...
# 9
> String test = "";> for(int iCounter=0; iCounter < 100; iCounter++) {>test += String.valueOf(iCounter);You should prefer StringBuilder in this example, anyway...
DrLaszloJamfa at 2007-7-12 0:55:48 > top of Java-index,Java Essentials,Java Programming...
# 10

> > !!!!!Never use constructor for String class!!!!!

>

> And why's that? If I do, for example:

>

> String test;

> for(int iCounter=0; iCounter < 100; iCounter++)

>test += String.valueOf(iCounter);

> will give me a nullpointerexception. While:

>

> String test = new String();

> for(int iCounter=0; iCounter < 100; iCounter++)

>test += String.valueOf(iCounter);

> ll work fine. So please tell me, why shouldn't I use

> the constructor? Isn't it designed to be used?

> Otherwise, String should be an Abstract class,

> shouldn't it?

where do you work? I want to make sure I never have to unravel any of your ultra-obscure bugs

georgemca at 2007-7-12 0:55:48 > top of Java-index,Java Essentials,Java Programming...
# 11

> !!!!!Never use constructor for String class!!!!!

Not entirely true.

String s1 = "... some very, very long string ...";

...

String s2 = new String(s1.subString(start, verySmallLength));

If we don't need s1 any more, but we do need s2 for a long time, we'll want to do new String(). If we don't, then the backing char array for s1 will be shared b2 s2, and we'll be eating up a lot of memory for just a few characters.

jverda at 2007-7-12 0:55:48 > top of Java-index,Java Essentials,Java Programming...
# 12
thanks pbrockway2 - that's was an interesting link.
Jyigala at 2007-7-12 0:55:48 > top of Java-index,Java Essentials,Java Programming...
# 13
>where do you work? I want to make sure I never have to unravel any of your ultra-obscure bugsWhy is it buggy? I truly don't know why it's buggy... I haven't encountered any bug using the constructor this way since I started using Java, about 4 years ago...
Nemesish3da at 2007-7-12 0:55:48 > top of Java-index,Java Essentials,Java Programming...
# 14

@jverd, have you looked at the source code for String lately? I just checked my copies of JDK 1.6, 1.5 and 1.4.2, and in all of them, substring() uses new String(String) internally unless the substring is exactly the same as the original (in which case it returns the original). And in all of them, that constructor creates a new array if the new string is smaller than the original. I, too, have been hearing for years that substring() keeps the whole backing array alive; I wonder if it was ever true?

There are circumsatnces in which it's appropriate to use one or another of the String constructors, but they're rare. New Java programmers would be well advised to pretend they don't exist. If they find themselves in a situation where they think using one of the String constructors is appropriate, they've probably misunderstood the problem.

uncle_alicea at 2007-7-12 0:55:48 > top of Java-index,Java Essentials,Java Programming...
# 15
Thanks for the interned strings link. Thats my one new thing learnt for the day. :-)
TimSparqa at 2007-7-21 20:12:21 > top of Java-index,Java Essentials,Java Programming...
# 16
> Thats my one new thing learnt for the day. :-) One and a half if you count UncleAlice's comment. (The link deals with the substring business amongst other things.)
pbrockway2a at 2007-7-21 20:12:21 > top of Java-index,Java Essentials,Java Programming...
# 17

> @jverd, have you looked at the source code for String

> lately? I just checked my copies of JDK 1.6, 1.5 and

> 1.4.2, and in all of them, substring() uses new

> String(String) internally unless the substring is

> exactly the same as the original (in which case it

> returns the original).

In my version of 1.4.2, substring() uses this code if the substring is shorter than the original:new String(offset + beginIndex, endIndex - beginIndex, value)

And that calls this code:// Package private constructor which shares value array for speed.

String(int offset, int count, char value[]) {

this.value = value;

this.offset = offset;

this.count = count;

}

So that isn't new String(string) at all. I don't have a Java 5 JDK on this computer so I can't comment on later versions.

DrClapa at 2007-7-21 20:12:21 > top of Java-index,Java Essentials,Java Programming...
# 18

> I don't have a Java 5 JDK on this computer so I can't comment on later versions.

1.6 is like this. substring() checks that the indices are within bounds then, unless the substring is the whole string, calls the private String(int,int,char[]) constructor.

This should not be confused with the public String(char[],int,int) constructor which does copy the value array.

pbrockway2a at 2007-7-21 20:12:21 > top of Java-index,Java Essentials,Java Programming...
# 19
You're right; it's the same in 1.5, too. I wasn't paying attention to which constructor it was calling.
uncle_alicea at 2007-7-21 20:12:21 > top of Java-index,Java Essentials,Java Programming...
# 20

String test = new String();

> for(int iCounter=0; iCounter < 100; iCounter++)

>test += String.valueOf(iCounter);

Could anyone tell me why georgemc thinks this is wrong? (not talking about purpose :) )

Nemesish3da at 2007-7-21 20:12:21 > top of Java-index,Java Essentials,Java Programming...
# 21

See reply 9. StringBuilder sb = new StringBuilder();

for (int iCounter = 0 ; iCounter < 100 ; iCounter++) {

sb.append(String.valueOf(iCounter);

}

String test = sb.toString()

I don't know if that was what georgemc was referring to - I wouldn't say it is an "ultra-obscure" bug, so I guess not. It is preferred over what you provided for performance reasons however.

TimRyanNZa at 2007-7-21 20:12:21 > top of Java-index,Java Essentials,Java Programming...
# 22
> @jverd, have you looked at the source code for String> lately? Nope. Thanks for setting me straight. :-)
jverda at 2007-7-21 20:12:21 > top of Java-index,Java Essentials,Java Programming...