Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

java.rmi.MarshalledObject Class in Java

  • Difficulty Level : Hard
  • Last Updated : 26 Nov, 2021

java.rmi.MarshalledObject is a java class, a MarshalledObject contains a byte stream with the serialized representation of an object given to its constructor, The contained object is serialized and deserialized with the same serialization semantics used for marshaling and unmarshaling parameters.

Signature 

public final class MarshalledObject<T> extends Object implements Serializable

Constructor

MarshalledObject(T object) – MarshalledObject(T object) initializes a new instance of MarshalledObject class that contains the serialized representation of the current state of the supplied object.

MarshalledObject m = new MarshalledObject(T object);

Note: m is the new instance of MarshalledObject class.

Methods

MarshalledObject class contains three method named as –

  1. hashCode()
  2. get()
  3. equals(Object object)

Let us discuss all three methods of this class individually to get a better understanding. Here we go:

1.  MarshalledObject.hashCode() method

It is a part of java.rmi.MarshalledObject class, hashCode() method will return hash code associated with this MarshalledObject.

Syntax:

public int hashCode().

Method Return Type: hashCode() method has int return type and will return a hash code for this MarshalledObject

How to invoke hashCode() method

Step 1: First create an instance of MarshalledObject and pass on the object to be serialized.

MarshalledObject marshalledObject = new MarshalledObject(object);

Step 2: Now invoke the hashCode() method to get the hash code of this marshalledObject

int code = marshalledObject.hashCode();

Example: Java program to get the hash code using MarshalledObject.hashCode() method

Java




// Java program to 
// get the hash code
import java.io.*;
import java.rmi.*;
  
// create a serialized class
class tmp implements Serializable {
    public int x;
}
class GFG {
    public static void main(String[] args)
    {
        // invoke getHashCode 
          // method to get hashCode
        getHashCode(new tmp());
    }
    @SuppressWarnings("unchecked")
    public static void getHashCode(tmp t)
    {
        try {
            MarshalledObject marshalledObject = new MarshalledObject(t);
            
            // invoke hashCode method for this
            // marshalledObject to get hash code
            System.out.println("Hash code for this marshalled object is " + marshalledObject.hashCode());
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

Output:

Hash code for this marshalled object is -571669764

2. MarshalledObject.get() method

It is a part of java.rmi.MarshalledObject class, get() method will return a new copy of the contained marshalledObject.

Method Signature

public T get() throws IOException,
ClassNotFoundException.

Method Return Type: get() method will return the copy of contained MarshalledObject.

Parameter: get() method has no parameter

Exception: get() method might throw IOException,ClassNotFoundException.

How to invoke get() method?

Step 1: First create an instance of MarshalledObject and pass on the object to be serialized.

MarshalledObject marshalledObject = new MarshalledObject(object);

Step 2: Now invoke the get() method to get the new copy of this marshalledObject.

Object obj = marshalledObject.get();

Example: Java program to get the copy of MarshalledObject using MarshalledObject.get() method

Java




// Java program to get 
// copy of marshalledObject
import java.io.*;
import java.rmi.*;
  
// create a serialized class
class tmp implements Serializable {
    public int x;
}
class GFG {
    public static void main(String[] args)
    {
        // invoke get method to get 
          // copy of marshalledObject
        get(new tmp());
    }
    @SuppressWarnings("unchecked")
    public static void get(tmp t)
    {
        try {
            
            MarshalledObject marshalledObject = new MarshalledObject(t);
            
            // invoke get method for this
            // marshalledObject to get copy
              // of marshalled object
            System.out.println("Copy marshalled object is " + marshalledObject.get());
            System.out.println("Original marshalled object is " + t);
  
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

Output:

Copy marshalled object is tmp@66cd51c3
Original marshalled object is tmp@63c12fb0

3.  MarshalledObject.equals() Method

It is a part of java.rmi.MarshalledObject class, equals() method will compare this MarshalledObject to another object, if both the serialized object is the same then this method will return true otherwise false.

Method Signature

public boolean equals(Object obj).

Method Return Type: equals() method has boolean return type, It will return true if the argument refers to a MarshalledObject that contains exactly the same serialized representation of an object as this one does.

Method Parameter: equals() method has one parameter of Object Type.

How to invoke equals() method?

Step 1: first create an instance of both MarshalledObject and pass on the object to be serialized.

MarshalledObject marshalledObjectOne = new MarshalledObject(object);
MarshalledObject marshalledObjectTwo = new MarshalledObject(object);

Step 2: Now invoke the equals() method to compare marshalledObject with the argument passed to the equals() method.

boolean isSame = marshalledObjectOne.equals(marshalledObjectTwo);

Example: Java program to compare this MarshalledObject using MarshalledObject.equals() method 

Java




// Java program to compare 
// two marshalled object
import java.io.*;
import java.rmi.*;
  
// create a serialized class
class tmp implements Serializable {
    public int x;
}
class GFG {
    public static void main(String[] args)
    {
        // invoke compare method to to
          // compare the marshalled object
        compare(new tmp(),new tmp());
    }
    @SuppressWarnings("unchecked")
    public static void compare(tmp a,tmp b)
    {
        try {
            MarshalledObject marshalledObjectOne = new MarshalledObject(a);
            MarshalledObject marshalledObjectTwo = new MarshalledObject(b);
  
            // invoke equals method for this
            // marshalledObject to compare 
              // the marshalled object
            System.out.println("marshalledObjectOne and marshalledObjectTwo are same : "
                + marshalledObjectOne.equals(marshalledObjectTwo));
  
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

Output:

marshalledObjectOne and marshalledObjectTwo are same : true

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!