This is the third week of Stack Overflow Weekly, due out on Thursday this week, only three content. Two Java, a Python. No public “transition code” for the day is more welcome attention.
DAY1. Enumeration objects == and equals the difference
We can use the operator == or equals method when comparing enumeration constants, such as:
First consider the first question: Why can judge == enumeration constants equal?
We all know that the object should be judged equally by the equals method, but == determine the object references are equivalent, enumeration constants can be used to determine equal == enumeration constant is because in addition to the definition of the object initialization, no other instances. For example: When defining constants APPLE has been initialized, and there will be no other APPLE enumeration constant running. According to the document JLS, Java instances to ensure that there is no outside enumeration constants defined by the following four points
Explicit examples of trying enumerated type is a compile-time errors, for example: new FRUITS ()
The clone method Enum class to ensure that never clone Enum constants to see the clone method can be seen directly throw a CloneNotSupportedException
Special treatment serialization mechanism to ensure that instances will not be deserialized and create duplicate
Reflection prohibited enumerated type instantiation
Enum constant because only one instance, can be determined directly == equal enumerated example. If we look at the source code you can find it equals == also be judged, and therefore we consider the second question: What is the difference between using == and equals that?
== not throw a NullPointerException, which equals will
== Type mismatch at compile time checking, but does not check equals
Therefore, the use of equals == compared to the following advantages:
More secure runtime
Safer compile time
DAY2. Copy the file with Python
We all know Python library is very powerful, usually we want to implement a basic function to find ready-made library. Today we introduce a Python standard library module – shutil, this module defines a method for file copying. Such as: copy a file, a function can be called directly
In addition to this function, the shutil module also defines a file copy function provides additional functionality, but the details are slightly different, the following comparison:
Which copy2 copy of a more comprehensive estimation performance is relatively low. The following two examples compare simple to distinguish between different functions.
1. whether the destination path is a directory, copyfile vs copy2
2. Can copy meta data, copyfile vs copy2
meta view the file information, the update time copyfile copied files are up to date, and copy2 copied files updated with the same source file.
Just to cite two examples, such other functions may attempt to self-interest. In addition, shutil module should have other functions can be more convenient for us to use. In short Python developers use efficiency can still be pricey, master Python really improve work efficiency.
DAY3. What is Java Bean
Java web development starting from the school, we know that there is this thing Java Bean. But for me, because I graduated after data have been bigger, say I really do not know the official definition of the Java Bean. Here we take a look at the requirements in line with what can be called Java Bean:
All the properties are private, provide getter and setter setting properties
There is a public constructor with no arguments
Java Bean in fact, is the norm. For our knowledge is often ask why Java Bean definition of such a specification. Control above three points, I’m thinking is this:
getter / setter methods to read and write interface to facilitate the framework of external exposure to property call. Meanwhile, with private attribute may be modified to improve security
Examples of previous articles read Hadoop framework reflected, public constructor without parameters can easily create a framework through reflection class instances
Since the web is typically required data transmission frame, the object is required to have the ability to serialize the deserialized
Today we This article is brief, just a brief introduction about the definition of Java Bean specification, and talked about some of my own thinking.
The above is the fourth week weekly Stack Overflow, and this week did not bring out the source file, copy the code numbers from the public picture, please forgive me. We hope this will be useful to you, follow-up will continue to be updated daily to see if the content is more welcome attention to the public number.
No public “transition code” to share more high-quality content