- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create a 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
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
- Related Articles
- What is the difference between a String object and a StringBuffer object in java?
- Change a single character in a Java StringBuffer object in Java
- Get the length of a StringBuffer Object in Java
- Remove characters in a range from a Java StringBuffer Object
- Remove a character from a Java StringBuffer object
- Reverse the sequence of characters in a StringBuffer Object in Java
- How to create a thread using method reference in Java?\n
- Where to use a StringBuffer/StringBuilder than a String in Java?
- How to call a function from a string stored in a variable PHP?
- Set the capacity value for a StringBuffer object in Java
- Call append() method to construct a StringBuffer object in Java
- Java Program to create a boolean variable from string
- Why should we use a StringBuffer instead of a String in Java?\n
- Java program to insert a String into a Substring of StringBuffer
- Is String a primitive data type or an object in Java?

Advertisements