1).what is the difference between below examples
String s="vijay";
String s=new String("vijay");
Answers were Sorted based on User's Feedback
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 |
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 |
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 |
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 |
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 |
Answer / info_vijaykumar
i got your answer,what are all the two instance in
that?.....
Is This Answer Correct ? | 0 Yes | 0 No |
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 |
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 |
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 |
Define array. Tell me about 2-D array.
How do you reverse a list?
Why do we declare a class static?
What is JDBC Driver interface?How can you retrieve data from the ResultSet
What is 32 bit float?
What is the major difference between linkedlist and arraylist?
Hi can u pls tell me what is the use of marker interface. Iknow what is marker interface but what ability will the object get by implementing this.
if we give input as " hi how are you" then the output should be "uoy woh"...it should skip odd words in the input and should reverse even words from the end of string...can anyone help me to write this program in java
Why synchronization is important?
What are the 8 primitive data types in java?
What does it mean that a method or field is “static”?
What is the difference between member variables initialization and assignment in a constructor?