Tuesday 3 July 2018

Conversion from String to StringBuffer and StringBuilder

    Here in this context We can directly pass String class object to StringBuffer and StringBuilder class constructors. As String class is immutable in java, so for editing a string, we can perform same by converting it to StringBuffer or StringBuilder class objects. 

Example.java
package java8s;

public class Example {

          public static void main(String[] args) {
                  
                   String str="SILAN";
                  
                   //Conversion from String object to StringBuffer
                   StringBuffer sb1=new StringBuffer(str);
                  
                   System.out.println(sb1);
                  
                   sb1.reverse();
                   System.out.println(sb1);
                  
                   //Conversion from String object to StringBuilder
                    StringBuilder sb2=new StringBuilder(str);
                   sb2.append("Technology");
                  
                   System.out.println(sb2);
                                     

          }

}

Output:
SILAN 
NALIS
SILANTechnology

1 comment:

  1. PokerStars Casino Login - JtmHub
    For your convenience, you'll also find 영천 출장안마 the PokerStars Casino Login page 김천 출장마사지 in the upper right corner of the 익산 출장샵 PokerStars Login page. If you 원주 출장안마 enjoy playing 목포 출장마사지 poker at home, you

    ReplyDelete