Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 984c94b

Browse filesBrowse files
committed
更新已翻译问题
1 parent 14b2cc9 commit 984c94b
Copy full SHA for 984c94b

File tree

Expand file treeCollapse file tree

3 files changed

+89
-140
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+89
-140
lines changed

‎README.md

Copy file name to clipboardExpand all lines: README.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ stackoverflow-Java-top-qa
6161
* [该什么时候使用 ThreadLocal变量,它是如何工作的](/contents/when-and-how-should-i-use-a-threadlocal-variable.md)
6262
* [servlets的运行原理](/contents/how-do-servlets-work-instantiation-shared-variables-and-multithreading.md)
6363
* [如何计算MD5值](/contents/how-can-i-generate-an-md5-hash.md)
64+
* [Java中软引用和弱引用的区别](/what-is-the-difference-between-a-soft-reference-and-a-weak-reference-in-java.md)
6465

6566
> 编程技巧
6667
@@ -140,7 +141,6 @@ stackoverflow-Java-top-qa
140141
- [How to create a generic array in Java?](http://stackoverflow.com/questions/529085/how-to-create-a-generic-array-in-java)
141142
- [Why does Math.round(0.49999999999999994) return 1](http://stackoverflow.com/questions/9902968/why-does-math-round0-49999999999999994-return-1)
142143
- [Eclipse: Set maximum line length for auto formatting?](http://stackoverflow.com/questions/3697287/eclipse-set-maximum-line-length-for-auto-formatting)
143-
- [What is the difference between a soft reference and a weak reference in Java?](http://stackoverflow.com/questions/299659/what-is-the-difference-between-a-soft-reference-and-a-weak-reference-in-java)
144144
- [What is the equivalent of the C++ Pair<L,R> in Java?](http://stackoverflow.com/questions/156275/what-is-the-equivalent-of-the-c-pairl-r-in-java)
145145
- [What is the difference between JSF, Servlet and JSP?](http://stackoverflow.com/questions/2095397/what-is-the-difference-between-jsf-servlet-and-jsp)
146146
- [How do I “decompile” Java class files?](http://stackoverflow.com/questions/272535/how-do-i-decompile-java-class-files)
+85-80Lines changed: 85 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,85 @@
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
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

‎contents/lookup-enum-by-string-value.md

Copy file name to clipboardExpand all lines: contents/lookup-enum-by-string-value.md
+3-59Lines changed: 3 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,9 @@
1-
<<<<<<< HEAD
2-
#通过String值查找enum中常量
3-
## 问题
4-
假设有一个枚举值
5-
public enum Blah
6-
{
7-
A,B,C,D
8-
}
9-
想通过一个String类型,找到所需要的枚举值。
10-
例如“A”->Blah.A
11-
是使用Enum.valueOf()方法吗?该如何使用
12-
## 回答
13-
Blah.valueOf("A")会得到Blah.A
14-
虽然api文档确实有静态方法valueOf()和values(),但是二者在编译期时才出现,而且在没出现在源程序中。
15-
例如可以采用Dialog.ModalityType显示了两种方法来处理这种情况。
16-
备注:Blah.valueOf("A")的方法是区分大小写,且不能含有空格。
17-
18-
如果String值与enum中不相同的查找方法:
19-
20-
public enum Blah
21-
{
22-
A("text1"),
23-
B("text2"),
24-
C("text3"),
25-
D("text4");
26-
private String text;
27-
Blah(String text)
28-
{
29-
this.text = text;
30-
}
31-
public String getText()
32-
{
33-
return this.text;
34-
}
35-
36-
public static Blah fromString(String text)
37-
{
38-
if (text != null)
39-
{
40-
for (Blah b : Blah.values())
41-
{
42-
if (text.equalsIgnoreCase(b.text))
43-
{
44-
return b;
45-
}
46-
}
47-
}
48-
return null;
49-
}
50-
}
51-
52-
备注:throw new IllegalArgumentException("No constant with text"+text+"found")会比直接抛出null更好
53-
54-
原文链接:
55-
> http://stackoverflow.com/questions/604424/lookup-enum-by-string-value#
1+
Java 中如何将 String 转换为 enum
562
=======
57-
## Java 中如何将 String 转换为 enum
583

594
###问题
60-
我有一个 enum 类
5+
6+
###我有一个 enum 类
617

628
``` java
639
public enum Blah {
@@ -159,5 +105,3 @@ _其他的答案都大同小异,感兴趣的可以看原帖_
159105
stackoverflow链接
160106
http://stackoverflow.com/questions/604424/lookup-enum-by-string-value
161107
_译者:[MagicWolf](https://github.com/DaiDongLiang)_
162-
163-
>>>>>>> upstream/master

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.