1).what is the difference between below examples

String s="vijay";

String s=new String("vijay");

Answer Posted / vijay

yes karun and ravi u both are right...!!
dear friends there is small difference b/w both string
declaration but both have create huge differences.

String s="vijay"; // string literal
that means it create one object and one reference and object
will be created in string pool.

String s= new String("vijay"); //string object
that means it create two object and one reference and one
object will be created in string pool and another on heap.

and difference what ravi want to say is ....if

String s="vijay";
String s1="vijay";
means here s and s1 both refer the same object whereas

String s=new String("vijay");
String s1=new String("vijay");
here these reference s and s1 refer refer 2-different
object.

that is why the first one give the better performance.

Is This Answer Correct ?    6 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What’s the difference between constructors and other methods?

536


What is the difference between compare and compareto in java?

492


Explain what access modifiers can be used for variables?

593


What are static blocks and static initalizers in java ?

587


What is a static class in java?

559






What do you understand by classes in java?

568


What is the relationship difference the canvas class and the graphics class?

567


What is object of class in java?

601


What is difference between char array and string?

529


What is the difference between static method and instance method in Java?

576


What are the drawbacks of reflection?

567


What is your platform?s default character encoding and how to know this?

1754


What are the features in java?

580


What is meant by method overriding?

549


What is java in detail?

562