Skip to content

Commit 561f48d

Browse files
committed
owen1190 8/3
2 parents a914c65 + 2d7c9a0 commit 561f48d

9 files changed

+366
-171
lines changed

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ stackoverflow-Java-top-qa
5656
* [serialVersionUID 有什么作用?该如何使用?](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/what-is-a-serialversionuid-and-why-should-i-use-it.md)
5757
* [为什么Java的```Vector```类被认为是过时的或者废弃的](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/why-is-java-vector-class-considered-obsolete-or-deprecated.md)
5858
* [Java的foreach循环是如何工作的](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-does-the-java-for-each-loop-work.md)
59+
* [为什么相减这两个时间(1927年)会得到奇怪的结果](/contents/why-is-subtracting-these-two-times-in-1927-giving-a-strange-result.md)
60+
* [Java 中如何将 String 转换为 enum](/contents/lookup-enum-by-string-value.md)
61+
* [该什么时候使用 ThreadLocal变量,它是如何工作的](/contents/when-and-how-should-i-use-a-threadlocal-variable.md)
62+
* [servlets的运行原理](/contents/how-do-servlets-work-instantiation-shared-variables-and-multithreading.md)
63+
* [如何计算MD5值](/contents/how-can-i-generate-an-md5-hash.md)
5964

6065
> 编程技巧
6166
@@ -101,7 +106,6 @@ stackoverflow-Java-top-qa
101106
* [如何获取Android设备唯一ID](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/is-there-a-unique-android-device-id.md)
102107

103108
### 待翻译问题链接(还剩x问题)
104-
- [Why is subtracting these two times (in 1927) giving a strange result?](http://stackoverflow.com/questions/6841333/why-is-subtracting-these-two-times-in-1927-giving-a-strange-result)
105109
- [Proper use cases for Android UserManager.isUserAGoat()?](http://stackoverflow.com/questions/13375357/proper-use-cases-for-android-usermanager-isuseragoat)
106110
- [How can I create an executable jar with dependencies using Maven?](http://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven)
107111
- [Why is executing Java code in comments with certain Unicode characters allowed?](http://stackoverflow.com/questions/30727515/why-is-executing-java-code-in-comments-with-certain-unicode-characters-allowed)
@@ -118,24 +122,20 @@ stackoverflow-Java-top-qa
118122
- [How do you assert that a certain exception is thrown in JUnit 4 tests?](http://stackoverflow.com/questions/156503/how-do-you-assert-that-a-certain-exception-is-thrown-in-junit-4-tests)
119123
- [What exactly is Apache Camel?](http://stackoverflow.com/questions/8845186/what-exactly-is-apache-camel)
120124
- [How can I permanently have line numbers in IntelliJ?](http://stackoverflow.com/questions/13751/how-can-i-permanently-have-line-numbers-in-intellij)
121-
- [How do servlets work? Instantiation, shared variables and multithreading](http://stackoverflow.com/questions/3106452/how-do-servlets-work-instantiation-shared-variables-and-multithreading)
122125
- [Access restriction on class due to restriction on required library rt.jar?](http://stackoverflow.com/questions/860187/access-restriction-on-class-due-to-restriction-on-required-library-rt-jar)
123126
- [How do I discover memory usage of my application in Android?](http://stackoverflow.com/questions/2298208/how-do-i-discover-memory-usage-of-my-application-in-android)
124-
- [How can I generate an MD5 hash?](http://stackoverflow.com/questions/415953/how-can-i-generate-an-md5-hash)
125127
- [Uncatchable ChuckNorrisException](http://stackoverflow.com/questions/13883166/uncatchable-chucknorrisexception)
126128
- [Can I add jars to maven 2 build classpath without installing them?](http://stackoverflow.com/questions/364114/can-i-add-jars-to-maven-2-build-classpath-without-installing-them)
127129
- [Update Eclipse with Android development tools v. 23](http://stackoverflow.com/questions/24437564/update-eclipse-with-android-development-tools-v-23)
128130
- [Setting multiple jars in java classpath](http://stackoverflow.com/questions/219585/setting-multiple-jars-in-java-classpath)
129131
- [Iterating through a list, avoiding ConcurrentModificationException when removing in loop](http://stackoverflow.com/questions/223918/iterating-through-a-list-avoiding-concurrentmodificationexception-when-removing)
130132
- [What is the Java equivalent for LINQ?](http://stackoverflow.com/questions/1217228/what-is-the-java-equivalent-for-linq)
131133
- [Hibernate hbm2ddl.auto possible values and what they do?](http://stackoverflow.com/questions/438146/hibernate-hbm2ddl-auto-possible-values-and-what-they-do)
132-
- [How to round a number to n decimal places in Java](http://stackoverflow.com/questions/153724/how-to-round-a-number-to-n-decimal-places-in-java)
133134
- [How can I pad an integers with zeros on the left?](http://stackoverflow.com/questions/473282/how-can-i-pad-an-integers-with-zeros-on-the-left)
134135
- [Sort ArrayList of custom Objects by property](http://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property)
135136
- [What's the difference between @Component, @Repository & @Service annotations in Spring?](http://stackoverflow.com/questions/6827752/whats-the-difference-between-component-repository-service-annotations-in)
136137
- [Efficiency of Java “Double Brace Initialization”?](http://stackoverflow.com/questions/924285/efficiency-of-java-double-brace-initialization)
137138
- [Why does this go into an infinite loop?](http://stackoverflow.com/questions/3831341/why-does-this-go-into-an-infinite-loop)
138-
- [When and how should I use a ThreadLocal variable?](http://stackoverflow.com/questions/817856/when-and-how-should-i-use-a-threadlocal-variable)
139139
- [decompiling DEX into Java sourcecode](http://stackoverflow.com/questions/1249973/decompiling-dex-into-java-sourcecode)
140140
- [How to create a generic array in Java?](http://stackoverflow.com/questions/529085/how-to-create-a-generic-array-in-java)
141141
- [Why does Math.round(0.49999999999999994) return 1](http://stackoverflow.com/questions/9902968/why-does-math-round0-49999999999999994-return-1)

SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
\# Summary* [前言](README.md)
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,80 @@
1-
## 比较java枚举成员使用equal还是==
2-
3-
### 问题
4-
我知道Java枚举会被编译成一个包含私有构造参数和一堆静态方法的类,当去比较两个枚举的时候,总是使用equals()方法,例如:
5-
```java
6-
public useEnums(SomeEnum a)
7-
{
8-
if(a.equals(SomeEnum.SOME_ENUM_VALUE))
9-
{
10-
...
11-
}
12-
...
13-
}
14-
```
15-
除此之外,我也可以使用 == 替代equals() 方法
16-
```java
17-
public useEnums2(SomeEnum a)
18-
{
19-
if(a == SomeEnum.SOME_ENUM_VALUE)
20-
{
21-
...
22-
}
23-
...
24-
}
25-
```
26-
我有5年以上的java编程经验,并且我想我也懂得 == 和 equals() 之间的区别,但是我仍然觉得很困惑,哪一个操作符才是我该使用的。
27-
28-
### 答案
29-
30-
二者皆对,如果你看过枚举的源码,你会发现在源码中,equals也仅仅非常简单的 == 。
31-
我使用 == ,因为无论如何,这个左值是可以为 null的
32-
33-
34-
译者补充 java.lang.Enum 中Equals 代码:
35-
```java
36-
public final boolean equals(Object other) {
37-
return this==other;
38-
}
39-
```
40-
41-
42-
### 额外答案
43-
#### 能在枚举中使用 == 进行判断?
44-
答案是肯定的,因为枚举有着严格的实例化控制,所以你可以用 == 去做比较符,这个用法,在官方文档中也有明确的说明。
45-
46-
>JLS 8.9 Enums
47-
>An enum type has no instances other than those defined by its enum constants.
48-
>It is a compile-time error to attempt to explicitly instantiate an enum type. The final clone method in Enum >ensures that enum constants can never be cloned, and the special treatment by the serialization mechanism ensures >that duplicate instances are never created as a result of deserialization. Reflective instantiation of enum types >is prohibited. Together, these four things ensure that no instances of an enum type exist beyond those defined by >the enum constants.
49-
>Because there is only one instance of each enum constant, it is permissible to use the == operator in place of the >equals method when comparing two object references if it is known that at least one of them refers to an enum ?>constant. (The equals method in Enum is a final method that merely invokes super.equals on its argument and ?>returns the result, thus performing an identity comparison.)
50-
51-
#### 什么时候 == 和 equals 不一样?
52-
As a reminder, it needs to be said that generally, == is NOT a viable alternative to equals. When it is, however (such as with enum), there are two important differences to consider:
53-
通常来说 == 不是一个 equals的一个备选方案,无论如何有2个重要的不同处需要考虑:
54-
55-
##### == 不会抛出 NullPointerException
56-
```java
57-
enum Color { BLACK, WHITE };
58-
59-
Color nothing = null;
60-
if (nothing == Color.BLACK); // runs fine
61-
if (nothing.equals(Color.BLACK)); // throws NullPointerException
62-
```
63-
##### == 在编译期检测类型兼容性
64-
```java
65-
enum Color { BLACK, WHITE };
66-
enum Chiral { LEFT, RIGHT };
67-
68-
if (Color.BLACK.equals(Chiral.LEFT)); // compiles fine
69-
if (Color.BLACK == Chiral.LEFT); // DOESN'T COMPILE!!! Incompatible types!
70-
```
71-
72-
#### 什么时候使用 == ?
73-
Bloch specifically mentions that immutable classes that have proper control over their instances can guarantee to their clients that == is usable. enum is specifically mentioned to exemplify.
74-
具体来说,那些提供恰当实例控制的不可变类能够保证 == 是可用的,枚举刚好符合这个条件。
75-
76-
> Item 1: Consider static factory methods instead of constructors
77-
[...] it allows an immutable class to make the guarantee that no two equal instances exist: a.equals(b) if and only if a==b. If a class makes this guarantee, then its clients can use the == operator instead of the equals(Object) method, which may result in improved performance. Enum types provide this guarantee.
78-
79-
总而言之,在枚举比较上使用 == , 因为:
80-
1. 能正常工作
81-
2. 更快
82-
3. 运行时是安全的
83-
4. 编译期也是安全的
84-
85-
stackoverlfow链接:http://stackoverflow.com/questions/1750435/comparing-java-enum-members-or-equals
1+
# 比较java中枚举成员是用“==”还是equals()
2+
3+
## 问题
4+
java枚举被编译成带有私有构造器和一堆public的静态成员的类。当比较枚举中的两个成员时,经常使用.equals()方法,例如
5+
public useEnums(SomeEnum a)
6+
{
7+
if(a.equals(SomeEnum.SOME_ENUM_VALUE))
8+
{
9+
...
10+
}
11+
...
12+
}
13+
然而,偶然间使用“==”代替equals方法
14+
public useEnums2(SomeEnum a)
15+
{
16+
if(a == SomeEnum.SOME_ENUM_VALUE)
17+
{
18+
...
19+
}
20+
...
21+
}
22+
应该使用哪个呢?
23+
## 解答
24+
### 回答1
25+
技术上来说,都是对的,如果你看了equals的源码,它简单地遵从“==” ,然而我一般使用“==” 因为它对于空指针,比较安全
26+
### 回答2
27+
#### 能在枚举时使用用“==”吗?
28+
29+
可以,枚举值有小型实例控制,允许你用“==”去比较实例,在文档中有说明:
30+
31+
JLS 8.9 枚举
32+
一个枚举类型除了定义的那些枚举常量外没有其他实例了。
33+
试图明确地说明一种枚举类型是会导致编译期异常。在枚举中final clone方法确保枚举常量从不会被克隆,而且序列化机制会确保从不会因为反序列化而创造复制的实例。枚举类型的反射实例化也是被禁止的。总之,以上内容确保了除了定义的枚举常量之外,没有枚举类型实例。
34+
35+
因为每个枚举常量只有一个实例,所以如果在比较两个参考值,至少有一个涉及到枚举常量时,允许使用“==”代替equals()。(equals()方法在枚举类中是一个final方法,在参数和返回结果时,很少调用父类的equals()方法,因此是一种恒等的比较。)
36+
37+
这足够强力地支持Josh的建议,如果你坚持使用单例模式,最好的方法是用枚举类型强化单例属性(见Effective Java第二版中的第三条:用私有构造器或者枚举类型强化Singleton属性,或者单例模式的线程安全
38+
>http://stackoverflow.com/questions/2912281/thread-safety-in-singleton/
39+
40+
#### “==”和equals的区别
41+
通常情况下,==并不是可以替换equals,然而在枚举中是可以的。它们之间有两个重要的不同:
42+
43+
“==”从不会抛出空指针异常
44+
45+
46+
enum Color { BLACK, WHITE };
47+
48+
Color nothing = null;
49+
50+
if (nothing == Color.BLACK); // 正常运行
51+
52+
if (nothing.equals(Color.BLACK)); // 抛出空指
53+
针异常
54+
55+
在编译期,"=="会检查其类型的兼容性
56+
57+
enum Color { BLACK, WHITE };
58+
enum Chiral { LEFT, RIGHT };
59+
60+
if (Color.BLACK.equals(Chiral.LEFT)); // 编译正常
61+
if (Color.BLACK == Chiral.LEFT); // 无法编译,类型不兼容
62+
63+
#### 在适用时,“==”可以被使用吗
64+
65+
Bloch(effective java的作者)明确指出不可变类可以控制它们实例保证客户端“==”是可用的。枚举就被明确地证明了
66+
67+
考虑静态工厂方法代替构造器
68+
它使得不可变的类可以确保不会存在两个相等的实例,即当且仅当a==b的时候才有a.equals(b)为true。如果类保证了这一点,它的客户端可以使用“==”操作符来代替equals(Object)方法,这样可以提升性能。枚举类型保证了这一点
69+
70+
总之,在枚举中使用“==”优势:
71+
72+
- 能运行
73+
- 更快
74+
- 在运行期更安全
75+
- 在编译期更安全
76+
77+
备注:强有力的反击了那些认为foo.equals(bar)比foo==bar更有可读性的人们。
78+
79+
原文地址:
80+
> http://stackoverflow.com/questions/1750435/comparing-java-enum-members-or-equals
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
##如何计算MD5值
2+
3+
###问题
4+
Java中有没有方法可以计算一个String的MD5值?
5+
6+
7+
###回答
8+
你可以用 ```MessageDigest``` 的MD5实例来计算String的MD5值。
9+
10+
使用 ```MessageDigest``` 和 String 时,一定要显式声明你的数据编码类型。如果你使用无参的 ```String.getBytes()``` , 它会以当前平台的默认编码来转换数据。不同平台的默认编码可能是不同的,这可能会导致你的数据不一致。
11+
12+
``` java
13+
import java.security.*;
14+
15+
..
16+
17+
byte[] bytesOfMessage = yourString.getBytes("UTF-8");
18+
MessageDigest md = MessageDigest.getInstance("MD5");
19+
byte[] thedigest = md.digest(bytesOfMessage);
20+
```
21+
22+
如果你的要计算的数据量很大,你可以循环使用 ```.update(byte[])``` 方法来加载数据。加载完毕后用 ```.digest()``` 方法来得到计算出的MD5值。
23+
24+
stackoverflow链接
25+
http://stackoverflow.com/questions/415953/how-can-i-generate-an-md5-hash

0 commit comments

Comments
 (0)