Newbie String Questions

D

Dom

Anyone have some time to answer simple String questions?

1. Why is there a String (String) constructor? Is there any
difference between these statements:
a) -- String s = new String ("ABC");
b) -- String s = "ABC";

2. Why is there a concat method? Is there any difference between
these statements:
a) -- s = s.concat ("ABC");
b) -- s = s + "ABC";

TIA,
Dom
 
S

Stuart Layton

I am not sure why there is a constructor, probably a relic from the
ealier versions of java.

I have never used a constructor for strings.

As for the .concat() method I would say the same thing.
 
W

Wesley Hall

Dom said:
Anyone have some time to answer simple String questions?

1. Why is there a String (String) constructor? Is there any
difference between these statements:
a) -- String s = new String ("ABC");
b) -- String s = "ABC";

Yes, there is a difference, but it is very subtle.

String s1 = new String("ABC");
String s2 = "ABC";

System.out.println(s1 == "ABC"); //This will print false
System.out.println(s2 == "ABC"); //This will print true

This is because the first time a string literal is encountered a String
object is created behind the scenes. The second time it is encountered
the string object is reused, so s1 and "ABC" point to the same string
object and == returns true. However, by doing 'new String' you are
explicitly creating a new string instance so == returns false.

It is better to use literals as it prevents garbage collector churn in
collecting all the temporary strings.

2. Why is there a concat method? Is there any difference between
these statements:
a) -- s = s.concat ("ABC");
b) -- s = s + "ABC";

Functionally, there is no difference, although there are probably some
subtle things related to the above. I can't be bothered to think it
through right now :eek:P
 
S

Steve W. Jackson

Wesley Hall said:
Dom wrote:

[ snip ]
Functionally, there is no difference, although there are probably some
subtle things related to the above. I can't be bothered to think it
through right now :eek:P

I agree that there's no functional difference, but it may be better to
use the concat method rather than the + operator.

The Javadocs for String indicate that the language has special support
for the concatenation operator (+) and indicates how it works. In the
source, you find that concat does a check for a zero-length parameter,
resulting in returning the original reference and thus no change to the
heap. But if a non-empty parameter is passed, it builds an array of
char to hold the old and new strings, then returns the result of calling
a String constructor with that array. Which is actually better probably
depends on a lot of factors, so...let's just say that the sure fire best
thing is not to do either in large quantities, preferring instead to use
a StringBuilder or StringBuffer.

= Steve =
 
O

Oliver Wong

Wesley Hall said:
Dom said:
Anyone have some time to answer simple String questions?

1. Why is there a String (String) constructor? Is there any
difference between these statements:
a) -- String s = new String ("ABC");
b) -- String s = "ABC";

Yes, there is a difference, but it is very subtle.

String s1 = new String("ABC");
String s2 = "ABC";

System.out.println(s1 == "ABC"); //This will print false
System.out.println(s2 == "ABC"); //This will print true
[...]
2. Why is there a concat method? Is there any difference between
these statements:
a) -- s = s.concat ("ABC");
b) -- s = s + "ABC";

String s1 = "ABC", s2 = "ABC";
s1 = s1.concat("");
s2 = s2 + "";
System.out.println(s1 == "ABC");
System.out.println(s2 == "ABC");

- Oliver
 

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

Forum statistics

Threads
474,270
Messages
2,571,102
Members
48,773
Latest member
Kaybee

Latest Threads

Top