How many objects are created when we create String class
object using new operator?

Answer Posted / rana

Answer is 2

String s = new String ("ABC"); // creates new object in heap & also adds entry in string literal pool.

String s1 = s.intern(); // will copy the string from pool
System.out.println(s1);



@Ravi -
System.out.println(s1.hashCode()==s2.hashCode() );

will return same because of String generates its hashCode based on the characters it has. there is a formula for hashCode generation on string


s[0]*31^n-1 + s[1]*31^n-2 ..... + s[n-1]

s[0] - 1st characters ascii value
n - length of the string

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is a map? What are the implementations of map?

576


What is a Transient Object?

643


Are arrays classes in java?

537


What is google full form?

540


Difference between character constant and string constant in java ?

563






What is factor r?

532


What is java and their uses?

552


Explain the private field modifier?

617


What is method and methodology?

612


Is zero a natural number?

576


What is the difference between public, private, protected, and friend access?

580


What is "this" keyword in java? Explain

666


What is the driver class?

571


What is a java applet? What is an interface?

635


What is the difference between static class and normal class?

496