StringBuilder toString() method in Java with Examples (original ) (raw )Last Updated : 23 Jan, 2023
The toString() method of the StringBuilder class is the inbuilt method used to return a string representing the data contained by StringBuilder Object. A new String object is created and initialized to get the character sequence from this StringBuilder object and then String is returned by toString(). Subsequent changes to this sequence contained by Object do not affect the contents of the String.
Syntax:
public abstract String toString() ;
Return Value: This method always returns a string representing the data contained by StringBuilder Object.
As this is a very basic method been incorporated in java so we will be discussing it within our clean java programs to get to know its working. INternally in the class, it is defined as follows which will give you a better understanding of how it actually works.
return getClass().getName()+ "@" + Integer.toHexString(hashCode);
Example 1:
Javaimport
java.io.*;
class
GFG {
`` public
static
void
main(String[] args)
`` {
`` StringBuilder str
`` =
new
StringBuilder(
"GeeksForGeeks"
);
`` System.out.println(
"String contains = "
`` + str.toString());
`` }
}
Output
String contains = GeeksForGeeks
Example 2:
Javaimport
java.io.*;
class
GFG {
`` public
static
void
main(String[] args)
`` {
`` StringBuilder str =
new
StringBuilder(
`` "Geeks for Geeks contribute"
);
`` System.out.println(
"String contains = "
`` + str.toString());
`` }
}
Output
String contains = Geeks for Geeks contribute
Now we are done with discussing basic examples let us operate the same over an array of strings by converting it to a single string using to.String() method. Here we will create a StringBuilder class object then we will store the array of string as its object. Later on, we will create another string and will throw all elements in this. Lastly, we will print this string.
Example 3:
Javaimport
java.io.*;
import
java.util.*;
class
GFG {
`` public
static
void
main(String[] args)
`` {
`` String gfg[] = {
"Are"
,
"You"
,
"A"
,
"Programmer"
};
`` StringBuilder obj =
new
StringBuilder();
`` obj.append(gfg[
0
]);
`` obj.append(
" "
+ gfg[
1
]);
`` obj.append(
" "
+ gfg[
2
]);
`` obj.append(
" "
+ gfg[
3
]);
`` String str = obj.toString();
`` System.out.println(
`` "Single string generated using toString() method is --> "
`` + str);
`` }
}
Output
Single string generated using toString() method is --> Are You A Programmer
Similar Reads
Java Basic Programs
Java Program to Display All Prime Numbers from 1 to N For a given number N, the purpose is to find all the prime numbers from 1 to N. Examples: Input: N = 11Output: 2, 3, 5, 7, 11Input: N = 7Output: 2, 3, 5, 7 Approach 1: Firstly, consider the given number N as input.Then apply a for loop in order to iterate the numbers from 1 to N.At last, check if ea 4 min read
Java Program to Check Whether the Character is Vowel or Consonant In this article, we are going to learn how to check whether a character is a vowel or a consonant. So, the task is, for any given character, we need to check if it is a vowel or a consonant. As we know, vowels are ‘a’, ‘e’, ‘i’, ‘o’, ‘u’ and all the other characters (i.e. ‘b’, ‘c’, ‘d’, ‘f’ …..) are 4 min read
Java Program to Find Sum of Fibonacci Series Numbers of First N Even Indexes For a given positive integer N, the purpose is to find the value of F2 + F4 + F6 +………+ F2n till N number. Where Fi indicates the i'th Fibonacci number. The Fibonacci Series is the numbers in the below-given integer sequence. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ……Examples: Input: n = 4 Output: 3 4 min read
Java Pattern Programs
Java Conversion Programs
Java Classes and Object Programs
Java Methods Programs
Java Searching Programs
Java 1-D Array Programs
Java Program to Find Largest Element in an Array Finding the largest element in an array is a common programming task. There are multiple approaches to solve it. In this article, we will explore four practical approaches one by one to solve this in Java. Example Input/Output: Input: arr = { 1, 2, 3, 4, 5}Output: 5 Input: arr = { 10, 3, 5, 7, 2, 12 4 min read
Java Program to Find Common Elements Between Two Arrays Given two arrays and our task is to find their common elements. Examples:Input: Array1 = ["Article", "for", "Geeks", "for", "Geeks"], Array2 = ["Article", "Geeks", "Geeks"]Output: [Article, Geeks] Input: Array1 = ["a", "b", "c", "d", "e", "f"], Array2 = ["b", "d", "e", "h", "g", "c"]Output: [b, c, d 4 min read
Java 2-D Arrays (Matrix) Programs
Java Program to Add two Matrices Given two matrices A and B of the same size, the task is to add them in Java. Examples: Input: A[][] = {{1, 2}, {3, 4}} B[][] = {{1, 1}, {1, 1}} Output: {{2, 3}, {4, 5}} Input: A[][] = {{2, 4}, {3, 4}} B[][] = {{1, 2}, {1, 3}} Output: {{3, 6}, {4, 7}} Program to Add Two Matrices in JavaFollow the St 2 min read
Java Program to Find Transpose of Matrix Transpose of a matrix is obtained by changing rows to columns and columns to rows. In other words, the transpose of A[ ][ ] is obtained by changing A[i][j] to A[j][i]. Example of First Transpose of MatrixInput: [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ]Output: [ [ 1 , 4 , 7 ] , [ 2 , 5 , 8 ] 5 min read
Java Program to Find the Normal and Trace of a Matrix For a given 2D matrix, the purpose is to find the Trace and Normal of the matrix. In this article, we will learn about Normal and Trace of a Matrix in Java. Examples : Input : matrix[][] = {{1, 4, 4}, {2, 3, 7}, {0, 5, 1}}; Output : Normal = 11 Trace = 5 Explanation : Normal = sqrt(1*1+ 4*4 + 4*4 + 3 min read
Java String Programs