Java lazy initialization small example

< span style="color:#808080;"> 1. Why use lazy initialization: @ h_ 419_ 1@ 
< span style="color:#808080;">  Because when the class is a basic type, it can be automatically initialized to 0, but the object reference will be initialized to null. The compiler does not simply create a default object for each reference, which is very useful, because it will increase a lot of overhead in many cases. If you want to initialize these references, you can do it in many places in the code, and "lazy initialization" is one of them@ H_ 419_ 1@ 
< span style="color:#808080;"> 2. The code is as follows: [after reading other people's code, I typed it again and deeply understood it] @ h_ 4191@ 
< span style="color:#808080;">/** @ H4191@ *  Created by cxh on 16/11/8. @ H4191@ */ @ H419_ 1@ class Simple{ String  s; < span style="color:#000080;"> public Simple(String si){ s=si;}< span style="color:#808080;">// Constructor, which internally assigns @ h to the construction variable_ 4191@ @ H4191@ public String toString(){ return  s;}< span style="color:#808080;">// Duplicate @ h4191 @ < span style = "color: #8080;" >@ H419_ 1@ public void setString(String sstr){ s=sstr;}< span style="color:#808080;">// Ordinary function @ h_ 419_ 1@}

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>