1).what is the difference between below examples
String s="vijay";
String s=new String("vijay");
Answer Posted / 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 |
Post New Answer View All Answers
What is a consumer in java?
What are the restriction imposed on a static method or a static block of code?
What is meant by string is immutable?
State the significance of public, private, protected class?
What exactly is java?
Break statement can be used as labels in java?
What is the difference between overriding and overloading in OOPS.
What is the base class of all exception classes in java?
Explain the features of java?
Can we have return statement in finally clause? What will happen?
Is there a case when finally will not execute?
What is jpa specification?
What is sorting in java?
What is meant by binding in rmi?
Which collections are thread safe in java?