Java - ObjectStreamField getTypeString() method



Description

The Java ObjectStreamField getTypeString() method returns the JVM type signature.

  • getTypeString() returns a String that represents the full type name of the field if it's an object (like java.lang.String, java.util.Date, etc.).

  • It returns null if the field is a primitive type (int, double, boolean, etc.).

Declaration

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

public String getTypeString()

Parameters

NA

Return Value

This method returns null if this field has a primitive type.

Exception

NA

Example - Usage of ObjectStreamField getTypeString() method

The following example shows the usage of ObjectStreamField getTypeString() 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 string of the field
      System.out.println("" + field.getTypeString());

      // 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("isSet");

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

Output

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

null
[Z

Example - Print type strings for object fields

The following example shows the usage of ObjectStreamField getTypeString() method. We're printing type names of fields from a Person 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(Person.class);
      ObjectStreamField[] fields = osc.getFields();

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

   static class Person implements Serializable {
      private static final long serialVersionUID = 1L;
      String name;
      int age;
   }
}

Output

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

Field names and type strings:
- age: null
 - name: Ljava/lang/String;

Explanation

  • For the String name, getTypeString() returns "Ljava/lang/String;".

  • For the primitive int age, it returns null.

  • L...; is the Java internal type descriptor format for objects.

Example - Differentiate between object and primitive fields using type string

The following example shows the usage of ObjectStreamField getTypeString() method. We're checking whether a field is a primitive or an object.

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) {
         if (field.getTypeString() != null) {
            System.out.println("Field '" + field.getName() + "' is an Object of type: " + field.getTypeString());
         } else {
            System.out.println("Field '" + field.getName() + "' is a Primitive type (" + field.getType().getSimpleName() + ").");
         }
      }
   }

   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 (boolean).
Field 'price' is a Primitive type (float).
Field 'productName' is an Object of type: Ljava/lang/String;
  • getTypeString() gives the object type encoding if the field is a reference.

  • If it's a primitive type, getTypeString() returns null, and we use getType() to get the primitive type.

java_io_objectstreamfield.htm
Advertisements