How to use StringBuffer class In Java
StringBuffer is a class that is used to concatenate multiple values into a String.
Example 1:
Java
// Java Program to Illustrate // Integer to String Conversions // Using StringBuffer Class // Main class class GFG { // Main driver method public static void main(String args[]) { // Integer input value int f = 1234 ; // Creating an object of StringBuffer class StringBuffer sb = new StringBuffer(); sb.append(f); String str6 = sb.toString(); System.out.println( "String str6 = " + str6); } } |
String str6 = 1234
Example 2:
Java
// Java Program to Illustrate // Integer to String Conversions // Using StringBuffer Class // Main class class GFG { // Main driver method public static void main(String args[]) { String str6 = new StringBuffer().append( 1234 ).toString(); System.out.println( "String str6 = " + str6); } } |
String str6 = 1234
Java Convert int to String | Different Ways Of Conversion
Converting Integers to Strings involves using the Integer classes toString() or String.valueOf() for direct conversion. String.format() is another method offering flexible formatting options. Using StringBuilder or StringBuffer for appending integer values as strings is efficient for extensive string manipulation.
We generally counter with such conversion articles because many operations can be performed over a string while we are limited to when it comes to integers. We have a wide varied list of in-built methods in the String class that helps us perform hassle-free operations.
Suppose we are required to concatenate two integers then it would become a tedious job as we need to go through as we need to deal with the number system corresponding to which we will be playing mathematics within the number system. But in order to convert integers to strings in Java, we have some inbuilt methods and classes which make our work too easy.
Tip: We generally convert primitive class data members types though we have the concept of Wrapper classes to Strings because in practical programming in java we deal with strings.