JAVA STRING

 

 

In   Java , String  is  an  object  that  represents   a  sequence  of  characters   enclosed  by  double  quotes  and  every  character  is  stored  in  16   bits.  String  class  is  present in  java.lang  package.  String   class  is  bydefault  declared  as   final  class  which  can’t  be  subclassed. String  class  is  considered  as  immutable, so  it  can’t  be  modified  once a  string  object  is  specified  with  specific  content . 

 

 

 

Key  Characteristics  of  Java  String  :

 

  • Object  Type :  Unlike  Java  primitive  data  types (int,char,boolean etc) , String  is  a  class   and  strings  are  objects .
  • String  Pool:  To  save  String  memory , Java  maintains  a  special  area  in  the  heap  memory  called  the  String  Constant  Pool . When a  String literal  is  created  in  Java, JVM (Java Virtual Machine )  checks  the  String constant pool ; if any  identical  string  exist , then it  reuses  the  reference  to  the  existing  object  instead  of  creating  new  one.
  • String Immutability:  String class  is  immutable , so  it can’t  be  modified  once   a  string  object  is  specified   with  specific  content .  Any  operation  which  is  done  to  modify   the  string (like  replacing  characters  or  concatenation )  actually  creates  a  new  String  object , without changing  the  original  one .

 

 

 

 

 

Creating  Strings :

 

Strings  can  be  created   in  two  ways:

  • Without   new  Operator (e.g. String  str=”Java”; )
  • With  new   Operator (e.g. String  str=new  String(“Java”);

 

While  creating  a String  without  new  operator ,  it  utilize  the  String constant pool  and  when  creating String  with  new  operator , it always  creates  a  new  object  in  the  heap .

 

 

 

 

 

Java Interfaces  and    Classes  in   Strings

 

 

 

 

CharSequence  Interface:

 

In  Java  CharSequence  Interface  is  used   for  representing  the  sequence  of  Characters   .  It  provides  basic  methods  such  as  length() , charAt() , subSequence() , toString() etc.  Classes  which  are  implementing  CharSequence  interface  are  mentioned  below.

 

 

 

 

  1. String 

           In  Java,  String  is  an  immutabe  class  , i.e. String  object  value  can’t  be  changed  once a  String object  is  created .  If  you  want  to  modify  any  string , a  new   String  object  is   created  and  the  original  string  remains  unchanged.

 

Syntax:

 

//Method1

String  str=” Java” ;

 

//Method2

 

String  str=new  String(” Java” );

 

 

 

 

 

 

2.  StringBuffer

 

String  Buffer  class  is  same  as  String  class  which   stores  collection  of  characters , but  it  is  a  Mutable  Class.

String Buffer  class  is  Mutable  because  of  capacity  concept .

 

By  using  capacity  concept  one  can  append  data  to  the String Buffer  object , but  modification  happens only within  the  allocated  memory.

 

Here, capacity means  count of  how  many  characters String Buffer  Object  can  hold .

 

Default  capacity  value of   each  and  every  String Buffer  object  is 16.

Incase , if  anyone appending  data  more  than  16  characters ,  then  capacity  will  be  increased   automatically  by  JVM  using  one  formulla given below:

 

New  Capacity = (Old  Capacity*2)+2;

e.g.  New  Capacity=(16*2)+2=34

After it, New  Capacity=(34*2)+2=70

 

String  Buffer  class  and  its  object are  synchronized i.e. Thread Safe .

 

Syntax:

StringBuffer   str = new  StringBuffer(“Javacoding”);

 

 

 

 

 

StringBuilder 

 

 

StringBuilder  class  implementation  is  same  as  StringBuffer, except  StringBuilder class  and  its  objects  are  non synchronized  i.e. StringBuilder  class  is  eligible  for  multitasking.

StringBuilder  class  objects  are  created  using  new  operator  only.

 

StringBuilder  class  has  3  constructors :

 

StringBuilder( );

 

StringBuilder( String str );

 

StringBuilder (int  capacity );

 

 

Syntax:

The  general  syntax  involves  creating  an  instance  of  the  StringBuilder  class  using  the  new  keyword  and  one  of  its  constructors .

 

e.g.  

 

//Creating  an  empty  StringBuilder  with  an initial  capacity  of  16 characters

 

StringBuilder  sb1 =  new  StringBuilder ();

 

//create  a  StringBuilder  initialized  with  the  content of an  existing String

 

StringBuilder  sb2 = new  StringBuilder (” Java  coding “);

 

//create  a  StringBuilder  with  the  specified  initial  capacity.

 

StringBuilder  sb3 =  new  StringBuilder(70);

 

 

 

 

 

String  Tokenizer

 

 

In  Java, String  Tokenizer  class  is  used  to  break  a  string  into  tokens .

A  String  Tokenizer  object  internally  maintains  a  current  position  within  the  string  to  be  tokenized . 

 

Syntax:

 

StringTokenizer  str= new  StringTokenizer (” Java  Coding  ” );

 

 

 

 

Immutable   String  In  Java

 

 

In  Java ,  string  objects  are  immutable  i.e. string object  is  unmodified  once  created  with  specified  content . 

 

 

 

Example:

 

 

import  java.io.*;

 

class   Flower 

{

           public  static  void  main(String  args [ ] ) {

           String    s = ” Sunflower”;

 

           // concat() method  appends  the  string  at  the  end

 

              s.concat(” Garden “ );

 

          //  This  will  print  Sunflower   only  because   strings  are  immutable  objects.

             System.out.println(s);

 

}

 

}

 

  

 

 

 

Output

 

Sunflower 

 

In  the  above  example ,  String  content  haven’t  changed  but   a   new  object  is  created  with  “Sunflower  Garden ” .  Thats  why  string  is  known  as  immutable.

 

 

In  the   example  below  we  have  discussed  how   to  assign  the  reference  explicitly  in  String  using  String.concat()  method.

 

 

 

import   java.io.*;

class   Flowers 

{

public  static  void  main( String  args [] ) {

String    flower = “Sunflower”;

 

flower=flower.concat(“Garden”);

System.out.println(flower);

 

}

 

}

 

 

Output:

 

Sunflower  Garden

 

 

How  Strings  are  Stored  in  Java  Memory?

 

 

 

String  literal:

 

Whenever  a  String  Object  is  created  as  a  literal ,   the  object  will  be  created  in  the  String  constant  pool .  This  String  constant  pool  allows  JVM  to  optimize  the  initialization  of  String  literal .  The  String  constant  pool  is  present  in  the  heap  memory .

 

 

Using  new  Keyword:

 

The  String  can  also  be  declared  using  a  new  operator  i.e.  dynamically  allocated .  When  String  is  dynamically  allocated ,  they  assigned  a  new  memory  location  in  the  heap memory .  The string  which  are  created  using  new  keyword  are  not  added  to  String  constant  pool .

 

 

If you  want   to store   this  string  which  are  created  using  new  keyword    in   string  constant  pool , then  use  intern()  method.

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *