Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
📘 Lesson  ·  Lesson 47

Serialization

Serialization क्या है

Serialization एक object को bytes की stream में बदलता है ताकि आप उसे file में save, store, या network पर भेज सकें। Deserialization उल्टा करता है — उन bytes से object फिर बनाता है। यह object को एक program run से आगे जीवित रहने देता है।

💡 Serialize = object → bytes (save/send को)। Deserialize = bytes → object (restore को)।

Serializable interface

import java.io.Serializable;

class Student implements Serializable {
    int id;
    String name;
    Student(int id, String name) { this.id = id; this.name = name; }
}

Class को serialize होने को Serializable implement करना ज़रूरी है। यह marker interface है — इसमें कोई methods नहीं; यह बस Java को बताता है object bytes में बदला जा सकता है।

Object serialize करना

import java.io.*;

Student s = new Student(1, "Aman");
ObjectOutputStream out =
    new ObjectOutputStream(new FileOutputStream("student.ser"));
out.writeObject(s);        // object -> bytes -> file
out.close();
System.out.println("Saved");
Saved

ObjectOutputStream object को writeObject से file में bytes के रूप में लिखता है। Object अब disk पर store है और बाद में restore हो सकता है।

वापस deserialize करना

ObjectInputStream in =
    new ObjectInputStream(new FileInputStream("student.ser"));
Student s = (Student) in.readObject();   // bytes -> object
in.close();
System.out.println(s.name);
Aman

ObjectInputStream bytes वापस पढ़ता है और readObject object फिर बनाता है। आप result को मूल type में cast करके दोबारा इस्तेमाल करते हैं।

transient keyword

class User implements Serializable {
    String name;
    transient String password;   // save नहीं होता

transient field serialization के दौरान छोड़ दिया जाता है, तो उसकी value file में नहीं लिखी जाती। इसे passwords जैसे sensitive data, या load के बाद दोबारा गिनने योग्य values को इस्तेमाल कीजिए।

आम इस्तेमाल

  • State save करना — बाद में reload को objects file में store।
  • Networking — programs के बीच या internet पर objects भेजना।
  • Caching — दोबारा इस्तेमाल को गिने objects रखना।

Serialization पूरे Java objects persist या transmit करने का standard तरीका है।

आम गलतियाँ

  • Serializable implement न करना। NotSerializableException देता है।
  • Streams close करना भूलना। उन्हें close कीजिए, या try-with-resources इस्तेमाल कीजिए।
  • readObject पर गलत cast। बिल्कुल मूल type में cast कीजिए।
  • Sensitive data serialize करना। passwords और secrets को transient चिह्नित कीजिए।

सारांश

  • Serialization object को save या भेजने को bytes में बदलता है।
  • Deserialization उन bytes से object फिर बनाता है।
  • Class को marker interface Serializable implement करना ज़रूरी।
  • लिखने/पढ़ने को ObjectOutputStream/ObjectInputStream इस्तेमाल कीजिए।
  • Serialization में fields छोड़ने को transient चिह्नित कीजिए।

आगे, file और I/O errors को सुरक्षित ढंग से संभालना सीखिए Java exception handling में, या memory manage कीजिए garbage collection से।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

Java में serialization क्या है?
Serialization एक object को bytes की stream में बदलना है ताकि वो file में save, store, या network पर भेजा जा सके। Deserialization उल्टा है — उन bytes को वापस object में बदलना। यह objects को एक run से आगे बने रहने देता है।
Class को serializable कैसे बनाऊँ?
Class को Serializable interface implement कराइए। इसमें लिखने को कोई methods नहीं — यह एक marker है जो Java को बताता है object serialize हो सकता है। इसके बिना, serialization NotSerializableException देता है।
transient keyword क्या है?
transient से चिह्नित field serialization के दौरान छोड़ दिया जाता है, तो उसकी value save नहीं होती। इसे उस data को इस्तेमाल कीजिए जो आप store नहीं करना चाहते, जैसे passwords या दोबारा गिने जा सकने वाली values।
Deserialization क्या है?
Deserialization save किए bytes पढ़कर मूल object को memory में फिर बनाता है। आप ObjectInputStream और readObject() इस्तेमाल करते हैं, फिर result को मूल type में वापस cast करते हैं।
serialVersionUID क्या है?
serialVersionUID serializable class का version number है। यह deserialize करते समय जाँचता है कि save किए bytes class version से मेल खाते हैं। इसे खुद set करना class थोड़ा बदलने पर errors बचाता है।
← Back to Java Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.
अगला topicSingleton Pattern आपकी प्रगति: 47 / 61 (77%) · Lesson 48 / 61 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें