Tuesday, December 13, 2011

String

  • String is immutable.(once a value is assigned to a string object, it cannot be changed).
  • String can be instantiated through Constructor or explicitly.
  • If the String is instantiated explicitly, it is loaded in Intern pool (Special memory in heap).
    • Eg:  String str1="Hello";   String str2="Hello"; //No duplications (Loads in InternPool)
    • When multiple String variables are assigned a same string value explicitly, all variables will share one string object loaded in the Intern Pool.
  • If the String is instantiated through Constructor or any method of string will be loaded in the Heap (not in Intern Pool) 
    • Eg:  String str1=new String("Hello"); //Have duplications (Loads in Heap)
Note: To see all the contents of String class, type "javap java.lang.String" in cmd.

No comments:

Post a Comment