Saturday, September 6, 2025
HomeLanguagesJavaFormatter out() method in Java with Examples

Formatter out() method in Java with Examples

The out() method is a built-in method of the java.util.Formatter which returns the destination for the output by the formatter.

Syntax:

public Appendable out()

Parameters: The function accepts no parameter.

Return Value: The function returns the destination for the output.

Exceptions: The function throws FormatterClosedException if the formatter has been closed before the function call.

Below is the implementation of the above function:

Program 1:




// Java program to implement
// the above function
  
import java.util.Formatter;
import java.util.Locale;
  
public class Main {
  
    public static void main(String[] args)
    {
  
        // Get the string Buffer
        StringBuffer buffer
            = new StringBuffer();
  
        // Object creation
        Formatter frmt
            = new Formatter(buffer,
                            Locale.CANADA);
  
        // Format a new string
        String name = "My name is Gopal Dave";
        frmt.format("What is your name? \n%s !",
                    name);
  
        // Print the Formatted string
        System.out.println(frmt);
  
        // Prints the destination of the output
        System.out.println("\nDestination: "
                           + frmt.out());
    }
}


Output:

What is your name? 
My name is Gopal Dave !

Destination: What is your name? 
My name is Gopal Dave !

Program 2:




// Java program to implement
// the above function
  
import java.util.Formatter;
import java.util.Locale;
  
public class Main {
  
    public static void main(String[] args)
    {
        try {
  
            // Get the string Buffer
            StringBuffer buffer
                = new StringBuffer();
  
            // Object creation
            Formatter frmt
                = new Formatter(buffer,
                                Locale.CANADA);
  
            // Format a new string
            String name = "My name is Gopal Dave";
            frmt.format("What is your name? \n%s !",
                        name);
  
            // Print the Formatted string
            System.out.println(frmt);
  
            // Formatter closed
            frmt.close();
  
            // Prints the destination of the output
            System.out.println("\nDestination: "
                               + frmt.out());
        }
        catch (Exception e) {
            System.out.println("Exception is: "
                               + e);
        }
    }
}


Output:

What is your name? 
My name is Gopal Dave !
Exception is: java.util.FormatterClosedException

Reference: https://docs.oracle.com/javase/10/docs/api/java/util/Formatter.html#out()

RELATED ARTICLES

Most Popular

Dominic
32270 POSTS0 COMMENTS
Milvus
82 POSTS0 COMMENTS
Nango Kala
6639 POSTS0 COMMENTS
Nicole Veronica
11805 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11869 POSTS0 COMMENTS
Shaida Kate Naidoo
6754 POSTS0 COMMENTS
Ted Musemwa
7029 POSTS0 COMMENTS
Thapelo Manthata
6705 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS