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 




Related Posts:

  • WebServices Test Automation-Introduction What is WebServices? WebServices are set of rules and protocols which helps in communication between two software on same or different platforms. … Read More
  • Strings in Java : Part 7 String Methods Remember our last post focused on usage of split() method in string in case you have missed seeing that you can see our Strings … Read More
  • What are collections in JAVA? What are Collections in JAVA? Today we will be learning of Collections , Collections in java are used to store different object in a single unit.… Read More
  • What is Gradle? Have you been hearing the term “Gradle” but not sure what it is? Let me give you a short and crisp overview.       Gradle … Read More
  • Strings in Java : Part 6 String Methods Exited about the next method that we are going to explain is Split() method in string. It is commonly asked method in the interviews … Read More

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.