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

What is polymorphism in java? What are the kinds of polymorphism?

0 Answers  


What is the function of compareto in java?

0 Answers  


What is string in java?

0 Answers  


What is the output of the below java program?

0 Answers  


How to decrease number of hashings in has

1 Answers  






What is a qms manual?

0 Answers  


please send me hr interview questions in it industry

0 Answers  


What are different access specifiers in java? Explain

0 Answers  


What is OOP Language?

0 Answers   Atos Origin,


Suppose there is a System A which gives some output. This output is used as input by system B. The rate at which System A produces is faster than the rate at which system B consumes it. How can you improve this?

1 Answers   RBS,


What language is an assembler written in?

0 Answers  


What is jit and its use?

0 Answers  


Categories