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 |
Difference between Interface & Abstract class?
Difference between ?System.out.println? and ?System.error.println??
Discuss different types of errors that generally occur while programming.
What is the differences between heap and stack memory in java? Explain
Given: 10. interface A { void x(); } 11. class B implements A { public void x() { } public voidy() { } } 12. class C extends B { public void x() {} } And: 20. java.util.List list = new java.util.ArrayList(); 21. list.add(new B()); 22. list.add(new C()); 23. for (A a:list) { 24. a.x(); 25. a.y();; 26. } What is the result? 1 Compilation fails because of an error in line 25. 2 The code runs with no output. 3 An exception is thrown at runtime. 4 Compilation fails because of an error in line 20.
What is the significance of continue jump statement? Explain with an example.
What is a module function?
How to handle a web browser resize operation?
give me the answer of this code class A extnds String This code we can write r not in Java? Explain?
Does google use java?
What is the difference between add() and addElement() method in Vector Class ?
What is the need of "creating and throwing an UserdefinedException" when the "Exception" class is already available?