Abstract Class Format in Java

Class Class in Java

Abstract Class Format in Java

The format is an abstract class that is the base class that is the superclass of all classes that handle locale-sensitive for all number, date, and string formatting classes in the java.text package. It defines two abstract methods that are implemented by subclasses.

format() converts an object to a string using the formatting rules encapsulated by the Format subclass and optionally appends the resulting string to an existing StringBuffer.

Ads code goes here

parseObject() performs the reverse operation–it parses a formatted string and returns the corresponding object.

They take the information from a string and construct an appropriate object.

Status information for these two operations is returned in FieldPosition and ParsePosition objects. The non-abstract methods of this class are simple shortcuts that rely on implementations of the abstract methods.

The structure of the class Format is given by

public abstract class java.text.Format extends java.lang.Object implements java.io.Serializable, java.lang.Cloneable {
// Default Constructor: 
public Format()
// Public Instance Methods
public Object clone(); // Overrides Object
public final String format(Object obj);
public abstract StringBuffer format(Object obj, StringBuffer toAppendTo,FieldPosition pos);
public abstract Object parseObject(String source, ParsePosition status);
public Object parseObject(String source) throws ParseException;
}

The details of the class structure are given as follows:

public Format()

public Format() is the default constructor

public Object clone(); // Overrides Object

public Object clone(); method creates a copy of this Format and returns it.

This method returns a copy of this Format.

public final String format(Object obj);

public final String format(Object obj) method formats the given object by calling format(Object, StringBuffer,
FieldPosition) with an empty StringBuffer and a FieldPosition that has a value of 0.

READ  Internationalization Concept Simplified In Java

This method returns a string that contains a formatted representation of the object.

Parameter
obj – The object to be formatted.

public abstract StringBuffer format(Object obj, StringBuffer toAppendTo,FieldPosition pos);

public abstract StringBuffer format(Object obj, StringBuffer toAppendTo,FieldPosition pos) method formats the given object and appends the result to the given StringBuffer. After the object has been formatted, the beginning and ending indices of the given FieldPosition are updated to reflect the field’s position in the formatted output.

A subclass of Format must implement this method.

This method returns the given buffer toAppendTo with the formatted representation of the object appended to it.

Parameter
obj – The object to be formatted.
toAppendTo – A StringBuffer on which to append the formatted information.
pos – A field.

public abstract Object parseObject(String source, ParsePosition status);

public abstract Object parseObject(String source, ParsePosition status) method parses the given text, starting at the specified position, and returns an object created from the data. After the string has been parsed, the given ParsePosition object is updated so that its index is after the parsed text.

A subclass of Format must implement this method.

This method returns the object represented by the text starting at the given position.

Parameter
source – The string to be parsed.
status – A ParsePosition object that specifies a position in the string.

public Object parseObject(String source) throws ParseException;

public Object parseObject(String source) throws ParseException method parses the given text and returns an appropriate object. It does this by calling parseObject(String, ParsePosition) with a ParsePosition that has an index of 0.

This method returns the object represented by the given string.

READ  Class CollationKey in Java

Parameter
source – The string to be parsed.

Apart from these methods Format class also has inherited methods from class- Object. They are as follows:

  • finalize()
  • hashCode()
  • notifyAll()
  • wait()
  • wait(long, int)
  • equals(Object)
  • getClass()
  • notify()
  • toString()
  • wait(long)

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *