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


Please Help Members By Posting Answers For Below Questions

Tell me are there implementations for sorting and searching in the java libarary?

603


how many types of Inheritance?

657


What is defined as false sharing in the context of multithreading?

512


What is the relationship between clipping and repainting under awt?

619


How do you create a method in java?

548






What are three ways in which a thread can enter the waiting state in java programming?

686


Write a program to print fibonacci series up to count 10.

522


Is java a super set of javascript?

589


What are the four integer types supported by java?

566


What is skeleton and stub?

594


What is a pointer and does java support pointers?

562


Explain which of the following methods releases the lock when yield(), join(),sleep(),wait(),notify(), notifyall() methods are executed?

617


What is the difference between keyword and identifier?

539


How does list work in java?

512


What is the purpose of format function?

630