Monday 23 March 2015

String in Java

  • Sequence of character is Called a String
    • Example:  "abc","xyz","pqr$","4lkm"
  • Strings are immutable (non mutable) meaning once the string object is created can't change the value of string object.
  • They are two ways to create string object
    • by declaring a variable (using assignment operator)
      • Example: String s="abc";
    • by creating a instance of a class
      • Example: String s=new String("pqr");
  • The Difference between the creating of string object using assignment and creating instance is that in First case"Memory" will be allotted in Free pool of Memory (Method Area) and in the second case"Memory" will be alloted in Heap Memory.
  • If string object is created using assignment operator which contain the same value as other like


String s1="abc";
String s2="abc";












See in the above example,String s1 is created which contains the value "abc" again String s2 which contain the same value "abc" will point to the same memory address.In these case the memory addresses of this content will be equal (s1==s2) true.
  • If the String object is created using new instance then what ever it may be case it will create a new object like 
String s3=new String("abc");
  • then it will create new instance of  object in heap memory.
  • Finally if lot of manipulation in string is required then its not recommended to use string, In this case need to use StringBuffer.

No comments:

CSS Selectors

  CSS Selectors In CSS, selectors are patterns used to select the element(s) you want to style There are 3 different types of CSS selectors ...