Create StringBuffer Object Using a Reference Stored in a Variable of Type String in Java



To create a StringBuffer Object, we use the following syntax −

StringBuffer s=new StringBuffer();

When we create a reference to an object, we use the assignment operator. For example,

String s1 = ”hi”;
String s2 = s1; // s2 is a reference of s1

A program to illustrate the creation of a StringBuffer object using a reference stored in a variable of type String is given below −

Example

 Live Demo

public class Example {
   public static void main(String args[]) {
      String input = "hey";
      String s1 = input; // creating a reference of the String
      StringBuffer s = new StringBuffer(s1);
      System.out.println(s);
   }
}

Output

The output is as follows −

Hey
Updated on: 2020-06-27T13:00:02+05:30

248 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements