Friday, March 13, 2009

Reversing a String without using any String Functions

This was one question i got on a written round in an interview. I couldn't solve it becos all my solutions had atleast one string function involved.I got a solution from Java Cookbook by Ian.F.Darwin. But still my doubt remains if Stringbuffer and stringtokenizer areconsidered as other variants of string itself. Will this be a sensible answer for the question?
If the purpose is to reduce the duplicate String objects created during the reversal process, these solutions (using StringBuffer and StringTokenizer) should do good.

The sol goes like this:

To reverse a string by character, use the
new StringBuffer(string).reverse();

To reverse the order of words , say in a sentence string
Stack staRev= new Stack();
//Tokenize words of the long sentence string in longSt using space as default delim
StringTokenizer st = new StringTokenizer(longSt);
//Push the tokens into the stack
while(st.hasMoreTokens)
staRev.push(st.nextElement());
//Pop stack , is always in Last in First Out order. Resulting in reversing the string
while(!staRev.empty())
{
S.o.p(starRev.pop());
}

Sunday, March 8, 2009

String Literals

I remember my struggle to understand the concept of String Literals and the difference between "==" and .equals() difference during my SCJP exam. There were many questions related to this in almost all the sample questions i went through.
Say there is a string literal, String sL= "Hello";
Another String object created using "new" keyword, String sO=new String("World");
There is a difference in the way a string literal and a string object is stored and processed by JVM.
JVM keeps a list of unique string literals in heap memory. When a string literal is assigned to a variable, JVM checks whether this literal is already there in its list of interned strings and adds it only if it does not exist in this list in heap. If it does exist, the reference variable sL would be assigned the location of this already existing literal value, thereby reducing duplication of literal values.
If we execute say a 100 times String sL1 ="hello";...............String sL100="hello";
There would be only one hello literal stored in heap, and all the 100 reference variables from sL1 to sL100 would be referring to this single literal.

In case of a string object created using a "new" keyword, there is no checking or list involved. It simply creates a string object with the value given with new keyword. It does not bother if there are identical values.
if we execute String sO1=new String(World)...........String sO100=new String("world");
There will be 100 string objs with content value "World"; assigned to each of the 100 references.

Here comes the use of "==" and .equals()
"==" checks if the references point to the same memory location (as in the case of literal references) and .equals() ckecks if the content values in the comparing objects are identical.

But all said we can still make the String objects avoid creating duplicate String objects using the method String.intern(). This would make the string objects to behave like literals by forcing a check for duplicate String objects having same value.

Friday, March 6, 2009

Strings&StringBuffer : mutable or immutable

Going by Java interview questions , very often the first question would be this. I remember during my campus interviews , this was one of the favourite and the opening questions for interviewers.
StringBuffer is mutable , String is immutable.
String s="Hello"; s =s+"World" . Then what does this mean?
A string object remains static , ie its contents remains the same. But then why on earth is variable s assigned a concatenated value ?
Well, s is a reference variable which refers to objects assigned to it on the other side of "=". The statement s+"World" creates a different memory location than where the s was pointing on to when it was holding "Hello". Why? because a string is static and immutable , once created it remains the same and can never be altered.
Hence when the statement s= s+"World" is executed, our reference variable s is assigned a different memory location which hold the value "HelloWorld"
Whereas , StringBuffer Object is mutable, that is those objects can be changed . Hence for the programs requiring a String concatenation dynamically, using StringBuffer would be more beneficial.

Meditate Java?

Well , i dn't know if i'm going to literally sit and meditate. But this blog will be based on Java , the technology i was working say 2 years back. I felt that i'm slowly going away from my Java memories , even the basics. So it was a wake up call for me to read again and refresh memory. When it comes to retaining what i read, i could'nt think of anything else than a "new Blog" .