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

Can private members of a base class are inheritable justify?

460


What does null mean in java?

607


Are true and false keywords?

537


What is abstraction in java?

594


What is consumer interface?

541






What is the public field modifier?

520


What access modifiers can be used for class ?

518


Explain about static nested classes in java?

580


How do you differentiate abstract class from interface?

508


What is the use of parse function in java?

492


What is the synonym of framework?

543


Why multiple inheritance is not supported by java?

490


Can we define package statement after import statement in java?

531


What is java in layman terms?

505


Define how objects are stored in java?

545