Метод PrintWriter print (Object) в Java с примерами

Опубликовано: 8 Февраля, 2022

Метод print (Object) класса PrintWriter в Java используется для печати указанного объекта в потоке. Этот объект принимается как параметр.

Синтаксис:

 public void print (объект объекта)

Параметры: этот метод принимает объект обязательного параметра, который является объектом, который будет напечатан в потоке.

Возвращаемое значение: этот метод не возвращает никакого значения.

Ниже приведены методы, иллюстрирующие работу метода print (Object):

Program 1:

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

Program 2:

// Java program to demonstrate
// PrintWriter print(Object) method
  
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        try {
  
            // Create a PrintWriter instance
            PrintWriter writer
                = new PrintWriter(System.out);
  
            // Get the String Object
            // to be printed in the stream
            String object = "GFG";
  
            // print the object
            // to this writer using print() method
            // This will put the object in the stream
            // till it is printed on the console
            writer.print(object);
  
            writer.flush();
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}
Output:
GFG

Вниманию читателя! Не переставай учиться сейчас. Ознакомьтесь со всеми важными концепциями Java Foundation и коллекций с помощью курса "Основы Java и Java Collections" по доступной для студентов цене и будьте готовы к работе в отрасли. Чтобы завершить подготовку от изучения языка к DS Algo и многому другому, см. Полный курс подготовки к собеседованию .