Sunday, February 10, 2013
Saturday, February 9, 2013
why String is immutable
Immutable means that the current object is unchangeable & operations on it will return another new object which you have to catch to see the changes. String class is such class where methods like toUpperCase() etc will return String object which you have to save in a new variable....
however if their is a long string of operations & you dont want to make many variables
you can always use StringBuffer or StringBuilder class where all operations are performed on original object without the need to create multiple new objects.......
String s1= "samba";
s1.toUpperCase();
then s1 will not automatically point to a new String variable at another memory location.
s1 would still be pointing to the original String object, and the new String object that comes out of toUpperCase() will be ignored and thrown away. If you wanted s1 to point to the new String, you must assign it explicitly:
Assign the result of the method to name
s1= s1.toUpperCase();
example:
package com.samba.org.sample;
public class StringImmutableTest {
public StringImmutableTest()
{
System.out.println("StringImmutableTest.StringImmutableTest()");
}
public static void main(String[] args)
{
String s1="samba";
System.out.println("Before String Operation hash code:"+s1.hashCode());
s1.toUpperCase();
System.out.println("After String Operation but nt assigned to any variable hashcode:"+s1.hashCode());
s1=s1.toUpperCase();
System.out.println("After String Operation and assigned to variable hashcode:"+s1.hashCode());
/*OUTPUT
*
* Before String Operation hash code:79649854
* After String Operation but nt assigned to any variable hashcode:79649854
* After String Operation and assigned to variable hashcode:78664766
*
*/
}
}
however if their is a long string of operations & you dont want to make many variables
you can always use StringBuffer or StringBuilder class where all operations are performed on original object without the need to create multiple new objects.......
String s1= "samba";
s1.toUpperCase();
then s1 will not automatically point to a new String variable at another memory location.
s1 would still be pointing to the original String object, and the new String object that comes out of toUpperCase() will be ignored and thrown away. If you wanted s1 to point to the new String, you must assign it explicitly:
Assign the result of the method to name
s1= s1.toUpperCase();
example:
package com.samba.org.sample;
public class StringImmutableTest {
public StringImmutableTest()
{
System.out.println("StringImmutableTest.StringImmutableTest()");
}
public static void main(String[] args)
{
String s1="samba";
System.out.println("Before String Operation hash code:"+s1.hashCode());
s1.toUpperCase();
System.out.println("After String Operation but nt assigned to any variable hashcode:"+s1.hashCode());
s1=s1.toUpperCase();
System.out.println("After String Operation and assigned to variable hashcode:"+s1.hashCode());
/*OUTPUT
*
* Before String Operation hash code:79649854
* After String Operation but nt assigned to any variable hashcode:79649854
* After String Operation and assigned to variable hashcode:78664766
*
*/
}
}
Friday, February 8, 2013
Mirchi Movie Review
Mirchi is an upcoming Telugu Movie. Directed by Koratala Siva and Produced by V Samsi Krishna Reddy and Prmod. Music Composed by Devi Sri Prasad. Prabhas, Anushka and Richa Gangopadhya are in the lead roles.
After his last movie didn’t go as great as expected , there was a lot of hype that Prabhas will be back with ’Mirchi’ even though the young rebel did his best but the all-round performance of the entire cast was below par and leaves a lot to be desired. The sript writer’s transition into a director has not been good. Let me get on with the full review.
Story
Jai is an architect based in Italy, an optimistic who believes love changes everything.He becomes friends with a girl and then falls for her.He then follows her back to Mumbai where her family accepts him as their son -in-law. But then he has a background with a girl at a rival camp. Will that love blossom? How will the love triangle turn out to be, forms the crux of the story.
Performance
As I have stated earlier Prabhas has lined up to the hype his name has created for the movie.He looks handsome as always, acts brilliant with top notch dialogue delivery ans with wonderful screen presesnce and dance moves.Full marks to his performance.
Anushka’s girl-next-door act deserves appreciation as well. A girl hopelessly in love but torn between the rivalry of two families she wonderfully enters into the character. The overall screen presence and chemistry with Prabhas in the movie.
However, Richa’s performance has a dampening effect on the entire movie. Her acting skills and chemistry with Prabhas is below par. She could not match Anushka in any way on any level.
Another graceful performance was given by Sampath Raj. There are some comical moments that Brahmi gives you throughout the movie but it leaves the plot half-baked. Others have substantially less screen time and so I cannot comment on their performances.
Overall performance of the cast is still a dud.
Direction, Script and Cinematography
Koratala Siva is a good writer for sure. But all good writers cannot be good directors. The script offers nothing new. It is a routine and a vary predictable story which is not apt for a star like Prabhas.
The Screenplay and dialogues are also below par and the story does not connecct well and leaves the entire audience confused.
The cinematography is disappointing and the scene backdorps, fights does not look good and even the editin is too bad for a movie that is hyped this much.
Music
This is the only part of the movie that is great overall.The background score and songs give an immersive experience for the user.Surprisingly, the picturization of the songs are better than the rest of the movie.Full marks to the music.
Verdict
If you are a Prabhas fan, you are most likely to see it any way but if you are not, you can chuck it because its not worth it. Koratala Siva should do a better job next time.
Story
Jai is an architect based in Italy, an optimistic who believes love changes everything.He becomes friends with a girl and then falls for her.He then follows her back to Mumbai where her family accepts him as their son -in-law. But then he has a background with a girl at a rival camp. Will that love blossom? How will the love triangle turn out to be, forms the crux of the story.
Performance
As I have stated earlier Prabhas has lined up to the hype his name has created for the movie.He looks handsome as always, acts brilliant with top notch dialogue delivery ans with wonderful screen presesnce and dance moves.Full marks to his performance.
Anushka’s girl-next-door act deserves appreciation as well. A girl hopelessly in love but torn between the rivalry of two families she wonderfully enters into the character. The overall screen presence and chemistry with Prabhas in the movie.
However, Richa’s performance has a dampening effect on the entire movie. Her acting skills and chemistry with Prabhas is below par. She could not match Anushka in any way on any level.
Another graceful performance was given by Sampath Raj. There are some comical moments that Brahmi gives you throughout the movie but it leaves the plot half-baked. Others have substantially less screen time and so I cannot comment on their performances.
Overall performance of the cast is still a dud.
Direction, Script and Cinematography
Koratala Siva is a good writer for sure. But all good writers cannot be good directors. The script offers nothing new. It is a routine and a vary predictable story which is not apt for a star like Prabhas.
The Screenplay and dialogues are also below par and the story does not connecct well and leaves the entire audience confused.
The cinematography is disappointing and the scene backdorps, fights does not look good and even the editin is too bad for a movie that is hyped this much.
Music
This is the only part of the movie that is great overall.The background score and songs give an immersive experience for the user.Surprisingly, the picturization of the songs are better than the rest of the movie.Full marks to the music.
Verdict
If you are a Prabhas fan, you are most likely to see it any way but if you are not, you can chuck it because its not worth it. Koratala Siva should do a better job next time.
Thursday, February 7, 2013
stringbuffer vs stringbuilder
StringBuilder was introduced in JDK 1.5. What's the difference between StringBuilder and StringBuffer? According to javadoc, StringBuilder is designed as a replacement for StringBuffer in single-threaded usage. Their key differences in simple term:
- StringBuffer is designed to be thread-safe and all public methods in StringBuffer are synchronized. StringBuilder does not handle thread-safety issue and none of its methods is synchronized.
- StringBuilder has better performance than StringBuffer under most circumstances.
- Use the new StringBuilder wherever possible.
What happens when a Static Variable has the same name as a Static Class in Java?
public
System.out.println(X.Y.Z);
}
}
class
X {
}
}
class
C {
String
Z = "White";
}
What Does It Print? Do not run it in eclipse and tell me the answer. I need to know why as well?
Ans: White
Reason :Outer static variable priority is higher than inner static variable..
You're hiding the class Y with a static instance of C named Y. The class Y is still there and can be used.
Try:
System.out.println(X.Y.Z);
System.out.println((new X.Y()).Z);
The output should be:
White
Black
Wednesday, February 6, 2013
== vs equals in Strings in Java
The equals( ) method compares the characters inside a String object. The == operator compares two object references to see whether they refer to the same instance. The following program shows how two different String objects can contain the same characters, but references to these objects will not compare as equal:
package
blogspot.sbollam.com
public class Sample {
public Sample()
{
System.out.println("Sample.Sample()");
}
/**
* @param args
*/
public static void main(String[] args)
{
String s1= new String("abc");
String s2= new String("abc");
String s3 ="abc";
String s4 ="abc";
System.out.println(s1==s3);
System.out.println(s1==s2);
System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
System.out.println(s3.equals(s4));
System.out.println(s3==s4);
/*OUTPPUT:
*
* false false true true true true
* */
}
}
public Sample()
{
System.out.println("Sample.Sample()");
}
/**
* @param args
*/
public static void main(String[] args)
{
String s1= new String("abc");
String s2= new String("abc");
String s3 ="abc";
String s4 ="abc";
System.out.println(s1==s3);
System.out.println(s1==s2);
System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
System.out.println(s3.equals(s4));
System.out.println(s3==s4);
/*OUTPPUT:
*
* false false true true true true
* */
}
}
Subscribe to:
Posts (Atom)