Serializable trong java

impliment cái serializable để làm gì vậy ạ

Hehe,

Câu hỏi này hay đấy.
Serializable là một tag interface, một dạng interface đặc biệt không có bất cứ method nào. Nhiệm vụ của interface kiểu này là để “tag” - đánh dấu 1 chức năng cho 1 object.

Chức năng mà Serializable được dùng để tag được đề cập trong Javadoc của interface Serializable:

Serializability of a class is enabled by the class implementing the java.io.Serializable interface. Classes that do not implement this interface will not have any of their state serialized or deserialized. All subtypes of a serializable class are themselves serializable. The serialization interface has no methods or fields and serves only to identify the semantics of being serializable.

Serializable là tính năng cho phép cậu convert object sang dạng byte stream. Dạng này thường nhỏ và nhẹ, cậu có thể đưa object đi muôn nơi trong network, hay đưa nó vào trong file/database. Khi cậu lấy ra, cậu chỉ cần deserialize byte stream thành object là được.
Serializable interface dùng để enable tính năng này cho object mà cậu muốn, thường là các object chứa dữ liệu.
Cậu có thể đọc thêm article này để biết thêm.

Hope it helps!

5 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?