Método PrintStream println(char[]) en Java con ejemplos

El método println(char[]) de PrintStream Class en Java se usa para imprimir la array de caracteres especificada en el flujo y luego dividir la línea. Esta array de caracteres se toma como parámetro.

Sintaxis:

public void println(char[] charArray)

Parámetros: este método acepta un parámetro obligatorio charArray que es la array de caracteres que se imprimirá en el Stream.

Valor devuelto: este método no devuelve ningún valor.

Excepciones: este método devuelve NullPointerException si el charArray() especificado es nulo.

Los siguientes métodos ilustran el funcionamiento del método println(char[]):

Programa 1:

// Java program to demonstrate
// PrintStream println(char[]) method
  
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        try {
  
            // Create a PrintStream instance
            PrintStream stream
                = new PrintStream(System.out);
  
            // Get the character array
            // to be printed in the stream
            char[] charArray = { 'G', 'e', 'e', 'k', 's',
                                 'F', 'o', 'r',
                                 'G', 'e', 'e', 'k', 's' };
  
            // print the charArray
            // to this stream using println() method
            // This will put the charArray in the stream
            // till it is printed on the console
            stream.println(charArray);
  
            stream.flush();
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}
Producción:

GeeksForGeeks

Programa 2:

// Java program to demonstrate
// PrintStream println(char[]) method
  
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        try {
  
            // Create a PrintStream instance
            PrintStream stream
                = new PrintStream(System.out);
  
            // Get the character array
            // to be printed in the stream
            char[] charArray = { 'G', 'F', 'G' };
  
            // print the charArray
            // to this stream using println() method
            // This will put the charArray in the stream
            // till it is printed on the console
            stream.println(charArray);
  
            stream.flush();
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}
Producción:

GFG

Publicación traducida automáticamente

Artículo escrito por Kirti_Mangal y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *