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
Why Do I Get A "permission Denied" Error After Downloading The .jnlp Java Launcher For The Vkvm?
Is 9 a prime number?
What is main method?
Is 0 an even number?
What does || mean in vectors?
What function extracts specified characters from a string?
What are recursive functions? Give some examples?
What is string in java with example?
What is structure of java heap? What is perm gen space in heap?
Can one thread block the other thread?
What is string english?
What is the numeric promotion?
What are pass by reference and pass by value?
How do you call a reference in java?
Can java object be locked down for exclusive use by a given thread?