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
Which is the best sorting technique in java?
Can we create our own wrapper class in java?
Is java 1.7 the same as java 7?
What is meant by string is immutable?
Is constructor inherited?
why an outer class cannot be declared as private?
Where is jre installed?
Explain about the main() method in java?
How many types of keywords are there?
how to open and edit XML file in Weblogic???
What is static method with example?
What happens if a try-catch-finally statement does not have a catch clause to handle an exception that is thrown within the body of the try statement?
What is ascii format?
How do you sort arrays in java?
Hi i am creating desktop application in that i want calling to mobile number. i have java telephone api (JTAPI) but i dont understand how it configure & use plese help me