1) What are three ways of creating a String object ?
2) What is the difference between string concat method and simply adding 2 strings ?

Recommended Answers

All 11 Replies

don't just copy paste your homework here. what do you think they are? we'll help you improve your knowledge by correcting you, or by giving additional explanation if you don't understand it, doesn't mean we'll just do your homework for you

Please explain exactly what is confusing you. I assume you have Googled these questions and read some of the standard answers.

i know the 2 ways of creating a String object and wanna ask the third way ...
N its not my homework i like to study through online tutorials n i got stuck betweeen concat method of string and simply adding 2 strings

OK!

"abc"
new String(something);
string1 + string2

The only difference I know between concat and + is their behaviour when either of the String refs is null.

(ps please don't use abbreviations like N for "and" - many of our readers have English as a second or third language)

There are two ways to create a String object
1) String str="hello"
2) String str=new String("hello")
3) new String(str)

and
for second one look at below code

public class Concat {
    String cat(String a, String b) {
        a += b;
        return a;
    }
}

If a is null, then the concat() method NPEs but the former will treat the original value of a as if it were "null". Furthermore, the concat() method only accepts String values while the + operator will silently convert the argument to a String (using the toString() method for objects). So the concat() method is more strict in what it accepts.

Daemon: sure you're not missing some?

String bool = String.valueOf(true);

If you count methods that return a String then there will be an unlimited number of them. Internally, however, they will have to come down to one of the three techniques that the compiler understands (ps: Daemion CC - don't forget concatenation with the + operator).

James:there may be unlimited number of count methods for a string but here the question lies to differentiate between concatenation via concat() and '+' operator.

OP posted two questions:

1) What are three ways of creating a String object ?
2) What is the difference between string concat method and simply adding 2 strings ?

I think the various responses have answered both. Job done.

of course the second question already provides 2 of the required 3 methods for the answer to question 1 ;)

Yeah...Thanks for the help guyss
and now i will not use abbreviations... sorry for that :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.