title: Casting Character to char and char to Character Java
body: <p>Can somebody please explain me why this code is working fine?</p>
  
  <pre><code>import java.io.*;
  
  public class Array {
      public static void main(String[] args) throws IOException, ClassNotFoundException {
          FileOutputStream fo=new FileOutputStream("Character.dat");
          ObjectOutputStream oos=new ObjectOutputStream(fo);
          FileInputStream fi=new FileInputStream("Character.dat");
          ObjectInputStream ois=new ObjectInputStream(fi);
          Character c = 'h';
          oos.writeObject(c);
          Character arr = (char)ois.readObject();
          System.out.println(arr);
          fo.close();
          fi.close();
          oos.close();
          ois.close();
      }
  }
  </code></pre>
  
  <p>In Line 10, I have created a <code>Character</code> and I have serialized it in line 11 and written into a file.
  In line 12, I have deserialized that object, it returns a reference of <code>Object</code>, thus we have to downcast it into <code>Character</code>. In line 12, I am downcasting it into char (which is a primitive data type and not a class) and still its working fine. Why is that?</p>