Java - ObjectStreamField isPrimitive() method



Description

The Java ObjectStreamField isPrimitive() method returns true if this field has a primitive type.

  • isPrimitive() returns true if the field is of a primitive type (int, double, boolean, etc.).

  • Returns false if the field is an object type (String, Date, CustomClass, etc.).

  • Very useful for −

    • Dynamically separating primitive and object fields.

    • Building custom serializers.

    • Debugging serialization structure.

Declaration

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

public boolean isPrimitive()

Parameters

NA

Return Value

This method returns true if and only if this field corresponds to a primitive type.

Exception

NA

Example - Usage of ObjectStreamField isPrimitive() method

The following example shows the usage of ObjectStreamField isPrimitive() method.

ObjectStreamFieldDemo.java

package com.tutorialspoint;

import java.io.*;
import java.util.Calendar;

public class ObjectStreamFieldDemo {
   public static void main(String[] args) {

      // create a new object stream class for Integers
      ObjectStreamClass osc = ObjectStreamClass.lookup(Integer.class);

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

      // check if field is primitive
      System.out.println("" + field.isPrimitive());

      // create a new object stream class for calendar
      ObjectStreamClass osc2 = ObjectStreamClass.lookup(Calendar.class);

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

      // check if field is primitive
      System.out.println("" + field2.isPrimitive());
   }
}

Output

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

true
false

Example - Print whether each field is primitive or object

The following example shows the usage of ObjectStreamField isPrimitive() method. We're checking and printing if each field in a User class is primitive.

ObjectStreamFieldDemo.java

package com.tutorialspoint;

import java.io.*;

public class ObjectStreamFieldDemo {
   public static void main(String[] args) {
      ObjectStreamClass osc = ObjectStreamClass.lookup(User.class);
      ObjectStreamField[] fields = osc.getFields();

      System.out.println("Checking if fields are primitive:");
      for (ObjectStreamField field : fields) {
         System.out.println(" - " + field.getName() + " isPrimitive: " + field.isPrimitive());
      }
   }

   static class User implements Serializable {
      private static final long serialVersionUID = 1L;
      String username;
      int id;
      boolean active;
   }
}

Output

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

Checking if fields are primitive:
  - active isPrimitive: true
  - id isPrimitive: true
- username isPrimitive: false

Explanation

  • username is an object (String), so isPrimitive() returns false.

  • id and active are primitive types (int, boolean), so isPrimitive() returns true.

Example - Count how many primitive vs object fields

The following example shows the usage of ObjectStreamField isPrimitive() method. We're counting primitive and non-primitive fields separately for a Product class.

ObjectStreamFieldDemo.java

package com.tutorialspoint;

import java.io.*;

public class ObjectStreamFieldDemo {
   public static void main(String[] args) {
      ObjectStreamClass osc = ObjectStreamClass.lookup(Product.class);
      ObjectStreamField[] fields = osc.getFields();

      int primitiveCount = 0;
      int objectCount = 0;

      for (ObjectStreamField field : fields) {
         if (field.isPrimitive()) {
            primitiveCount++;
         } else {
            objectCount++;
         }
      }

      System.out.println("Primitive fields: " + primitiveCount);
      System.out.println("Object fields: " + objectCount);
   }

   static class Product implements Serializable {
      private static final long serialVersionUID = 1L;
      String productName;
      float price;
      boolean available;
   }
}

Output

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

Primitive fields: 2
Object fields: 1

Explanation

  • productName is an object (String).

  • price (float) and available (boolean) are primitives.

java_io_objectstreamfield.htm
Advertisements