Interview Q&A · Java · 11 questions in this topic

Java Serialization — Java Interview Q&A

All Java topics

How Java converts objects to bytes and back, what must hold for it to work, and how versioning and exclusions behave. Part of Serialization · 294 questions in the Java bank.

What is serialization?

Answer

Serialization is the conversion of an object into a byte stream so it can be stored in a file or sent across a network. The stream captures the object data together with type information needed to rebuild it. Deserialization is the reverse step, reconstructing an equivalent object from those bytes.

Key points

  1. 1Output is bytes, not text: the stream encodes field values plus class metadata.
  2. 2Purpose is transport and storage: bytes can persist or travel where live objects cannot.
  3. 3Deserialization rebuilds the graph without invoking ordinary constructors.

What is serialization used for?

Answer

Serialization persists object state to files or databases for later restoration by the same or another run. It transfers objects across network boundaries, historically through RMI and related remoting schemes. It also supports deep copying through a byte-array round trip and distributes objects across JVMs that share the class definitions.

Key points

  1. 1Persistence: save state now, restore it later without manual field mapping.
  2. 2Communication: pass objects over the network as remote-call payloads.
  3. 3Copying: a serialize-then-deserialize round trip duplicates a whole reachable graph.
  4. 4Distribution: move work or state between JVMs in a cluster.

What are the alternatives to Java serialization?

Answer

Modern systems usually prefer JSON libraries such as Jackson or Gson, which produce readable text that any language can parse. XML binding frameworks such as JAXB serve a similar role where XML contracts are required. For compact binary traffic, schema-based formats such as Protocol Buffers or Avro add cross-language support with stronger version tolerance.

Key points

  1. 1JSON with Jackson or Gson: human-readable, language-neutral, web-friendly payloads.
  2. 2XML with JAXB: schema-validated documents for enterprise contracts.
  3. 3Protocol Buffers and Avro: compact binary encodings with explicit schema evolution rules.
  4. 4Motivation for switching: readability, cross-language use, security and versioning control.

💡 Name one option per category, JSON plus XML plus a binary schema format, to show breadth.

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits