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
Tell me are there implementations for sorting and searching in the java libarary?
how many types of Inheritance?
What is defined as false sharing in the context of multithreading?
What is the relationship between clipping and repainting under awt?
How do you create a method in java?
What are three ways in which a thread can enter the waiting state in java programming?
Write a program to print fibonacci series up to count 10.
Is java a super set of javascript?
What are the four integer types supported by java?
What is skeleton and stub?
What is a pointer and does java support pointers?
Explain which of the following methods releases the lock when yield(), join(),sleep(),wait(),notify(), notifyall() methods are executed?
What is the difference between keyword and identifier?
How does list work in java?
What is the purpose of format function?