Follow Our FB Page << CircleMedia.in >> for Daily Laughter. We Post Funny, Viral, Comedy Videos, Memes, Vines...


1).what is the difference between below examples

String s="vijay";

String s=new String("vijay");

Answers were Sorted based on User's Feedback



1).what is the difference between below examples String s="vijay"; String s=new Stri..

Answer / ravi

Guys please don't miss guide.

Karun you r absolutely right, but there is one big
difference between them

String s="vijay"; --> This is string literal

String s=new String("vijay"); --> String Object

Definitely first one gives the better performance than
second one, why because..

JVM maintains an internal list of references for interned
Strings( POOL of unique strings) to avoid duplicate string
objects in heap memory. when ever the JVM loads string
literals from class file and executes, it checks whether
that string exists in the internal list or not. If it is
already exists in the list then it does not create new
string and it uses the references to the existing String
object. JVM does this type by checking tinternally for
string literal but not for string object which it creates
through 'new' keyword

You can explicitly force the JVM todo this type of checking
for string objects which are created through 'new' keyword
using "String.intern()" method. This forces the JVM to check
the internal list and use the existing String object if it
is already present.

Is This Answer Correct ?    26 Yes 0 No

1).what is the difference between below examples String s="vijay"; String s=new Stri..

Answer / 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

1).what is the difference between below examples String s="vijay"; String s=new Stri..

Answer / karun_cts

the two instances are created with the same name but one is
storing in constant pool and another one is saved in the
non constant pool.

Is This Answer Correct ?    4 Yes 0 No

1).what is the difference between below examples String s="vijay"; String s=new Stri..

Answer / info_vijaykumar

Thanks a lot Karun....

Is This Answer Correct ?    1 Yes 0 No

1).what is the difference between below examples String s="vijay"; String s=new Stri..

Answer / susrhee kanta

am unable to get constant pool and non constant pool?

can you please explain it and send it to my id.

sushreekp@gmail.com

Is This Answer Correct ?    1 Yes 0 No

1).what is the difference between below examples String s="vijay"; String s=new Stri..

Answer / uday

Hi All,
Here one object is stored in the heap and you said that
you can change that, but bydefault String objects are
immutable(ReadOnly), how can u change it?

Is This Answer Correct ?    1 Yes 0 No

1).what is the difference between below examples String s="vijay"; String s=new Stri..

Answer / info_vijaykumar

i got your answer,what are all the two instance in
that?.....

Is This Answer Correct ?    0 Yes 0 No

1).what is the difference between below examples String s="vijay"; String s=new Stri..

Answer / ramya

as kiran said in the first answer that the string created
in the heap memory can be changed,is it that the memory in
the heap can be changed.I read it as heap provides a stable
storage.Please clarify.Thanks in advance.

Is This Answer Correct ?    0 Yes 2 No

1).what is the difference between below examples String s="vijay"; String s=new Stri..

Answer / karun

In first statement String s = "vijay"

Means it will create a single instance and single reference
variable

but in second statement

String s = new String("vijay");

Means it will create a two instances and single reference
variable

Is This Answer Correct ?    4 Yes 7 No

1).what is the difference between below examples String s="vijay"; String s=new Stri..

Answer / navneet raushan

the first case does: if u write first case , one object and
one refernce will be created. object will be created in
string pool.
But when u write 2nd case , two object and one refernce
will be created , one in string pool and one in heap.

Is This Answer Correct ?    1 Yes 4 No

Post New Answer



More Core Java Interview Questions

Can we add default constructor to Servlet?

1 Answers   Fidelity,


how can we import the user defined package without classpath?

1 Answers   CSC, Infosys,


How to do a true java ping from windows?

0 Answers  


What is OOPs & Why?

3 Answers  


why is multiple inheritance not allowed in java?

7 Answers   Elementus Technologies, Huawei, Infosys,






What is complexity and its types?

0 Answers  


why java does not contain pointers?

13 Answers   Infosys, TCS,


Why is it important to initialize a variable?

0 Answers  


What are event-delegation model and event-inheritance model? Which is best?

1 Answers  


Is boolean a wrapper class in java?

0 Answers  


What is the transient keyword?

0 Answers  


What is the difference between reader/writer and inputstream/output stream?

0 Answers  






Categories