CharArrayWriter size() method in Java with examples

The size() method of the CharArrayWriter class in Java is used to get the current size of the buffer.

Syntax:

public int size()

Parameters: This method does not accept any parameter.



Return Value: This method returns an integer value which signifies the current size of the buffer.

Below program illustrate the above method:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate
// the size() method
  
import java.io.*;
  
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
  
        // Initailizing the character array
        char[] geek = { 'G', 'E', 'E', 'K', 'S' };
  
        // Initailizing the CharArrayWriter
        CharArrayWriter char_array1
            = new CharArrayWriter();
  
        for (int c = 72; c < 77; c++) {
            // Use of write(int char)
            // Writer int value to the Writer
            char_array1.write(c);
        }
  
        // Use of size() method
        System.out.println("\nSize of char_array1 : "
                           + char_array1.size());
    }
}

chevron_right


Output:

Size of char_array1 : 5

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate
// the size() method
  
import java.io.*;
  
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
  
        // Initailizing the character array
        char[] geek
            = { 'G', 'O', 'P', 'A', 'L', 'L' };
  
        // Initailizing the CharArrayWriter
        CharArrayWriter char_array1
            = new CharArrayWriter();
  
        for (int c = 72; c < 78; c++) {
            // Use of write(int char)
            // Writer int value to the Writer
            char_array1.write(c);
        }
  
        // Use of size() method
        System.out.println("\nSize of char_array1 : "
                           + char_array1.size());
    }
}

chevron_right


Output:

Size of char_array1 : 6

Reference: https://docs.oracle.com/javase/10/docs/api/java/io/CharArrayWriter.html#size()



My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.