Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
cppg
toBeTopJavaer
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
toBeTopJavaer
/
docs
/
basics
/
java-basic
/
serialVersionUID.md
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
21 lines (10 loc) · 1.93 KB
master
Breadcrumbs
toBeTopJavaer
/
docs
/
basics
/
java-basic
/
serialVersionUID.md
Copy path
Top
File metadata and controls
Code
Blame
21 lines (10 loc) · 1.93 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
序列化是将对象的状态信息转换为可存储或传输的形式的过程
。
我们都知道
,
Java对象是保存在JVM的堆内存中的
,
也就是说
,
如果JVM堆不存在了
,
那么对象也就跟着消失了
。
⽽
序列化提供了
⼀
种
⽅
案
,
可以让你在即使JVM停机的情况下也能把对象保存下来的
⽅
案
。
就像我们平时
⽤
的U盘
⼀
样
。
把Java对象序列化成可存储或传输的形式
(
如
⼆
进制流
) , ⽐
如保存在
⽂
件中
。
这样
,
当再次需要这个对象的时候
,
从
⽂
件中读取出
⼆
进制流
,
再从
⼆
进制流中反序列化出对象
。
但是
,
虚拟机是否允许反序列化
,
不仅取决于类路径和功能代码是否
⼀
致
, ⼀
个
⾮
常重要的
⼀
点是两个类的序列化
ID
是否
⼀
致
,
即
`
serialVersionUID
`
要求
⼀
致
。
在进
⾏
反序列化时
,
JVM会把传来的字节流中的
`
serialVersionUID
`
与本地相应实体类的
`
serialVersionUID
`
进
⾏⽐
较
,
如果相同就认为是
⼀
致的
,
可以进
⾏
反序列化
,
否则就会出现序列化版本不
⼀
致的异常
,
即是
`
InvalidCastException
`。
这样做是为了保证安全
,
因为
⽂
件存储中的内容可能被篡改
。
当实现
`
java
.
io
.
Serializable
`
接口的类没有显式地定义
⼀
个
`
serialVersionUID
`
变量时候
,
Java序列化机制会根据编译的Class
⾃
动
⽣
成
⼀
个
`
serialVersionUID
`
作序列化版本
⽐
较
⽤,
这种情况下
,
如果Class
⽂
件没有发
⽣
变化
,
就算再编译多
次
,
serialVersionUID也不会变化的
。
但是
,
如果发
⽣
了变化
,
那么这个
⽂
件对应的
`
serialVersionUID
`
也就会发
⽣
变化
。
基于以上原理
,
如果我们
⼀
个类实现了Serializable接口
,
但是没有定义
`
serialVersionUID
`,
然后序列化
。
在序列化之后
,
由于某些原因
,
我们对该类做了变更
,
重新启动应
⽤
后
,
我们相对之前序列化过的对象进
⾏
反序列化的话就会报错
You can’t perform that action at this time.