== 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.
> !!!!!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?
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
> > !!!!!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
> !!!!!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.
@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.
> @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.
> 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.
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 :) )
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.