• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interview String question

 
ket bhav
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

If I have code like below is it fisible?


String b = "abc";
String c = "def";
for (int i=0;i<100000000;i++){
String a = b + c;
}

How it affects the system? can we improve it and how?

My answer is using StringBuffer isn't it?
 
Mohana Rao Sv
Ranch Hand
Posts: 485
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, you are right
 
ket bhav
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes but any one have any other idea?

cause I was asked the same ;)

Other then StringBuffer???
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the different between

and


other than 1/100000000 ?
 
Badal Chowdhary
Ranch Hand
Posts: 34
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ket
StringBuilder will be faster than StringBuffer as it is not thread-safe.
Infact, Java compiler will automatically convert it to StringBuilder to by-pass so many String objects creation. Below is the code snippet that will run:


Thanks,
Badal
 
Tomasz Sochanski
Ranch Hand
Posts: 47
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ket bhav wrote:
String b = "abc";
String c = "def";
for (int i=0;i<100000000;i++){
String a = b + c;
}

How it affects the system? can we improve it and how?

My answer is using StringBuffer isn't it?


There is no sense in assigning 100000000 times the same value to string "a" - one could be enough. Additionally - this variable "a" is declared inside "for" body block, so it won't be available outside this block, which lets you delete completely "for" loop keeping same functionality.
 
Badal Chowdhary
Ranch Hand
Posts: 34
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Tomasz
I think it is not a real time scenario. It's more of an interview question to test String fundamentals.

Thanks,
Badal
 
ket bhav
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Badel

Yeah that is definatly to check fundamental but it may be real time scenario like this... not same one but might need loop depends on some variable and variables value goes too high and might some string combination needs to be assign like an example.
 
ket bhav
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And thanks for you answer Badal..

 
sakthi moorthy
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


How it affects the system?
The line1 will create new literals of "abcdef" every time it executes, in this case 10 Crores times. so this will consume unnecessary space.

can we improve it ?
Yes

How ?
using StringBuffer or StringBuilder or making b and c variable 'final' so that line1 wont new String Literals for each and every Execution.

I think String Interview Question also explains about literals.

Correct me if i am wrong somewhere.


Thanks & Regards
S.Sathiya
 
Tomasz Sochanski
Ranch Hand
Posts: 47
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sakthi moorthy wrote:

can we improve it ?
Yes

How ?
using StringBuffer or StringBuilder or making b and c variable 'final' so that line1 wont new String Literals for each and every Execution.


Using StringBuffer or StringBuilder is just little improvement - this redundant code inside "for" loop will run faster, but it is still unnecessary code - you can just remove "for" loop (variable declared inside it is unreachable outside the loop and creation of this String object doesn't affect any data outside the loop, so there is no sense to execute it even once!).
 
Arun Giridharan
Ranch Hand
Posts: 290
Debian Fedora Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ket bhav wrote:
If I have code like below is it fisible?




How it affects the system? can we improve it and how?

My answer is using StringBuffer isn't it?


Correct me if i am wrong, Isn't that String pool concept come into picture even if he says i to range 100 or 1000 or 10000 , String a points to same reference object right !
how can StringBuffer or StringBuilder could be a better option ??
 
Tomasz Sochanski
Ranch Hand
Posts: 47
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ket bhav wrote:

Correct me if i am wrong, Isn't that String pool concept come into picture even if he says i to range 100 or 1000 or 10000 , String a points to same reference object right !
how can StringBuffer or StringBuilder could be a better option ??


This is not true, check this code:

Output of last println() will be "false". Strings "c" and "d" are not final, so compiler can't be sure if (c + d) have fixed value (and use it as String literal from pool), so new String will be created every time when this code is called.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sakthi moorthy wrote:can we improve it ?
Yes

How ?
using StringBuffer or StringBuilder

This won't improve it and may in future versions of java make it perform worse. This is because the current compiler will optimise this code to use StringBuilder. Therefore changing the code to use StringBuilder will have no effect on performance. However, later versions of java may find a better way to optimise the performance of String concatenation, but if you've written your code to use StringBuilder then the compiler won't be able to make use of that optimisation.
Always use the simplest code that does the job and only worry about trying to optimise it when you can show it is causing a problem.
 
Arun Giridharan
Ranch Hand
Posts: 290
Debian Fedora Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tomasz Sochanski wrote:
so new String will be created every time when this code is called.

No it won't. try to check the hashcode and equals method of the String.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arun Giridharan wrote:
Tomasz Sochanski wrote:
so new String will be created every time when this code is called.

No it won't. try to check the hashcode and equals method of the String.

The hashcode and equals method can be used to check if two Strings have the same content. They cannot show if two String references refer to the same object or not.
 
Tomasz Sochanski
Ranch Hand
Posts: 47
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arun Giridharan wrote:
Tomasz Sochanski wrote:
so new String will be created every time when this code is called.

No it won't. try to check the hashcode and equals method of the String.


As Joanne noticed you can't use hashCode() or equals() to check if 2 references points the same object or not. There is "==" operator to check this.
However it this example (c+d).equals(c+d) returns true, and also (c+d).hashCode() == (c+d).hashCode() returns true. This is because String.hashCode() and String.equals(obj) return values depends only on char array they hold.
 
Akhilesh Trivedi
Ranch Hand
Posts: 1599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One small thing, we can have the declaration of variable outside the for loop. Let alone initialization be inside.
 
Arun Giridharan
Ranch Hand
Posts: 290
Debian Fedora Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Joanne Neal,Tomasz Sochanski
Tomasz Sochanski wrote:

This is not true, check this code:

Output of last println() will be "false". Strings "c" and "d" are not final, so compiler can't be sure if (c + d) have fixed value (and use it as String literal from pool), so new String will be created every time when this code is called.

few things still didn't hit my brain
1)how does compiler differenciate one with final String<true> and other with String <false> but both follow String pool concept right ?
 
Arun Giridharan
Ranch Hand
Posts: 290
Debian Fedora Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get it now,before it even start looking at String Pool(on the second time of loop) it would have created an Object , thus leads to 100000000 ,so preffered is StringBuilder , moreover String Pool refers to compiler constant (i guess) .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic