All times are UTC - 5 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: About Question enthuware.ocajp.i.v7.2.941 :
PostPosted: Thu May 29, 2014 3:54 am 
Offline

Joined: Tue May 20, 2014 12:41 am
Posts: 4
Am I correct, if like below

String abc = "";     
String def = abc.concat("abc");          
System.out.print(def);

then the output is abc?


Top
 Profile Send private message  
 
 Post subject: Re: About Question enthuware.ocajp.i.v7.2.941 :
PostPosted: Thu May 29, 2014 4:15 am 
Offline
Site Admin

Joined: Fri Sep 10, 2010 9:26 pm
Posts: 6799
I can tell you the answer, but you should try it out yourself by compiling and running it. If you don't understand the output, then post a question here.

thank you,
Paul.


Top
 Profile Send private message  
 
 Post subject: Re: About Question enthuware.ocajp.i.v7.2.941 :
PostPosted: Fri Nov 10, 2017 3:18 pm 
Offline

Joined: Tue Nov 07, 2017 8:23 am
Posts: 2
Hi there
When i type out the following code
String abc = " ";
abc.concat("abc");
abc.concat("def");
System.out.println(abc);
I get a blank reply

whereas the answer says i should get abcdef
I assumed that String was immutable .
If I type out System.out.println((abc.concat("abc"))+(abc.concat("def"))); i get a reply abcdef

I find the question a bit confusing... please advise


Top
 Profile Send private message  
 
 Post subject: Re: About Question enthuware.ocajp.i.v7.2.941 :
PostPosted: Fri Nov 10, 2017 9:43 pm 
Offline
Site Admin

Joined: Fri Sep 10, 2010 9:26 pm
Posts: 6799
No, the answer says, "It will print empty string (or in other words, nothing)."

Why do you think the question is confusing? It shows a piece of code and expects you to find out what will be printed.

You are right about String being immutable. That is the reason abc.concat("abc") doesn't modify the string referred to by the variable abc. The concat method returns a new String object that contains "abc".

That is why when you do (abc.concat("abc"))+(abc.concat("def")), you are basically using the two new String objects "abc" and "def" (created by the concat methods) and to create yet another new String containing "abcdef" using the + operator.


Top
 Profile Send private message  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Template made by DEVPPL/ThatBigForum