Java PushbackReader Class

Java PushbackReader class is a class in the Java IO API that allows you to “push back” or unread data from a reader, which can be helpful when you want to read a character from a stream and then look ahead to see if it’s part of a larger pattern. The PushbackReader class extends the FilterReader class, which in turn extends the abstract Reader class.

To use the PushbackReader class, you first need to create an instance of it, passing in the reader you want to read from as an argument. Once you have an instance of PushbackReader, you can use its various methods to read data from the underlying reader and push back any characters that you don’t need.

Here’s an example code snippet that demonstrates how to use the PushbackReader class:

import java.io.*;

public class PushbackReaderExample {
    public static void main(String[] args) throws IOException {
        String data = "Hello world!";
        Reader reader = new StringReader(data);
        PushbackReader pushbackReader = new PushbackReader(reader);

        int c = pushbackReader.read();
        System.out.println((char) c); // prints 'H'

        pushbackReader.unread('H');
        c = pushbackReader.read();
        System.out.println((char) c); // prints 'H' again

        pushbackReader.unread('H');
        pushbackReader.unread('!');
        c = pushbackReader.read();
        System.out.println((char) c); // prints '!' since we pushed back 'H' twice
    }
}

In this example, we create a PushbackReader instance using a StringReader that reads “Hello world!”. We then read the first character (‘H’) from the PushbackReader and print it. We then push the ‘H’ back into the PushbackReader and read again, printing ‘H’ again. Finally, we push back ‘!’ and ‘H’ again, and read a third time, which returns ‘!’ since we have pushed back ‘H’ twice.

Class declaration:

In Java, the class declaration is used to define a new class. The basic syntax for declaring a class in Java is as follows:

[access modifier] class ClassName [extends SuperClass] [implements Interface1, Interface2, ...] {
    // class variables
    // constructors
    // methods
}

Let’s break down each component of the class declaration:

  • Access modifier: This is an optional keyword that specifies the visibility of the class. The four access modifiers in Java are public, protected, private, and package-private (no modifier).
  • Class name: This is the name of the class, which follows the same naming conventions as variable names in Java (i.e., camel case with the first letter capitalized).
  • Extends clause: This is an optional keyword that specifies the superclass of the class being defined. A class can only extend one superclass, and it must be the first clause in the class declaration.
  • Implements clause: This is an optional keyword that specifies the interfaces that the class implements. A class can implement one or more interfaces, separated by commas.
  • Class variables: These are variables that are defined at the class level and are shared by all instances of the class. Class variables are declared using the static keyword.
  • Constructors: These are special methods that are used to initialize instances of the class. Constructors have the same name as the class and do not have a return type.
  • Methods: These are functions that define the behavior of the class. Methods can be declared with various access modifiers and can have a return type and parameters.

Here’s an example of a simple class declaration in Java:

public class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void sayHello() {
        System.out.println("Hello, my name is " + name + " and I am " + age + " years old.");
    }
}

In this example, we define a class called Person with two private instance variables (name and age), a constructor that takes two parameters and initializes the instance variables, and a method called sayHello() that prints out a greeting using the instance variables.

Class Methods:

In Java, a class can have methods, which are functions that define the behavior of the class. Methods are defined within the class declaration and can be invoked on instances of the class.

The basic syntax for defining a method in Java is as follows:

[access modifier] [static] returnType methodName([parameter list]) {
    // method body
    return [expression];
}

Let’s break down each component of the method definition:

  • Access modifier: This is an optional keyword that specifies the visibility of the method. The four access modifiers in Java are public, protected, private, and package-private (no modifier).
  • Static keyword: This is an optional keyword that specifies that the method belongs to the class rather than to instances of the class. Static methods can be invoked on the class itself, rather than on instances of the class.
  • Return type: This is the data type that the method returns. If the method doesn’t return anything, the return type is void.
  • Method name: This is the name of the method, which follows the same naming conventions as variable names in Java (i.e., camel case with the first letter lowercased).
  • Parameter list: This is a comma-separated list of input parameters that the method accepts. Each parameter consists of a data type and a parameter name.
  • Method body: This is the code that defines the behavior of the method.
  • Return statement: This is an optional statement that specifies the value that the method returns. The return statement must match the return type of the method.

Here’s an example of a class with a method in Java:

public class Calculator {
    public static int add(int a, int b) {
        return a + b;
    }
}

In this example, we define a class called Calculator with a static method called add() that takes two int parameters and returns their sum. The add() method can be invoked on the class itself, like this:

int sum = Calculator.add(2, 3);

The sum variable would then have a value of 5.

Example of PushbackReader class:

Sure! Here’s an example of using the PushbackReader class in Java:

import java.io.*;

public class PushbackReaderExample {
    public static void main(String[] args) {
        String str = "Hello World!";
        try {
            Reader reader = new StringReader(str);
            PushbackReader pushbackReader = new PushbackReader(reader);

            // Read the first character
            int c = pushbackReader.read();
            System.out.println("Read: " + (char)c);

            // Push back the character
            pushbackReader.unread(c);

            // Read the character again
            c = pushbackReader.read();
            System.out.println("Read again: " + (char)c);

            // Read the rest of the string
            char[] buffer = new char[11];
            pushbackReader.read(buffer);
            System.out.println("Read buffer: " + new String(buffer));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

In this example, we create a StringReader object from the string “Hello World!”, and then pass that reader to a PushbackReader object. We then read the first character from the PushbackReader, push it back onto the reader, and read it again. Finally, we read the rest of the string into a buffer and print it out.

The output of this program would be:

Read: H
Read again: H
Read buffer: ello World

As you can see, the PushbackReader allows us to read characters from a reader and then push them back onto the reader, effectively “unread” them so that they can be read again later.