aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Interview String question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Interview String question" Watch "Interview String question" New topic
Author

Interview String question

ket bhav
Greenhorn

Joined: Aug 28, 2011
Posts: 22

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

Joined: Aug 01, 2007
Posts: 485

yes, you are right


ocjp 6 — Feeding a person with food is a great thing in this world. Feeding the same person by transferring the knowledge is far more better thing. The reason is the amount of satisfaction which we get through food is of only one minute or two. But the satisfaction which we can get through the knowledge is of life long.
ket bhav
Greenhorn

Joined: Aug 28, 2011
Posts: 22
yes but any one have any other idea?

cause I was asked the same ;)

Other then StringBuffer???
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

what is the different between

and


other than 1/100000000 ?
Badal Chowdhary
Ranch Hand

Joined: Apr 26, 2011
Posts: 34

@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


Blog: http://badalchowdhary.wordpress.com/
Twitter: http://twitter.com/badalrocks
Tomasz Sochanski
Ranch Hand

Joined: Jan 13, 2009
Posts: 47

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

Joined: Apr 26, 2011
Posts: 34

@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

Joined: Aug 28, 2011
Posts: 22
@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

Joined: Aug 28, 2011
Posts: 22
And thanks for you answer Badal..

sakthi moorthy
Ranch Hand

Joined: Oct 17, 2007
Posts: 54


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

Joined: Jan 13, 2009
Posts: 47

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

Joined: Sep 30, 2010
Posts: 290

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

Joined: Jan 13, 2009
Posts: 47

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

Joined: Aug 05, 2005
Posts: 3689
    
  16
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.


Joanne
Arun Giridharan
Ranch Hand

Joined: Sep 30, 2010
Posts: 290

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

Joined: Aug 05, 2005
Posts: 3689
    
  16
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

Joined: Jan 13, 2009
Posts: 47

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

Joined: Jun 22, 2005
Posts: 1527
One small thing, we can have the declaration of variable outside the for loop. Let alone initialization be inside.


Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
Arun Giridharan
Ranch Hand

Joined: Sep 30, 2010
Posts: 290

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

Joined: Sep 30, 2010
Posts: 290

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) .
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Interview String question