Learn Selenium and Java for Selenium

Sunday, 3 June 2018

Strings in Java Part 2

String vs String Buffer

String is a immutable and String Buffer is mutable. Confused?, no need to worry just understand the below definition and program , Its easy concept.

String - Let us discuss about sting , String class in Java is immutable because we want to increase the performance so once object is created with some content , if we try to add new content/change the existing object it would not change

Let us see the below program and its output to understand the nature of immutable class

Class String Test
  {
      public static void main(String[] args)
              {
                   String s = new String("Hello");
                         s.concat("World");
                     System.out.println(s);
               }
}

Guessing the output?

Here the output is Hello 

String Buffer - String buffer is mutable class in java , which means if we try modify exiting object value it would get changed.

Class String Test
  {
      public static void main(String[] args)
              {
                   String s = new String("Hello");
                         s.append("World");
                     System.out.println(s);
               }
}

Here the Output would be HelloWorld 




0 comments:

Post a Comment

Popular Posts

Recent Posts



Newsletter

Join our newsletter to get secret tips directly in your inbox!


*We Hate Spam!

Powered by Blogger.