Java.io.ObjectStreamField.compareTo() Method



Description

The java.io.ObjectStreamField.compareTo(Object obj) method compares this field with another ObjectStreamField. Return -1 if this is smaller, 0 if equal, 1 if greater. Types that are primitives are "smaller" than object types. If equal, the field names are compared.

Declaration

Following is the declaration for java.io.ObjectStreamField.compareTo() method.

public int compareTo(Object obj)

Parameters

obj − The object to be compared.

Return Value

This method returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

Exception

NA

Example

The following example shows the usage of java.io.ObjectStreamField.compareTo() method.

package com.tutorialspoint;

import java.io.*;

public class ObjectStreamFieldDemo {
   public static void main(String[] args) {
   
      // create a new object stream class for Integers
      ObjectStreamClass osc = ObjectStreamClass.lookupAny(Integer.class);

      // get the field value from Integer class
      ObjectStreamField field = osc.getField("value");

      // create a new object stream class for floats
      ObjectStreamClass osc2 = ObjectStreamClass.lookupAny(Float.class);

      // get the field value from Integer class
      ObjectStreamField field2 = osc.getField("value");

      // compare with another field
      System.out.println("" + field.compareTo(field2));
   }
}

Let us compile and run the above program, this will produce the following result −

0
java_io_objectstreamfield.htm
Advertisements