Java - ObjectStreamField getTypeCode() method



Description

The Java ObjectStreamField getTypeCode() method returns character encoding of field type. The encoding is as follows −

 B            byte
 C            char
 D            double
 F            float
 I            int
 J            long
 L            class or interface
 S            short
 Z            boolean
 [            array
  • getTypeCode() returns a single character (char) that represents the type of the field.

  • It's mostly used internally by the serialization system but can be helpful when building custom serializers.

Declaration

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

public char getTypeCode()

Parameters

NA

Return Value

This method returns the typecode of the serializable field.

Exception

NA

Example - Usage of ObjectStreamField getTypeCode() method

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

ObjectStreamFieldDemo.java

package com.tutorialspoint;

import java.io.ObjectStreamClass;
import java.io.ObjectStreamField;
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");

      // get the type code of the field
      System.out.println("" + field.getTypeCode());

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

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

      // get the type code of the field2
      System.out.println("" + field2.getTypeCode());
   }
}

Output

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

I 
Z

Example - Print field names with their type codes

The following example shows the usage of ObjectStreamField getTypeCode() method. We're displaing the name and type code of each serializable field in a Student class.

ObjectStreamFieldDemo.java

package com.tutorialspoint;

import java.io.ObjectStreamClass;
import java.io.ObjectStreamField;
import java.io.Serializable;

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

      System.out.println("Field names and their type codes:");
      for (ObjectStreamField field : fields) {
         System.out.println(" - " + field.getName() + ": " + field.getTypeCode());
      }
   }

   static class Student implements Serializable {
      private static final long serialVersionUID = 1L;
      String name;
      int rollNumber;
      double marks;
   }
}

Output

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

Field names and their type codes:
 - marks: D
 - rollNumber: I
 - name: L

Explanation

  • getTypeCode() gives the compact internal type code −

    • 'L' → String (an object)

    • 'I' → int

    • 'D' → double

  • Helps you understand how Java serialization categorizes fields internally.

Example - Detect and handle primitive vs. object fields

The following example shows the usage of ObjectStreamField getTypeCode() method. We need to detect which fields are primitive types vs. objects.

ObjectStreamFieldDemo.java

package com.tutorialspoint;

import java.io.ObjectStreamClass;
import java.io.ObjectStreamField;
import java.io.Serializable;

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

      for (ObjectStreamField field : fields) {
         char typeCode = field.getTypeCode();
         if (typeCode == 'L') {
            System.out.println("Field '" + field.getName() + "' is an Object type.");
         } else {
            System.out.println("Field '" + field.getName() + "' is a Primitive type (code: " + typeCode + ").");
         }
      }
   }

   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−

Field 'available' is a Primitive type (code: Z).
Field 'price' is a Primitive type (code: F).
Field 'productName' is an Object type.

Explanation

  • getTypeCode() returns 'L' for String (object), 'F' for float, 'Z' for boolean.

  • You can easily distinguish primitive fields from reference fields based on the type code.

java_io_objectstreamfield.htm
Advertisements