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
Explain the scope or life time of class variables or static variables?
Which number is denoted by leading zero in java?
What are static initalizers in java ?
Can we override static methods in java?
how to write a server program and sending the mails to the server using smtp protocol please help me
What is the difference between array list and vector in java?
How many ways can we create singleton class?
What is logical variable?
When do you call copy constructor?
Explain a few methods of overloading best practices in java?
What is string :: npos?
What is Garbage Collection in Java
why an outer class cannot be declared as private?
I don’t want my class to be inherited by any other class. What should I do?
How is string immutable in java?