Date: 2015年9月16日 18:59:21 +0800
Subject: [PATCH 004/195] modified README.md to add links
---
README.md | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/README.md b/README.md
index 3a6928c..72c31d1 100644
--- a/README.md
+++ b/README.md
@@ -51,6 +51,10 @@ stackoverflow-Java-top-qa
* [如何测试 private 方法,变量或者内部类](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/How_to_test_a_class_that_has_private_methods,_fields_or_inner_classes.md)
+> Android
+
+* [在Android里面下载文件,并在ProgressDialog显示进度](https://github.com/troyliu0105/stackoverflow-java-top-qa/blob/master/contents/Download_a_file_with_Android_and_showing_the_progress_in_a_ProgressDialog.md)
+
### 待翻译问题链接(还剩x问题)
- [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)
- [Proper use cases for Android UserManager.isUserAGoat()?](http://stackoverflow.com/questions/13375357/proper-use-cases-for-android-usermanager-isuseragoat)
From a439dbc5dd263a29eb32bb503156bce8f90c715a Mon Sep 17 00:00:00 2001
From: Troy Liu
Date: 2015年9月16日 18:59:21 +0800
Subject: [PATCH 005/195] modified README.md to add links
---
README.md | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/README.md b/README.md
index 3a6928c..4ec08b0 100644
--- a/README.md
+++ b/README.md
@@ -51,6 +51,10 @@ stackoverflow-Java-top-qa
* [如何测试 private 方法,变量或者内部类](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/How_to_test_a_class_that_has_private_methods,_fields_or_inner_classes.md)
+> Android
+
+* [在Android里面下载文件,并在ProgressDialog显示进度](https://github.com/troyliu0105/stackoverflow-java-top-qa/blob/master/contents/Download_a_file_with_Android_and_showing_the_progress_in_a_ProgressDialog.md)
+
### 待翻译问题链接(还剩x问题)
- [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)
- [Proper use cases for Android UserManager.isUserAGoat()?](http://stackoverflow.com/questions/13375357/proper-use-cases-for-android-usermanager-isuseragoat)
@@ -90,7 +94,6 @@ stackoverflow-Java-top-qa
- [How can I Initialize a static Map?](http://stackoverflow.com/questions/507602/how-can-i-initialize-a-static-map)
- [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)
- [How can I generate an MD5 hash?](http://stackoverflow.com/questions/415953/how-can-i-generate-an-md5-hash)
-- [Download a file with Android, and showing the progress in a ProgressDialog](http://stackoverflow.com/questions/3028306/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog)
- [Uncatchable ChuckNorrisException](http://stackoverflow.com/questions/13883166/uncatchable-chucknorrisexception)
- [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)
- [Update Eclipse with Android development tools v. 23](http://stackoverflow.com/questions/24437564/update-eclipse-with-android-development-tools-v-23)
From 7b1c17ed82efe5f6e3fc2fc2d92bb501e7dfeb7c Mon Sep 17 00:00:00 2001
From: zhupeiquan
Date: 2015年9月16日 21:05:12 +0800
Subject: [PATCH 006/195] =?UTF-8?q?Examples=5Fof=5FGoF=5FDesign=5FPatterns?=
=?UTF-8?q?=5Fin=5FJava\'s=5Fcore=5Flibraries.md=20=E5=9F=BA=E6=9C=AC?=
=?UTF-8?q?=E7=BF=BB=E8=AF=91=E5=AE=8C=E6=88=90?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...esign_Patterns_in_Java's_core_libraries.md | 131 +++++++++++++++++-
1 file changed, 125 insertions(+), 6 deletions(-)
diff --git a/contents/Examples_of_GoF_Design_Patterns_in_Java's_core_libraries.md b/contents/Examples_of_GoF_Design_Patterns_in_Java's_core_libraries.md
index 85585ed..1f24148 100644
--- a/contents/Examples_of_GoF_Design_Patterns_in_Java's_core_libraries.md
+++ b/contents/Examples_of_GoF_Design_Patterns_in_Java's_core_libraries.md
@@ -2,22 +2,22 @@
从 [维基百科](https://en.wikipedia.org/wiki/Software_design_pattern#Classification_and_list) 中,可以让你对大部分设计模式有一个概览,而且它页指出了那些设计模式是 GoF 中规范.下面列出可以从 JavaSE 和 JavaEE API 中找到的设计模式:
-## 创建型模式
+## [创建型模式](https://en.wikipedia.org/wiki/Creational_pattern)
-### 抽象工厂
+### [抽象工厂](http://en.wikipedia.org/wiki/Abstract_factory_pattern)
- [javax.xml.parsers.DocumentBuilderFactory#newInstance()](http://docs.oracle.com/javase/6/docs/api/javax/xml/parsers/DocumentBuilderFactory.html#newInstance%28%29)
- [javax.xml.transform.TransformerFactory#newInstance()](http://docs.oracle.com/javase/6/docs/api/javax/xml/transform/TransformerFactory.html#newInstance%28%29)
- [javax.xml.xpath.XPathFactory#newInstance()](http://docs.oracle.com/javase/6/docs/api/javax/xml/xpath/XPathFactory.html#newInstance%28%29)
-### 建造者模式
+### [建造者模式](http://en.wikipedia.org/wiki/Builder_pattern)
- [java.lang.StringBuilder#append()](http://docs.oracle.com/javase/6/docs/api/java/lang/StringBuilder.html#append%28boolean%29)(非同步)
- [java.lang.StringBuffer#append()](http://docs.oracle.com/javase/6/docs/api/java/lang/StringBuffer.html#append%28boolean%29)(同步)
- [java.nio.ByteBuffer#put()](http://docs.oracle.com/javase/6/docs/api/java/nio/ByteBuffer.html#put%28byte%29)(类似的还有, [CharBuffer](http://docs.oracle.com/javase/6/docs/api/java/nio/CharBuffer.html#put%28char%29), [ShortBuffer](http://docs.oracle.com/javase/6/docs/api/java/nio/ShortBuffer.html#put%28short%29), [IntBuffer](http://docs.oracle.com/javase/6/docs/api/java/nio/IntBuffer.html#put%28int%29), [LongBuffer](http://docs.oracle.com/javase/6/docs/api/java/nio/LongBuffer.html#put%28long%29), [FloatBuffer](http://docs.oracle.com/javase/6/docs/api/java/nio/FloatBuffer.html#put%28float%29) 和 [DoubleBuffer](http://docs.oracle.com/javase/6/docs/api/java/nio/DoubleBuffer.html#put%28double%29))
- [javax.swing.GroupLayout.Group#addComponent()](http://docs.oracle.com/javase/6/docs/api/javax/swing/GroupLayout.Group.html#addComponent%28java.awt.Component%29)
-### 工厂模式
+### [工厂模式](http://en.wikipedia.org/wiki/Factory_method_pattern)
- [java.util.Calendar#getInstance()](http://docs.oracle.com/javase/6/docs/api/java/util/Calendar.html#getInstance%28%29)
- [java.util.ResourceBundle#getBundle()](http://docs.oracle.com/javase/6/docs/api/java/util/ResourceBundle.html#getBundle%28java.lang.String%29)
@@ -25,6 +25,125 @@
- [java.nio.charset.Charset#forName()](http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html#forName%28java.lang.String%29)
- [java.net.URLStreamHandlerFactory#createURLStreamHandler(String)](http://docs.oracle.com/javase/6/docs/api/java/net/URLStreamHandlerFactory.html)
-### 原型模式
+### [原型模式](http://en.wikipedia.org/wiki/Prototype_pattern)
-- [java.lang.Object#clone()](http://docs.oracle.com/javase/6/docs/api/java/lang/Object.html#clone%28%29)(类需要实现 [java.lang.Cloneable](http://docs.oracle.com/javase/6/docs/api/java/lang/Cloneable.html) 接口)
\ No newline at end of file
+- [java.lang.Object#clone()](http://docs.oracle.com/javase/6/docs/api/java/lang/Object.html#clone%28%29)(类需要实现 [java.lang.Cloneable](http://docs.oracle.com/javase/6/docs/api/java/lang/Cloneable.html) 接口)
+
+### [单例模式](http://en.wikipedia.org/wiki/Singleton_pattern)
+
+- [java.lang.Runtime#getRuntime()](http://docs.oracle.com/javase/6/docs/api/java/lang/Runtime.html#getRuntime%28%29)
+- [java.awt.Desktop#getDesktop()](http://docs.oracle.com/javase/6/docs/api/java/awt/Desktop.html#getDesktop%28%29)
+- [java.lang.System#getSecurityManager()](http://docs.oracle.com/javase/6/docs/api/java/lang/System.html#getSecurityManager%28%29)
+
+## [结构型模式](http://en.wikipedia.org/wiki/Structural_pattern)
+
+### [适配器模式](http://en.wikipedia.org/wiki/Adapter_pattern)
+
+- [java.util.Arrays#asList()](http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html#asList%28T...%29)
+- [java.io.InputStreamReader(InputStream) ](http://docs.oracle.com/javase/6/docs/api/java/io/InputStreamReader.html#InputStreamReader%28java.io.InputStream%29)(返回 Reader)
+- [java.io.OutputStreamWriter(OutputStream)](http://docs.oracle.com/javase/6/docs/api/java/io/OutputStreamWriter.html#OutputStreamWriter%28java.io.OutputStream%29)(返回 Writer)
+- [javax.xml.bind.annotation.adapters.XmlAdapter#marshal()](http://docs.oracle.com/javase/6/docs/api/javax/xml/bind/annotation/adapters/XmlAdapter.html#marshal%28BoundType%29) 和 [#unmarshal()](http://docs.oracle.com/javase/6/docs/api/javax/xml/bind/annotation/adapters/XmlAdapter.html#unmarshal%28ValueType%29)
+
+### [桥模式](http://en.wikipedia.org/wiki/Bridge_pattern)
+暂时没有发现
+
+### [合成模式](http://en.wikipedia.org/wiki/Composite_pattern)
+
+- [java.awt.Container#add(Component)](http://docs.oracle.com/javase/6/docs/api/java/awt/Container.html#add%28java.awt.Component%29)(Swing 中几乎所有类都使用)
+- [javax.faces.component.UIComponent#getChildren()](http://docs.oracle.com/javaee/6/api/javax/faces/component/UIComponent.html#getChildren%28%29)(JSF UI 中几乎所有类都使用)
+
+### [装饰模式](http://en.wikipedia.org/wiki/Decorator_pattern)
+
+- [java.io.InputStream](http://docs.oracle.com/javase/6/docs/api/java/io/InputStream.html),[OutputStream](http://docs.oracle.com/javase/6/docs/api/java/io/OutputStream.html),[Reader](http://docs.oracle.com/javase/6/docs/api/java/io/Reader.html) 和 [Writer](http://docs.oracle.com/javase/6/docs/api/java/io/Writer.html) 的所有资料都有一个使用 InputStream,OutputStream,Reader,Writer 的构造器
+- [java.util.Collections](http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html) 中的 [checkedXXX()](http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#checkedCollection%28java.util.Collection,%20java.lang.Class%29), [synchronizedXXX()](http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#synchronizedCollection%28java.util.Collection%29) 和 [unmodifiableXXX()](http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#unmodifiableCollection%28java.util.Collection%29) 方法
+- [javax.servlet.http.HttpServletRequestWrapper](http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequestWrapper.html) 和 [HttpServletResponseWrapper](http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletResponseWrapper.html)
+
+### [门面模式](http://en.wikipedia.org/wiki/Facade_pattern)
+[javax.faces.context.FacesContext](http://docs.oracle.com/javaee/6/api/javax/faces/context/FacesContext.html),其内部使用了 [LifeCycle](http://docs.oracle.com/javaee/6/api/javax/faces/lifecycle/Lifecycle.html), [ViewHandler](http://docs.oracle.com/javaee/6/api/javax/faces/application/ViewHandler.html), [NavigationHandler](http://docs.oracle.com/javaee/6/api/javax/faces/application/NavigationHandler.html) 等接口或抽象类,没有这一个门面类,终端就需要考虑如何去使用接口或抽象类(实际上不需要,因为门面类通过反射完成了)
+[javax.faces.context.ExternalContext](http://docs.oracle.com/javaee/6/api/javax/faces/context/ExternalContext.html), 其内部使用了 [ServletContext](http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContext.html), [HttpSession](http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpSession.html), [HttpServletRequest](http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html), [HttpServletResponse](http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletResponse.html) 等
+
+### [享元模式](http://en.wikipedia.org/wiki/Flyweight_pattern)
+
+- [java.lang.Integer#valueOf(int)](http://docs.oracle.com/javase/6/docs/api/java/lang/Integer.html#valueOf%28int%29),类似得还有 [Boolean](http://docs.oracle.com/javase/6/docs/api/java/lang/Boolean.html#valueOf%28boolean%29), [Byte](http://docs.oracle.com/javase/6/docs/api/java/lang/Byte.html#valueOf%28byte%29), [Character](http://docs.oracle.com/javase/6/docs/api/java/lang/Character.html#valueOf%28char%29), [Short](http://docs.oracle.com/javase/6/docs/api/java/lang/Short.html#valueOf%28short%29) 和 [Long](http://docs.oracle.com/javase/6/docs/api/java/lang/Long.html#valueOf%28long%29)
+
+### [代理模式](http://en.wikipedia.org/wiki/Proxy_pattern)
+
+- [java.lang.reflect.Proxy](http://docs.oracle.com/javase/6/docs/api/java/lang/reflect/Proxy.html)
+- [java.rmi.*](http://docs.oracle.com/javase/6/docs/api/java/rmi/package-summary.html)(所有 api)
+
+## [表现型模式](http://en.wikipedia.org/wiki/Behavioral_pattern)
+
+### [责任链模式](http://en.wikipedia.org/wiki/Chain_of_responsibility_pattern)
+
+- [java.util.logging.Logger#log()](http://docs.oracle.com/javase/6/docs/api/java/util/logging/Logger.html#log%28java.util.logging.Level,%20java.lang.String%29)
+- [javax.servlet.Filter#doFilter()](http://docs.oracle.com/javaee/6/api/javax/servlet/Filter.html#doFilter%28javax.servlet.ServletRequest,%20javax.servlet.ServletResponse,%20javax.servlet.FilterChain%29)
+
+### [命令模式](http://en.wikipedia.org/wiki/Command_pattern)
+
+- [所有 java.lang.Runnable 的实现](http://docs.oracle.com/javase/6/docs/api/java/lang/Runnable.html)
+- [所有 javax.swing.Action 的实现](http://docs.oracle.com/javase/6/docs/api/javax/swing/Action.html)
+
+### [解释器模式](http://en.wikipedia.org/wiki/Interpreter_pattern)
+
+- [java.util.Pattern](http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html)
+- [java.text.Normalizer](http://docs.oracle.com/javase/6/docs/api/java/text/Normalizer.html)
+- [所有 java.text.Format 的子类](http://docs.oracle.com/javase/6/docs/api/java/text/Format.html)
+- [所有 javax.el.ELResolver 的子类](http://docs.oracle.com/javaee/6/api/javax/el/ELResolver.html)
+
+### [迭代模式](http://en.wikipedia.org/wiki/Iterator_pattern)
+
+- [所有 java.util.Iterator 的实现](http://docs.oracle.com/javase/6/docs/api/java/util/Iterator.html)(因此也包含了所有 [java.util.Scanner](http://docs.oracle.com/javase/6/docs/api/java/util/Scanner.html) 的子类)
+- [所有 java.util.Enumeration 的实现](http://docs.oracle.com/javase/6/docs/api/java/util/Enumeration.html)
+
+
+### [中介模式](http://stackoverflow.com/questions/1673841/examples-of-gof-design-patterns-in-javas-core-libraries)
+
+- [java.util.Timer 中的所有 scheduleXXX() 方法)](http://docs.oracle.com/javase/6/docs/api/java/util/Timer.html)
+- [java.util.concurrent.Executor#execute()](http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/Executor.html#execute%28java.lang.Runnable%29)
+- [java.util.concurrent.ExecutorService 中的 invokeXXX() 和 submit() 方法](http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ExecutorService.html)
+- [java.util.concurrent.ScheduledExecutorService 中的所有 scheduleXXX() 方法](http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ScheduledExecutorService.html)
+- [java.lang.reflect.Method#invoke()](http://docs.oracle.com/javase/6/docs/api/java/lang/reflect/Method.html#invoke%28java.lang.Object,%20java.lang.Object...%29)
+
+### [备忘录模式](http://en.wikipedia.org/wiki/Memento_pattern)
+
+[java.util.Date](http://docs.oracle.com/javase/6/docs/api/java/util/Date.html)(setXXX 方法更新的就是其内部的 Date 的值)
+[java.io.Serializable 的所有实现](http://docs.oracle.com/javase/6/docs/api/java/io/Serializable.html)
+[javax.faces.component.StateHolder 的所有实现](http://docs.oracle.com/javaee/6/api/javax/faces/component/StateHolder.html)
+
+### [观察者模式(订阅模式)](http://en.wikipedia.org/wiki/Observer_pattern)
+
+[java.util.Observer](http://docs.oracle.com/javase/6/docs/api/java/util/Observer.html)/[java.util.Observable](http://docs.oracle.com/javase/6/docs/api/java/util/Observable.html)(实际应用中,很少会用到)
+[java.util.EventListener 的所有实现](http://docs.oracle.com/javase/6/docs/api/java/util/EventListener.html)(几乎包含了所有 Swing 中使用到的类)
+[javax.servlet.http.HttpSessionBindingListener](http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpSessionBindingListener.html)
+[javax.servlet.http.HttpSessionAttributeListener](http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpSessionAttributeListener.html)
+[javax.faces.event.PhaseListener](http://docs.oracle.com/javaee/6/api/javax/faces/event/PhaseListener.html)
+
+### [状态模式](http://en.wikipedia.org/wiki/State_pattern)
+
+[javax.faces.lifecycle.LifeCycle#execute()](http://docs.oracle.com/javaee/6/api/javax/faces/lifecycle/Lifecycle.html#execute%28javax.faces.context.FacesContext%29)(由FacesServlet控制,行为是依赖于当前JSF生命周期阶段(状态))
+
+### [策略模式](http://en.wikipedia.org/wiki/Strategy_pattern)
+
+[java.util.Comparator#compare()](http://docs.oracle.com/javase/6/docs/api/java/util/Comparator.html#compare%28T,%20T%29), 在 Collections#sort() 中会使用到.
+[javax.servlet.http.HttpServlet](http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServlet.html),service() 和 所有 doXXX() 方法都以 HttpServletRequest 和 HttpServletResponse 作为参数,所有方法的实现都需要显式处理这两个参数(而不是持有这个变量。)
+[javax.servlet.Filter#doFilter()](http://docs.oracle.com/javaee/6/api/javax/servlet/Filter.html#doFilter%28javax.servlet.ServletRequest,%20javax.servlet.ServletResponse,%20javax.servlet.FilterChain%29)
+
+### [模板模式](http://en.wikipedia.org/wiki/Template_method_pattern)
+[java.io.InputStream](http://docs.oracle.com/javase/6/docs/api/java/io/InputStream.html), [java.io.OutputStream](http://docs.oracle.com/javase/6/docs/api/java/io/OutputStream.html), [java.io.Reader](http://docs.oracle.com/javase/6/docs/api/java/io/Reader.html) 和 [java.io.Writer](http://docs.oracle.com/javase/6/docs/api/java/io/Writer.html) 的所有 非抽象 方法。
+[java.util.AbstractList](http://docs.oracle.com/javase/6/docs/api/java/util/AbstractList.html), [java.util.AbstractSet](http://docs.oracle.com/javase/6/docs/api/java/util/AbstractSet.html) 和 [java.util.AbstractMap](http://docs.oracle.com/javase/6/docs/api/java/util/AbstractMap.html) 的所有 非抽象 方法。
+
+[javax.servlet.http.HttpServlet 中 doXXX() 方法](http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServlet.html),这些方法默认返回 405 "Method Not Allowed" ,你可以自由地选择覆盖实现其中的一个或多个。
+
+### [访问者模式](http://en.wikipedia.org/wiki/Visitor_pattern)
+
+[javax.lang.model.element.AnnotationValue](http://docs.oracle.com/javase/6/docs/api/javax/lang/model/element/AnnotationValue.html) 和 [AnnotationValueVisitor](http://docs.oracle.com/javase/6/docs/api/javax/lang/model/element/AnnotationValueVisitor.html)
+[javax.lang.model.element.Element](http://docs.oracle.com/javase/6/docs/api/javax/lang/model/element/Element.html) 和 [ElementVisitor](http://docs.oracle.com/javase/6/docs/api/javax/lang/model/element/ElementVisitor.html)
+[javax.lang.model.type.TypeMirror](http://docs.oracle.com/javase/6/docs/api/javax/lang/model/type/TypeMirror.html) 和 [TypeVisitor](http://docs.oracle.com/javase/6/docs/api/javax/lang/model/type/TypeVisitor.html)
+[java.nio.file.FileVisitor](http://docs.oracle.com/javase/7/docs/api/java/nio/file/FileVisitor.html) 和 [SimpleFileVisitor](http://docs.oracle.com/javase/7/docs/api/java/nio/file/SimpleFileVisitor.html)
+
+
+附录拓展:
+
+- [设计模式-百度百科](http://baike.baidu.com/link?url=_XNWwtm_SeObjikESBkyse_nfXm2HIOOkwJ1XwyVZALLU36AG36DhOMN0Utln5-nJBT6aAplJFOGXCdwQSsm3_)
+
+stackoverflow原址:http://stackoverflow.com/questions/1673841/examples-of-gof-design-patterns-in-javas-core-libraries
\ No newline at end of file
From 61df85a4ed9cbd9bf9cd45678f2ff4a3c91ffcc8 Mon Sep 17 00:00:00 2001
From: zhupeiquan
Date: 2015年9月16日 21:22:34 +0800
Subject: [PATCH 007/195] =?UTF-8?q?What=5Fis=5Fa=5FserialVersionUID=5Fand?=
=?UTF-8?q?=5Fwhy=5Fshould=5FI=5Fuse=5Fit.md=20=E4=BF=AE=E6=AD=A3?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
contents/What_is_a_serialVersionUID_and_why_should_I_use_it.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/contents/What_is_a_serialVersionUID_and_why_should_I_use_it.md b/contents/What_is_a_serialVersionUID_and_why_should_I_use_it.md
index c9ee44c..192d129 100644
--- a/contents/What_is_a_serialVersionUID_and_why_should_I_use_it.md
+++ b/contents/What_is_a_serialVersionUID_and_why_should_I_use_it.md
@@ -15,8 +15,8 @@ serialVersionUID 是实现 Serializable 接口而来的,而 Serializable 则
```
ANY-ACCESS-MODIFIER static final long serialVersionUID = 1L;
```
+ 当显式定义 serialVersionUID 的值时,Java 根据类的多个方面(具体可参考 Java 序列化规范)动态生成一个默认的 serialVersionUID 。尽管这样,还是建议你在每一个序列化的类中显式指定 serialVersionUID 的值,因为不同的 jdk 编译很可能会生成不同的 serialVersionUID 默认值,进而导致在反序列化时抛出 InvalidClassExceptions 异常。所以,为了保证在不同的 jdk 编译实现中,其 serialVersionUID 的值也一致,可序列化的类必须显式指定 serialVersionUID 的值。另外,serialVersionUID 的修饰符最好是 private,因为 serialVersionUID 不能被继承,所以建议使用 private 修饰 serialVersionUID。
-文档上还建议将 serialVersionUID 置为 private。
举例说明如下:
现在尝试通过将一个类 Person 序列化到磁盘和反序列化来说明 serialVersionUID 的作用: Person 类如下:
From a0b92fe701ca614bcefb26d90b4c0936bf9ea359 Mon Sep 17 00:00:00 2001
From: zhupeiquan
Date: 2015年9月16日 21:41:37 +0800
Subject: [PATCH 008/195] =?UTF-8?q?What=5Fis=5Fan=5Fefficient=5Fway=5Fto?=
=?UTF-8?q?=5Fimplement=5Fa=5Fsingleton=5Fin=5FJava.md=20=E4=BF=AE?=
=?UTF-8?q?=E6=AD=A3?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...ent_way_to_implement_a_singleton_in_Java.md | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git a/contents/What_is_an_efficient_way_to_implement_a_singleton_in_Java.md b/contents/What_is_an_efficient_way_to_implement_a_singleton_in_Java.md
index f81fe32..b3e2d78 100644
--- a/contents/What_is_an_efficient_way_to_implement_a_singleton_in_Java.md
+++ b/contents/What_is_an_efficient_way_to_implement_a_singleton_in_Java.md
@@ -145,6 +145,24 @@ public enum Foo {
INSTANCE;
}
```
+08 年 google 开发者年会中,Joshua Bloch
+Joshua Bloch 在 [高效 Java 话题中](http://sites.google.com/site/io/effective-java-reloaded) 解释了这种方法,视频请戳 [这里](http://www.youtube.com/watch?v=pi_I7oD_uGI#t=28m50s).在 他[演讲的ppt](https://14b1424d-a-62cb3a1a-s-sites.googlegroups.com/site/io/effective-java-reloaded/effective_java_reloaded.pdf?attachauth=ANoY7crKCOet2NEUGW7RV1XfM-Jn4z8YJhs0qJM11OhLRnFW_JbExkJtvJ3UJvTE40dhAciyWcRIeGJ-n3FLGnMOapHShHINh8IY05YViOJoZWzaohMtM-s4HCi5kjREagi8awWtcYD0_6G7GhKr2BndToeqLk5sBhZcQfcYIyAE5A4lGNosDCjODcBAkJn8EuO6572t2wU1LMSEUgjvqcf4I-Fp6VDhDvih_XUEmL9nuVJQynd2DRpxyuNH1SpJspEIdbLw-WWZ&attredirects=0) 30-32 页提到:
+
+ 实现单例正确的方式如下:
+ ```
+ public enum Elvis {
+ INSTANCE;
+ private final String[] favoriteSongs =
+ { "Hound Dog", "Heartbreak Hotel" };
+ public void printFavorites() {
+ System.out.println(Arrays.toString(favoriteSongs));
+ }
+ }
+ ```
+
+在 [高效 Java 线上部分](http://www.ddj.com/java/208403883?pgno=3) 有说到:
+
+ 上述实现单例的方式,其实等同于,将 INSTANCE 设置为 public static final 的方式,不同之处在于,使用枚举的方式显得更为简洁,且默认提供了序列化机制,也保证了多线程访问的安全。虽然这种单例的实现方式还未被广泛使用,可实现单例的最好方式就是使用一个单元素的枚举。
为什么可以这么简洁?因为 Java 中每一个枚举类型都默认继承了 java.lang.Enum ,而 Enum 实现了 Serializable 接口,所以枚举类型对象都是默认可以被序列化的。通过反编译,也可以知道枚举常量本质上就是一个
```
From cbd2a8402099fa583027b20d1be1a1636ebce43c Mon Sep 17 00:00:00 2001
From: zhupeiquan
Date: 2015年9月16日 21:52:57 +0800
Subject: [PATCH 009/195] =?UTF-8?q?How=5Fto=5Fgenerate=5Fa=5Frandom=5Falph?=
=?UTF-8?q?a-numeric=5Fstring.md=20=E4=BF=AE=E6=AD=A3?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...w_to_generate_a_random_alpha-numeric_string.md | 15 +++++++++++++--
1 file changed, 13 insertions(+), 2 deletions(-)
diff --git a/contents/How_to_generate_a_random_alpha-numeric_string.md b/contents/How_to_generate_a_random_alpha-numeric_string.md
index b15ac27..e825676 100644
--- a/contents/How_to_generate_a_random_alpha-numeric_string.md
+++ b/contents/How_to_generate_a_random_alpha-numeric_string.md
@@ -33,6 +33,17 @@ public class RandomString {
}
```
-为了安全,可以考虑使用下面这段简洁且安全的代码,不过用其作为 session 的标识符,可能略显昂贵了一点:
+为了安全,可以考虑使用下面这段简洁且安全的代码,不过用其作为 session 的标识符,倒显得有点大材小用了:
```
-```
\ No newline at end of file
+import java.security.SecureRandom;
+
+public final class SessionIdentifierGenerator {
+ private SecureRandom random = new SecureRandom();
+
+ public String nextSessionId() {
+ return new BigInteger(130, random).toString(32);
+ }
+}
+```
+
+其工作原理就是,使用一个 130 位的安全的随机数生成器生成一个随机数,接着转化为 32 进制。我们知道,128 位安全随机数的生成已经是足够安全的,不过以 32 进制编码的每一个数字可编码 5 位,所以需要取大于 128 且是 5 的倍数,所以就选择了 130 位。相对于 随机 UUID 来说(在标准输出中,每个字符使用 3.4 bit,共 122 bit),每个字符使用 5 个随机的 bit 来编码的方式,显得更为简洁和高效。
\ No newline at end of file
From f644ddfb741051b122d1f0a7c62b6855ccf1eedb Mon Sep 17 00:00:00 2001
From: zhupeiquan
Date: 2015年9月16日 22:00:02 +0800
Subject: [PATCH 010/195] update README.md
---
README.md | 2 --
1 file changed, 2 deletions(-)
diff --git a/README.md b/README.md
index 09f0ac3..a32e137 100644
--- a/README.md
+++ b/README.md
@@ -69,8 +69,6 @@ stackoverflow-Java-top-qa
- [Fastest way to determine if an integer's square root is an integer](http://stackoverflow.com/questions/295579/fastest-way-to-determine-if-an-integers-square-root-is-an-integer)
- [How to fix: Unsupported major.minor version 51.0 error?](http://stackoverflow.com/questions/10382929/how-to-fix-unsupported-major-minor-version-51-0-error)
- [What is reflection and why is it useful?](http://stackoverflow.com/questions/37628/what-is-reflection-and-why-is-it-useful)
-- [How to generate a random alpha-numeric string?](http://stackoverflow.com/questions/41107/how-to-generate-a-random-alpha-numeric-string)
-- [Examples of GoF Design Patterns in Java's core libraries](http://stackoverflow.com/questions/1673841/examples-of-gof-design-patterns-in-javas-core-libraries)
- [Comparing Java enum members: == or equals()?](http://stackoverflow.com/questions/1750435/comparing-java-enum-members-or-equals)
- [Failed to load the JNI shared Library (JDK)](http://stackoverflow.com/questions/7352493/failed-to-load-the-jni-shared-library-jdk)
- [Does Java support default parameter values?](http://stackoverflow.com/questions/997482/does-java-support-default-parameter-values)
From 5c5d7005ebfa7b07ee05713652c2a1677b38aea3 Mon Sep 17 00:00:00 2001
From: zhupeiquan
Date: 2015年9月16日 22:13:22 +0800
Subject: [PATCH 011/195] =?UTF-8?q?What\'s=5Fthe=5Fsimplest=5Fway=5Fto=5Fp?=
=?UTF-8?q?rint=5Fa=5FJava=5Farray.md=20=E6=B7=BB=E5=8A=A0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
..._the_simplest_way_to_print_a_Java_array.md | 49 +++++++++++++++++++
1 file changed, 49 insertions(+)
create mode 100644 contents/What's_the_simplest_way_to_print_a_Java_array.md
diff --git a/contents/What's_the_simplest_way_to_print_a_Java_array.md b/contents/What's_the_simplest_way_to_print_a_Java_array.md
new file mode 100644
index 0000000..76e6aeb
--- /dev/null
+++ b/contents/What's_the_simplest_way_to_print_a_Java_array.md
@@ -0,0 +1,49 @@
+# 输出 Java 数组最简单的方式
+
+## 问题
+因为 Java 数组中没有 toString() 方法,所以直接输出如下数组,输出显得并人性化:
+```
+int[] intArray = new int[] {1, 2, 3, 4, 5};
+System.out.println(intArray); // prints something like '[I@3343c8b3'
+```
+
+有什么方式可以实现如下的输出效果?
+```
+// array of primitives:
+int[] intArray = new int[] {1, 2, 3, 4, 5};
+//output: [1, 2, 3, 4, 5]
+
+// array of object references:
+String[] strArray = new String[] {"John", "Mary", "Bob"};
+//output: [John, Mary, Bob]
+```
+
+## 解答
+
+在 Java 5+ 以上,可以使用 Arrays.toString(arr) 输出数组的值,使用 Arrays.deepToString(arr) 输出多维数组的值.提示,数组中的对象都会调用 toString() 方法输出其值。
+
+首先,引入对应的包:
+```
+package packageName;
+import java.util.Arrays;
+...
+```
+
+例子:
+```
+// simple array
+String[] array = new String[] {"John", "Mary", "Bob"};
+System.out.println(Arrays.toString(array));
+//output: [John, Mary, Bob]
+
+// nested array
+String[][] deepArray = new String[][] {{"John", "Mary"}, {"Alice", "Bob"}};
+System.out.println(Arrays.toString(deepArray));
+//output: [[Ljava.lang.String;@106d69c, [Ljava.lang.String;@52e922]
+System.out.println(Arrays.deepToString(deepArray));
+//output: [[John, Mary], [Alice, Bob]]
+```
+
+对于原始类型的数组,其本质就是将原始类型转换为对应包装类型,然后调用其 toString() 方法输出其值。
+
+stackoverflow原址:http://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array
\ No newline at end of file
From 681b40129fd390664ab613f724a8456de1c114f3 Mon Sep 17 00:00:00 2001
From: zhupeiquan
Date: 2015年9月16日 22:14:28 +0800
Subject: [PATCH 012/195] update README.md
---
README.md | 1 -
1 file changed, 1 deletion(-)
diff --git a/README.md b/README.md
index a32e137..4c399ca 100644
--- a/README.md
+++ b/README.md
@@ -72,7 +72,6 @@ stackoverflow-Java-top-qa
- [Comparing Java enum members: == or equals()?](http://stackoverflow.com/questions/1750435/comparing-java-enum-members-or-equals)
- [Failed to load the JNI shared Library (JDK)](http://stackoverflow.com/questions/7352493/failed-to-load-the-jni-shared-library-jdk)
- [Does Java support default parameter values?](http://stackoverflow.com/questions/997482/does-java-support-default-parameter-values)
-- [What's the simplest way to print a Java array?](http://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array)
- [Why can't I switch on a String?](http://stackoverflow.com/questions/338206/why-cant-i-switch-on-a-string)
- [How to create a Java String from the contents of a file?](http://stackoverflow.com/questions/326390/how-to-create-a-java-string-from-the-contents-of-a-file)
- [How can I convert a stack trace to a string?](http://stackoverflow.com/questions/1149703/how-can-i-convert-a-stack-trace-to-a-string)
From c6f46611b7f9478f3ee7591954f374363fd5c9fd Mon Sep 17 00:00:00 2001
From: zhupeiquan
Date: 2015年9月16日 22:17:47 +0800
Subject: [PATCH 013/195] =?UTF-8?q?What\'s=5Fthe=5Fsimplest=5Fway=5Fto=5Fp?=
=?UTF-8?q?rint=5Fa=5FJava=5Farray.md=20=E4=BF=AE=E6=AD=A3?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
contents/What's_the_simplest_way_to_print_a_Java_array.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/contents/What's_the_simplest_way_to_print_a_Java_array.md b/contents/What's_the_simplest_way_to_print_a_Java_array.md
index 76e6aeb..bb8278e 100644
--- a/contents/What's_the_simplest_way_to_print_a_Java_array.md
+++ b/contents/What's_the_simplest_way_to_print_a_Java_array.md
@@ -1,7 +1,7 @@
# 输出 Java 数组最简单的方式
## 问题
-因为 Java 数组中没有 toString() 方法,所以直接输出如下数组,输出显得并人性化:
+因为 Java 数组中没有 toString() 方法,所以直接输出如下数组,输出显得并不人性化:
```
int[] intArray = new int[] {1, 2, 3, 4, 5};
System.out.println(intArray); // prints something like '[I@3343c8b3'
From 44760d468fa50254299d227dfb4ea1e13e92e354 Mon Sep 17 00:00:00 2001
From: sunwne
Date: 2015年9月26日 21:42:24 +0800
Subject: [PATCH 014/195] =?UTF-8?q?=E5=9C=A8java=E4=B8=AD=E5=A3=B0?=
=?UTF-8?q?=E6=98=8E=E6=95=B0=E7=BB=84?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
contents/Declare-array-in-Java.md | 23 +++++++++++++++++++++++
1 file changed, 23 insertions(+)
create mode 100644 contents/Declare-array-in-Java.md
diff --git a/contents/Declare-array-in-Java.md b/contents/Declare-array-in-Java.md
new file mode 100644
index 0000000..0bf87ba
--- /dev/null
+++ b/contents/Declare-array-in-Java.md
@@ -0,0 +1,23 @@
+##在java中声明数组
+
+###问题描述:
+你是如何在jva中声明数组的。
+
+你可以进行用数组声明或者数组常量(注意:当你声明或者立即影响了变量,数组常量将不能再用来分配一个数组)
+对于原始类型:
+```
+int[] myIntArray = new int[3];
+int[] myIntArray = {1, 2, 3};
+int[] myIntArray = new int[]{1, 2, 3};
+```
+杜宇类,比如String类,也是相同的:
+```
+String[] myStringArray = new String[3];
+String[] myStringArray = {"a", "b","c"};
+String[] myStringArray = new String[]("a", "b", "c");
+```
+[stack overflow链接:Declare array in Java?](http://stackoverflow.com/questions/1200621/declare-array-in-java#)
+
+
+
+
From f238da4242f9837b3bfba9760b609c45356d6c80 Mon Sep 17 00:00:00 2001
From: sunwne
Date: Sat, 3 Oct 2015 14:15:01 +0800
Subject: [PATCH 015/195] =?UTF-8?q?=E4=B8=80=E8=A1=8C=E4=BB=A3=E7=A0=81?=
=?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96ArrayList?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...tialization-of-an-arraylist-in-one-line.md | 114 +++++++++---------
1 file changed, 58 insertions(+), 56 deletions(-)
diff --git a/contents/initialization-of-an-arraylist-in-one-line.md b/contents/initialization-of-an-arraylist-in-one-line.md
index cd690fe..ebd4b9b 100644
--- a/contents/initialization-of-an-arraylist-in-one-line.md
+++ b/contents/initialization-of-an-arraylist-in-one-line.md
@@ -1,56 +1,58 @@
-##如何通过一行代码初始化ArrayList
-
-###问题
-为了测试,我需要临时快速创建一个list。一开始我这样做:
-```java
-ArrayList places = new ArrayList();
-places.add("Buenos Aires");
-places.add("Córdoba");
-places.add("La Plata");
-```
-之后我重构了下
-```java
-ArrayList places = new ArrayList(
- Arrays.asList("Buenos Aires", "Córdoba", "La Plata"));
-```
-是否有更加简便的方法呢?
-
-###回答
-
-####常见方式
-实际上,也许"最好"的方式,就是你写的这个方式,因为它不用再创建新的`List`:
-```
-ArrayList list = new ArrayList();
-list.add("A");
-list.add("B");
-list.add("C");
-```
-只是这个方式看上去要多写些代码,让人郁闷
-
-####匿名内部类
-当然,还有其他方式,例如,写一个匿名内部类,然后在其中做初始化(也被称为 brace initialization):
-```
-ArrayList list = new ArrayList() {{
- add("A");
- add("B");
- add("C");
-}};
-```
-但是,我不喜欢这个方式。只是为了做个初始化,却要在`ArrayList`的同一行后面加这么一坨代码。
-
-####Arrays.asList
-```
-List places = Arrays.asList("Buenos Aires", "Córdoba", "La Plata");
-```
-####Collections.singletonList
-```
-List places = Collections.singletonList("Buenos Aires");
-```
-注意:后面的这两种方式,得到的是一个定长的`List`(如果add操作会抛异常)。如果你需要一个不定长的`List`,可以这样做:
-```
-ArrayList places = new ArrayList(Arrays.asList("Buenos Aires", "Córdoba", "La Plata"));
-
-```
-
-stackoverflow链接:
-http://stackoverflow.com/questions/1005073/initialization-of-an-arraylist-in-one-line
\ No newline at end of file
+如何用一句代码初始化一个ArrayList
+##问题描述
+
+我想创建一个用于测试的的选项列表,首先:
+```
+ArrayList places = new ArrayList();
+places.add("Buenos Aires");
+places.add("Córdoba");
+places.add("La Plata");
+```
+之后我如下重构了代码:
+```
+ArrayList places = new ArrayList(
+ Arrays.asList("Buenos Aires", "Córdoba", "La Plata"));
+```
+是否还有更好的方法来完成这个功能呢?
+
+###回答一(750赞)
+实际上,可能初始化ArrayList的"最好"的方法就如你所写的一样,无论如何,都不需要创建一个新的list。
+```
+ArrayList list = new ArrayList();
+list.add("A");
+list.add("B");
+list.add("C");
+```
+但问题是,有相当多多的类型需要参考`list`实例。
+可以有替代的办法,比如使用匿名内部类,初始化实例,也被称为一个"double brace initailzation"。
+```
+ArrayList list = new ArrayList() {{
+ add("A");
+ add("B");
+ add("C");
+}};
+```
+然而,我不是太喜欢这种方法,因为这样得到是一个初始化一个实例的ArrayList的子类,并且这个类只是被用来创建一个对象,好像我有点过分了。
+如果 `the Collection Literals proposal for Project Coin (抱歉,不知怎么翻译)`被接受的话,可能是最好的方式(将被引入Java7,但它不太可能成为Java 8的一部分)。
+[更多关于the Collection Literals proposal的内容链接](http://stackoverflow.com/questions/924285/efficiency-of-java-double-brace-initialization)
+```
+List list = ["A","B","C"];
+```
+不行的是,并没有帮到你。这会初始化成一个不可变的列表,而不是一个ArrayList,而且,现在还不能用。
+
+###回答二(714赞)
+如果你只是作为一个List声明的话,这样可能会更简单.(必须是ArrayList吗)
+```
+List places = Arrays.asList("Buenos Aires", "Córdoba", "La Plata");
+```
+或者只有一个元素:
+```
+List places = Collections.singletonList("Buenos Aires");
+```
+这意味着places是不可变的(如果改变它,将会抛出一个异常).
+让一个可变列表是一个具体的ArrayList,您可以从不可变列表创建一个ArrayList:
+```
+ArrayList places = new ArrayList(Arrays.asList("Buenos Aires", "Córdoba", "La Plata"));
+```
+[原文链接:Initialization of an ArrayList in one line](http://stackoverflow.com/questions/1005073/initialization-of-an-arraylist-in-one-line)
+
From 88d1c32a366f8d1f8bc7b2d7a5423c38b80cea1c Mon Sep 17 00:00:00 2001
From: sunwne
Date: Sat, 3 Oct 2015 14:18:18 +0800
Subject: [PATCH 016/195] =?UTF-8?q?=E4=BB=80=E4=B9=88=E6=98=AF=E5=8F=8D?=
=?UTF-8?q?=E5=B0=84=EF=BC=8C=E4=B8=BA=E4=BB=80=E4=B9=88=E6=9C=89=E7=94=A8?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...What-is-reflection-and-why-is-it-useful.md | 24 +++++++++++++++++++
1 file changed, 24 insertions(+)
create mode 100644 contents/What-is-reflection-and-why-is-it-useful.md
diff --git a/contents/What-is-reflection-and-why-is-it-useful.md b/contents/What-is-reflection-and-why-is-it-useful.md
new file mode 100644
index 0000000..908c441
--- /dev/null
+++ b/contents/What-is-reflection-and-why-is-it-useful.md
@@ -0,0 +1,24 @@
+# What is reflection, and why is it useful?
+
+##问题描述
+反射是什么,为什么它是有用的?
+我特别感兴趣的是java,但我认为任何语言的原理都是相同的。
+
+##回答
+反射是被用来描述代码的,可以检查同一系统(或者自身)的其他代码。
+
+举个例子,在java中你有一个不知道具体类型的对象,并且你可能会调用它的dosomething的方法(如果存在的话)java的静态类型系统并没有设计来支持此用法,除非对象符合已知的接口。但是用反射,你的代码能查看对象,并找出一个dosomething的方法(如果有的话),然后你可以调用他。
+因此,一个例子如下(想象在问题中的对象foo):
+```
+Method method = foo.getClass().getMethod("dosomething",null);
+method.invoke(foo,null); //调用foo的dosomething方法
+```
+java中一个非常常见的用例是使用注释。JUnit 4,举个例子,将使用反射来浏览你的标记有@test注释的类方法,之后当运行测试单元时进行调用。
+
+[有很多好的反射例子,可以用来入门](http://docs.oracle.com/javase/tutorial/reflect/index.html)
+
+最后,其概念在其他支持反射的静态类型语言中是非常相似的。在动态语言中,上面描述的用例并不是必要的(因为编译器允许任何方法都能被任何对象调用,在运行时如果不存在就会失败),但是第二种情况,寻找能以某种确定的方式工作的方法任然是常见的。
+
+[原文链接:What is reflection, and why is it useful?](http://stackoverflow.com/questions/37628/what-is-reflection-and-why-is-it-useful)
+
+
From 5bb65f75143b84e3e0a7584ab2017bd1d5858bba Mon Sep 17 00:00:00 2001
From: sunwne
Date: Sat, 3 Oct 2015 14:19:41 +0800
Subject: [PATCH 017/195] =?UTF-8?q?=E4=B8=BA=E4=BB=80=E4=B9=88=E4=B8=8D?=
=?UTF-8?q?=E8=83=BD=E7=94=A8string=E4=BD=9C=E4=B8=BAswitch=E7=9A=84case?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
contents/Why-can't-I-switch-on-a-String.md | 41 ++++++++++++++++++++++
1 file changed, 41 insertions(+)
create mode 100644 contents/Why-can't-I-switch-on-a-String.md
diff --git a/contents/Why-can't-I-switch-on-a-String.md b/contents/Why-can't-I-switch-on-a-String.md
new file mode 100644
index 0000000..d6d76ab
--- /dev/null
+++ b/contents/Why-can't-I-switch-on-a-String.md
@@ -0,0 +1,41 @@
+# Why can't I switch on a String
+
+##问题描述
+为什么不能用string类型进行switch判断?
+是否作为一个功能将会被添加如java后续版本中?
+有人能给我一篇文章,或者解释一下为什么不能这样做,说明java中switch语句的运行方式?
+
+##回答
+用string作为case来使用switch语句已经在java SE7中被实现了,距离这个问题被提出至少有16年了。一个明确的原因迟迟未提供,但是性能要求它必须这样做。
+
+**Implementtation in JDK 7**
+这个特性现在已经被实现,`javac`with a ["de-sugaring peocess"](http://blogs.oracle.com/darcy/entry/project_coin_string_switch_anatomy)。一个高级的,清楚的使用String常量的语法能在case声明中被使用,会在编译时扩展成为更复杂的代码模式,生成的代码使用jvm指令也一直存在。
+switch将string用作case在编译时翻译成两个switch。第一次每一个string映射到一个独一无二的整数,它的位置在原始的switch上。这是通过在case上的hashcode进行switch选择,相应的案例是一个if语句,测试字符串是否相等。如果有哈希碰撞,测试就像`if-else-if`。
+第二次switch反映在最初的源代码中,用相应位置上的整数代替响应的string标签case。这两步过程更容易保留原始switch的流程控制。
+
+**Switchs in the JVM**
+在switch的更多深层技术上,可以参考JVM规范,[compliation of switch statements](http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-3.html#jvms-3.10)有响应的描述。简单概括说,根据使用的常量的多少,switch有两种不同的JVM指令来执行,每种情况下都取决于使用整数常量来有效的执行。
+
+如果常量很多,case会在一个指令表被用作索引(减去最小值后)——`tablewitch`指令
+如果常量相对较少,那么可用二分查找来找到正确的case--`lookupswitch`指令
+
+在`de-sugaring`中,一个switch(String)的对象,两种指令都会被用到。`lookupswitch`指令适用于第一次计算哈希值找到初始位置,由此产生的顺序表很自然的用`tableswitch`指令
+
+两种指令在编译时都需要将整数常量赋值给每个case。在运行时,虽然`tableswitch`O(1)的性能通常要好于`lookupswitch`O(log(n))的性能,但是前者需要一定的分析来决定是否有足够的case来满足时间空间的平衡。Bill Venners的文章[a great article](http://www.artima.com/underthehood/flowP.html)在细节上讲述的更多,以及涉及到从底层的出发,来看其他java流程控制指令。
+
+**Before JDK 7**
+在JDK之前,枚举近似的用String作为switch的case,这使用了静态方法`valueOf`,编译器生成了一个枚举类型。例子如下:
+```
+Pill p = Pill.valueOf(str);
+switch(p) {
+ case RED:pop();break;
+ case BLUE:push();break;
+}
+```
+
+[阅读原文:Why can`t I switch on a String](http://stackoverflow.com/questions/338206/why-cant-i-switch-on-a-string)
+
+
+
+
+
From c2ebd5ef387936b8ea3fc6bbbeac53d50166689c Mon Sep 17 00:00:00 2001
From: tangculijier <327493498@qq.com>
Date: 2015年12月23日 16:37:32 +0800
Subject: [PATCH 018/195] Create
What's-the-simplest-way-to-print-a-Java-array.md
---
...-the-simplest-way-to-print-a-Java-array.md | 62 +++++++++++++++++++
1 file changed, 62 insertions(+)
create mode 100644 contents/What's-the-simplest-way-to-print-a-Java-array.md
diff --git a/contents/What's-the-simplest-way-to-print-a-Java-array.md b/contents/What's-the-simplest-way-to-print-a-Java-array.md
new file mode 100644
index 0000000..6e3ac63
--- /dev/null
+++ b/contents/What's-the-simplest-way-to-print-a-Java-array.md
@@ -0,0 +1,62 @@
+java中打印一个数组最简单的方法是什么
+===
+问题
+---
+在java中,数组没有重写toString()方法,所以我如果直接调用数组toStrign()方法的话,我会知道得到它的内存地址。像这样:
+```java
+int[] intArray = new int[] {1, 2, 3, 4, 5};
+System.out.println(intArray); // 有时候会输出 '[I@3343c8b3'
+```
+但是实际上我想要的输出效果是
+```java
+[1, 2, 3, 4, 5]
+```
+所以打印一个数组最简单的方法是什么?我想要的效果是
+```java
+// 数字数组:
+int[] intArray = new int[] {1, 2, 3, 4, 5};
+//输出: [1, 2, 3, 4, 5]
+
+// 对象数组:
+String[] strArray = new String[] {"John", "Mary", "Bob"};
+//输出: [John, Mary, Bob]
+```
+
+回答
+---
+在JAVA5中使用 Arrays.toString(arr) 或 Arrays.deepToString(arr)来打印数组。
+
+不要忘了引入import java.util.Arrays;
+```java
+package packageName;
+import java.util.Arrays;
+```
+
+```java
+int[] intArray = new int[] {1, 2, 3, 4, 5};
+System.out.println(Arrays.toString(intArray));
+//输出: [1, 2, 3, 4, 5]
+
+String[] strArray = new String[] {"John", "Mary", "Bob"};
+System.out.println(Arrays.deepToString(strArray));
+*//输出: [John, Mary, Bob]
+```
+Arrays.deepToString与Arrays.toString不同之处在于,Arrays.deepToString更适合打印多维数组
+比如:
+
+```java
+String[][] b = new String[3][4];
+ for (int i = 0; i < 3; i++) + { + for (int j = 0; j < 4; j++) + { + b[i][j] = "A" + j; + } + } + System.out.println(Arrays.toString(b)); + //输出[[Ljava.lang.String;@55e6cb2a, [Ljava.lang.String;@23245e75, [Ljava.lang.String;@28b56559] + System.out.println(Arrays.deepToString(b)); + //输出[[A0, A1, A2, A3], [A0, A1, A2, A3], [A0, A1, A2, A3]] + +``` +stackoverflow链接: http://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array From 4070e8b83dd1856edf0a7c1f508bbf2e82065e85 Mon Sep 17 00:00:00 2001 From: tangculijier <327493498@qq.com>
Date: 2015年12月23日 17:06:26 +0800
Subject: [PATCH 019/195] Create
Check-if-at-least-two-out-of-three-booleans-are-true.md
---
...east-two-out-of-three-booleans-are-true.md | 39 +++++++++++++++++++
1 file changed, 39 insertions(+)
create mode 100644 Check-if-at-least-two-out-of-three-booleans-are-true.md
diff --git a/Check-if-at-least-two-out-of-three-booleans-are-true.md b/Check-if-at-least-two-out-of-three-booleans-are-true.md
new file mode 100644
index 0000000..07ac501
--- /dev/null
+++ b/Check-if-at-least-two-out-of-three-booleans-are-true.md
@@ -0,0 +1,39 @@
+给3个布尔变量,当其中有2个或者2个以上为true菜返回true
+===
+问题
+给3个boolean变量,a,b,c,当其中有2个或2个以上为true时才返回true?
+最笨的方法:
+```java
+boolean atLeastTwo(boolean a, boolean b, boolean c)
+{
+ if ((a && b) || (b && c) || (a && c))
+ {
+ return true;
+ }
+ else
+ {
+ return false;
+ }
+}
+```
+* 优雅解法1
+```java
+ return a ? (b || c) : (b && c);
+```
+
+* 优雅解法2
+```java
+ return (a==b) ? a : c;
+```
+
+* 优雅解法3
+```java
+ return a ^ b ? c : a
+```
+
+* 优雅解法4
+```java
+ return a ? (b || c) : (b && c);
+```
+
+stackoverflow链接: http://stackoverflow.com/questions/3076078/check-if-at-least-two-out-of-three-booleans-are-true
From 2cc58758e0f6afdd4f15caab85216e8e1d0e28c5 Mon Sep 17 00:00:00 2001
From: tangculijier <327493498@qq.com>
Date: 2015年12月23日 17:08:48 +0800
Subject: [PATCH 020/195] Rename
Check-if-at-least-two-out-of-three-booleans-are-true.md to
contents/Check-if-at-least-two-out-of-three-booleans-are-true.md
---
.../Check-if-at-least-two-out-of-three-booleans-are-true.md | 0
1 file changed, 0 insertions(+), 0 deletions(-)
rename Check-if-at-least-two-out-of-three-booleans-are-true.md => contents/Check-if-at-least-two-out-of-three-booleans-are-true.md (100%)
diff --git a/Check-if-at-least-two-out-of-three-booleans-are-true.md b/contents/Check-if-at-least-two-out-of-three-booleans-are-true.md
similarity index 100%
rename from Check-if-at-least-two-out-of-three-booleans-are-true.md
rename to contents/Check-if-at-least-two-out-of-three-booleans-are-true.md
From e69a0289a0f723cc03a211c2e1f6254ecf04bd43 Mon Sep 17 00:00:00 2001
From: mahongliang
Date: 2016年1月22日 16:28:37 +0800
Subject: [PATCH 021/195] =?UTF-8?q?=E8=BF=99=E4=B8=AA=E7=AD=94=E6=A1=88?=
=?UTF-8?q?=E6=9B=B4=E5=A5=BD=EF=BC=8C=E6=9B=B4=E8=AF=A6=E7=BB=86?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
contents/iterate-through-a-hashmap.md | 96 +++++++++++++++++++++------
1 file changed, 74 insertions(+), 22 deletions(-)
diff --git a/contents/iterate-through-a-hashmap.md b/contents/iterate-through-a-hashmap.md
index 27a003c..8eb27c7 100644
--- a/contents/iterate-through-a-hashmap.md
+++ b/contents/iterate-through-a-hashmap.md
@@ -1,22 +1,74 @@
-##如何遍历map对象(如HashMap)
-
-###jdk1.5以上版本
-```java
-for (Entry entry : map.entrySet()){
- System.out.println(entry.getKey() + "/" + entry.getValue());
-}
-```
-需要 import java.util.Map.Entry;
-
-###jdk1.4以下版本
-```java
-Iterator entries = myMap.entrySet().iterator();
-while (entries.hasNext()) {
- Entry thisEntry = (Entry) entries.next();
- Object key = thisEntry.getKey();
- Object value = thisEntry.getValue();
-}
-```
-
-stackoverflow链接:
-http://stackoverflow.com/questions/1066589/iterate-through-a-hashmap
\ No newline at end of file
+# HashMap遍历 #
+
+在Java中有多种遍历HashMAp的方法。让我们回顾一下最常见的方法和它们各自的优缺点。由于所有的Map都实现了Map接口,所以接下来方法适用于所有Map接口的图的实现(如:HaspMap,TreeMap,LinkedMap,HashTable,etc)
+
+## 方法#1 使用For-Each迭代entries ##
+
+这是最常见的方法,并在大多数情况下更可取的。当你在循环中需要使用Map的键和值时,就可以使用这个方法
+
+ Map map = new HashMap();
+ for(Map.Entry entry : map.entrySet()){
+ System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue())
+ }
+
+注意:For-Each循环是Java5新引入的,所以只能在Java5以上的版本中使用。如果你遍历的map是null的话,For-Each循环会抛出NullPointerException异常,所以在遍历之前你应该判断是否为空引用。
+
+## 方法#2 使用For-Each迭代keys和values ##
+
+如果你只需要用到map的keys或values时,你可以遍历KeySet或者values代替entrySet
+
+ Map map = new HashMap();
+
+ //iterating over keys only
+ for (Integer key : map.keySet()) {
+ System.out.println("Key = " + key);
+ }
+
+ //iterating over values only
+ for (Integer value : map.values()) {
+ System.out.println("Value = " + value);
+ }
+
+这个方法比entrySet迭代具有轻微的性能优势(大约快10%)并且代码更简洁
+
+## 方法#3 使用Iterator迭代 ##
+
+使用泛型
+
+ Map map = new HashMap();
+ Iterator> entries = map.entrySet().iterator();
+ while (entries.hasNext()) {
+ Map.Entry entry = entries.next();
+ System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
+ }
+
+不适用泛型
+
+ Map map = new HashMap();
+ Iterator entries = map.entrySet().iterator();
+ while (entries.hasNext()) {
+ Map.Entry entry = (Map.Entry) entries.next();
+ Integer key = (Integer)entry.getKey();
+ Integer value = (Integer)entry.getValue();
+ System.out.println("Key = " + key + ", Value = " + value);
+ }
+
+你可以使用同样的技术迭代keyset或者values
+
+这个似乎有点多余但它具有自己的优势。首先,它是遍历老java版本map的唯一方法。另外一个重要的特性是可以让你在迭代的时候从map中删除entries的(通过调用iterator.remover())唯一方法.如果你试图在For-Each迭代的时候删除entries,你将会得到unpredictable resultes 异常。
+
+从性能方法看,这个方法等价于使用For-Each迭代
+
+## 方法#4 迭代keys并搜索values(低效的) ##
+
+ Map map = new HashMap();
+ for (Integer key : map.keySet()) {
+ Integer value = map.get(key);
+ System.out.println("Key = " + key + ", Value = " + value);
+ }
+
+这个方法看上去比方法#1更简洁,但是实际上它更慢更低效,通过key得到value值更耗时(这个方法在所有实现map接口的map中比方法#1慢20%-200%)。如果你安装了FindBugs,它将检测并警告你这是一个低效的迭代。这个方法应该避免
+
+## 总结 ##
+
+如果你只需要使用key或者value使用方法#2,如果你坚持使用java的老版本(java 5 以前的版本)或者打算在迭代的时候移除entries使用方法#3,另外的话使用方法#1,尽量避免出现方法#1的情况
\ No newline at end of file
From ddeb7a7c0f57aa60e4493953a8b26dbdad673d77 Mon Sep 17 00:00:00 2001
From: mahongliang
Date: 2016年1月23日 20:16:36 +0800
Subject: [PATCH 022/195] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96final?=
=?UTF-8?q?=E9=9D=99=E6=80=81map?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
contents/How-can-I-Initialize-a-static-Map.md | 72 +++++++++++++++++++
1 file changed, 72 insertions(+)
create mode 100644 contents/How-can-I-Initialize-a-static-Map.md
diff --git a/contents/How-can-I-Initialize-a-static-Map.md b/contents/How-can-I-Initialize-a-static-Map.md
new file mode 100644
index 0000000..cb502f4
--- /dev/null
+++ b/contents/How-can-I-Initialize-a-static-Map.md
@@ -0,0 +1,72 @@
+# 初始化静态map #
+
+## 问题 ##
+
+怎么在Java中初始化一个静态的map
+
+**方法一**:静态初始化器
+
+**方法二**:实例初始化(匿名子类)
+
+下面是描述上面两种方法的例子
+
+ import java.util.HashMap;
+ import java.util.Map;
+ public class Test{
+ private static final Map myMap = new HashMap();
+ static {
+ myMap.put(1, "one");
+ myMap.put(2, "two");
+ }
+
+ private static final Map myMap2 = new HashMap(){
+ {
+ put(1, "one");
+ put(2, "two");
+ }
+ };
+ }
+
+## 答案 ##
+
+### 答案1 ###
+
+匿名子类初始化器是java的语法糖,我搞不明白为什么匿名类来初始化,而且如果create的类是final的话,它将不起作用
+
+我创建固定大小图的时候使用static初始化器
+
+ public class Test{
+ private static final Map myMap;
+ static{
+ Map aMap = ...;
+ aMap.put(1,"one");
+ aMap.put(2,"two");
+ myMap = Collections.unmodifiableMap(aMap);
+ }
+ }
+
+
+### 答案2 ###
+
+我喜欢用Guava(是 Collection 框架的增强和扩张)的方法初始化一个静态的,不可改变的map
+
+ static fianl Map myMap = ImmutablMap.of(
+ 1,"one",
+ 2, "two"
+ )
+·
+当你的图entry的个数超过5个时候你就不能使用`ImmutableMap.of`可以试试`ImmutableMap.bulider()`
+
+ static fianl Map myMap = ImmutableMap.builder()
+ {
+ .put(1, "one")
+ .put(2, "two")
+
+ .put(15, "fifteen")
+ .build();
+ }
+
+
+# 原文链接 #
+
+http://stackoverflow.com/questions/507602/how-can-i-initialize-a-static-map
\ No newline at end of file
From 7a9b59a1baa350ea3aa963a5c35058b36afecf68 Mon Sep 17 00:00:00 2001
From: mahongliang
Date: 2016年1月25日 13:42:49 +0800
Subject: [PATCH 023/195] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=9B=AE=E5=BD=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...reate-arraylist-arraylistt-from-array-t.md | 12 +-
"contents/347円233円256円345円275円225円.md" | 202 ++++++++++++++++++
"347円233円256円345円275円225円.md" | 202 ++++++++++++++++++
3 files changed, 414 insertions(+), 2 deletions(-)
create mode 100644 "contents/347円233円256円345円275円225円.md"
create mode 100644 "347円233円256円345円275円225円.md"
diff --git a/contents/create-arraylist-arraylistt-from-array-t.md b/contents/create-arraylist-arraylistt-from-array-t.md
index 5536c6e..7d58c8f 100644
--- a/contents/create-arraylist-arraylistt-from-array-t.md
+++ b/contents/create-arraylist-arraylistt-from-array-t.md
@@ -5,9 +5,14 @@
```java
Element[] array = {new Element(1),new Element(2),new Element(3)};
```
-如何将其转换为ArrayList`` arraylist呢?
+如何将其转换为ArrayList`` arraylist = ???
+
+### 回答1 ###
+
+ `new ArrayList(Arrays.asList(array))`
+
+###回答2
-###回答
Arrays.asList(array)或者Arrays.asList(new Element(1),new Element(2),new Element(3))
不过,这样做有些坑要注意:
@@ -15,6 +20,9 @@ Arrays.asList(array)或者Arrays.asList(new Element(1),new Element(2),new Elemen
1. 这样做生成的list,是定长的。也就是说,如果你对它做add或者remove,都会抛UnsupportedOperationException。
2. 如果修改数组的值,list中的对应值也会改变!
+**Arrays.asList() 返回的是Arrays内部静态类,而不是Java.util.ArrayList的类。这个java.util.Arrays.ArrayList有set(),get(),contains()方法,但是没有任何add() 方法,所以它是固定大小的**
+
+
如果希望避免这两个坑,请改用这个方式
```java
Collections.addAll(arraylist, array);
diff --git "a/contents/347円233円256円345円275円225円.md" "b/contents/347円233円256円345円275円225円.md"
new file mode 100644
index 0000000..f2911c8
--- /dev/null
+++ "b/contents/347円233円256円345円275円225円.md"
@@ -0,0 +1,202 @@
+# [目录](http://stackoverflow.com/questions/tagged/java?page=1&sort=votes&pagesize=15) #
+
+## [1、为什么处理排序过得数组比没有排序的数组快](http://stackoverflow.com/questions/11227809/why-is-processing-a-sorted-array-faster-than-an-unsorted-array "Why is processing a sorted array faster than an unsorted array?") ##
+
+
+## [2、为什么在1927年的两个时间相减会产生奇怪的结果](http://stackoverflow.com/questions/6841333/why-is-subtracting-these-two-times-in-1927-giving-a-strange-result) ##
+
+## [3、Java是传引用还是传值](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) ##
+
+## [4、Java += 操作符](http://stackoverflow.com/questions/8710619/java-operator) ##
+
+## [5、避免!=null 声明](http://stackoverflow.com/questions/271526/avoiding-null-statements) ##
+
+## [6、Android适当的用例UserManager.isUserAGoat()](http://stackoverflow.com/questions/13375357/proper-use-cases-for-android-usermanager-isuseragoat) ##
+
+## [7、HashMap和HaspTable的区别](http://stackoverflow.com/questions/40471/differences-between-hashmap-and-hashtable) ##
+
+## [8、InputStream转换为String](http://stackoverflow.com/questions/309424/read-convert-an-inputstream-to-a-string) ##
+
+## [9、为什么在存密码时char[]要优于String](http://stackoverflow.com/questions/8881291/why-is-char-preferred-over-string-for-passwords-in-java) ##
+
+## [10、遍历HashMap](http://stackoverflow.com/questions/1066589/iterate-through-a-hashmap) ##
+
+## [11、用数组创建一个ArrayList](http://stackoverflow.com/questions/157944/create-arraylist-from-array) ##
+
+## [12、产生特定范围的随机数](http://stackoverflow.com/questions/363681/generating-random-integers-in-a-specific-range) ##
+
+## [13、创建内存泄露?](http://stackoverflow.com/questions/6470651/creating-a-memory-leak-with-java) ##
+
+## [14、LinkedList和ArrayList区别](http://stackoverflow.com/questions/322715/when-to-use-linkedlist-over-arraylist) ##
+
+## [15、为什么动态的答应B比答应#慢](http://stackoverflow.com/questions/21947452/why-is-printing-b-dramatically-slower-than-printing) ##
+
+## [16、serialVersionUID是什么](http://stackoverflow.com/questions/285793/what-is-a-serialversionuid-and-why-should-i-use-it) ##
+
+## [17、把字符串转换为Int](http://stackoverflow.com/questions/5585779/converting-string-to-int-in-java) ##
+
+## [18、public、default、protected、private的区别](http://stackoverflow.com/questions/215497/difference-between-public-default-protected-and-private) ##
+
+## [19、Android设备是否有唯一的ID](http://stackoverflow.com/questions/2785485/is-there-a-unique-android-device-id) ##
+
+## [20、怎么样测试私有方法,字段和内部类](http://stackoverflow.com/questions/34571/how-to-test-a-class-that-has-private-methods-fields-or-inner-classes) ##
+
+## [21、怎样有效的遍历Map中的entry](http://stackoverflow.com/questions/46898/how-to-efficiently-iterate-over-each-entry-in-a-map) ##
+
+## [22、使用java.net.URLConnection处理HTTP requests](http://stackoverflow.com/questions/2793150/using-java-net-urlconnection-to-fire-and-handle-http-requests) ##
+
+## [23、为什么这段代码使用随机字符串答应"hello world"](http://stackoverflow.com/questions/15182496/why-does-this-code-using-random-strings-print-hello-world) ##
+
+## [24、 如果使用Maven创建外部JAR](http://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven) ##
+
+## [25、怎样判断数组包含一个特定的值](http://stackoverflow.com/questions/1128723/how-can-i-test-if-an-array-contains-a-certain-value) ##
+
+## [26、如何在JSP中避免java代码](http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files) ##
+
+## [27、一行代码初始化ArrayList](http://stackoverflow.com/questions/1005073/initialization-of-an-arraylist-in-one-line) ##
+
+## [28、finally在java一定会被执行吗](http://stackoverflow.com/questions/65035/does-finally-always-execute-in-java) ##
+
+## [29、"implements Runnable"和"extends Thread"](http://stackoverflow.com/questions/541487/implements-runnable-vs-extends-thread) ##
+
+## [30、如何在一个构造函数中调用另一个](http://stackoverflow.com/questions/285177/how-do-i-call-one-constructor-from-another-in-java) ##
+
+## [31、String转换为Enum](http://stackoverflow.com/questions/604424/convert-a-string-to-an-enum-in-java) ##
+
+## [32、在//后代码会被执行](http://stackoverflow.com/questions/30727515/why-is-executing-java-code-in-comments-with-certain-unicode-characters-allowed) ##
+
+## [33、处理java.lang.OutOfMemoryError:PermGen space错误](http://stackoverflow.com/questions/88235/dealing-with-java-lang-outofmemoryerror-permgen-space-error) ##
+
+## [34、退出嵌套循环](http://stackoverflow.com/questions/886955/breaking-out-of-nested-loops-in-java) ##
+
+## [35、Android SDK 安装后找不到JDK](http://stackoverflow.com/questions/4382178/android-sdk-installation-doesnt-find-jdk) ##
+
+## [36、Java内部类和嵌套类](http://stackoverflow.com/questions/70324/java-inner-class-and-static-nested-class) ##
+
+## [37、在导入项目后出现必须覆盖超类方法的错误](http://stackoverflow.com/questions/1678122/must-override-a-superclass-method-errors-after-importing-a-project-into-eclips) ##
+
+## [38、错误Unsupported major.minor version 51.0 error](http://stackoverflow.com/questions/10382929/how-to-fix-unsupported-major-minor-version-51-0-error) ##
+
+## [39、声明数组](http://stackoverflow.com/questions/1200621/declare-array-java) ##
+
+## [40、反射的作用](http://stackoverflow.com/questions/37628/what-is-reflection-and-why-is-it-useful) ##
+
+## [41、Map值排序](http://stackoverflow.com/questions/109383/sort-a-mapkey-value-by-values-java) ##
+
+## [42、产生字母数字型随机数](http://stackoverflow.com/questions/41107/how-to-generate-a-random-alpha-numeric-string) ##
+
+## [43、判断一个数的根号是整数](http://stackoverflow.com/questions/295579/fastest-way-to-determine-if-an-integers-square-root-is-an-integer) ##
+
+## [44、用==或者equals()比较enum成员](http://stackoverflow.com/questions/1750435/comparing-java-enum-members-or-equals) ##
+
+## [45、最简单的答应数组](http://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array) ##
+
+## [46、java是否支持默认参数值](http://stackoverflow.com/questions/997482/does-java-support-default-parameter-values) ##
+
+## [47、接口和抽象类区别](http://stackoverflow.com/questions/1913098/what-is-the-difference-between-an-interface-and-abstract-class) ##
+
+## [48、IntelliJ持久化行号](http://stackoverflow.com/questions/13751/how-can-i-permanently-have-line-numbers-in-intellij) ##
+
+## [49、StringBuilder and StringBuffer](http://stackoverflow.com/questions/355089/stringbuilder-and-stringbuffer) ##
+
+## [50、怎么连接两个数组](http://stackoverflow.com/questions/80476/how-can-i-concatenate-two-arrays-in-java) ##
+
+## [51、在JUit中怎么断言异常](http://stackoverflow.com/questions/156503/how-do-you-assert-that-a-certain-exception-is-thrown-in-junit-4-tests) ##
+
+## [52、字符串比较](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) ##
+
+## [53、根据文件的内容创建字符串](http://stackoverflow.com/questions/326390/how-to-create-a-java-string-from-the-contents-of-a-file) ##
+
+## [54、Failed to load the JNI shared Library (JDK)](http://stackoverflow.com/questions/7352493/failed-to-load-the-jni-shared-library-jdk) ##
+
+## [55、JavaBean](http://stackoverflow.com/questions/3295496/what-is-a-javabean-exactly) ##
+
+## [56、为什么我不能打开一个字符串](http://stackoverflow.com/questions/338206/why-cant-i-switch-on-a-string) ##
+
+## [57、stack trace转换String](http://stackoverflow.com/questions/1149703/how-can-i-convert-a-stack-trace-to-a-string) ##
+
+## [58、java核心代码的Gof设计模式例子](http://stackoverflow.com/questions/1673841/examples-of-gof-design-patterns-in-javas-core-libraries) ##
+
+## [59、Apache Camel](http://stackoverflow.com/questions/8845186/what-exactly-is-apache-camel) ##
+
+## [60、java为什么会有临时变量](http://stackoverflow.com/questions/910374/why-does-java-have-transient-variables) ##
+
+## [61、Servlet 怎么工作](http://stackoverflow.com/questions/3106452/how-do-servlets-work-instantiation-shared-variables-and-multithreading) ##
+
+## [62、重写equals和hashCode](http://stackoverflow.com/questions/27581/what-issues-should-be-considered-when-overriding-equals-and-hashcode-in-java) ##
+
+## [63、得到当前stack trace](http://stackoverflow.com/questions/1069066/get-current-stack-trace-in-java) ##
+
+## [64、foreach工作原理](http://stackoverflow.com/questions/85190/how-does-the-java-for-each-loop-work) ##
+
+## [65、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) ##
+
+## [66、wait()和sleep()区别](http://stackoverflow.com/questions/1036754/difference-between-wait-and-sleep) ##
+
+## [67、产生MD5hash](http://stackoverflow.com/questions/415953/how-can-i-generate-an-md5-hash) ##
+
+## [68、在调用instanceof之前是否要检测is null](http://stackoverflow.com/questions/2950319/is-null-check-needed-before-calling-instanceof) ##
+
+## [69、Download a file with Android, and showing the progress in a ProgressDialog](http://stackoverflow.com/questions/3028306/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog) ##
+
+## [70、初始化静态Map](http://stackoverflow.com/questions/507602/how-can-i-initialize-a-static-map) ##
+
+## [71、怎么发现Android应用的内存只用量](http://stackoverflow.com/questions/2298208/how-do-i-discover-memory-usage-of-my-application-in-android) ##
+
+## [72、在遍历list的时候,remove一个值,避免出现concurrenModificationExcetiion](http://stackoverflow.com/questions/223918/iterating-through-a-list-avoiding-concurrentmodificationexception-when-removing) ##
+
+## [73、在java classpath中设置多行jars](http://stackoverflow.com/questions/219585/setting-multiple-jars-in-java-classpath) ##
+
+## [74、Hibernate hbm2ddl.auto possible values and what they do](http://stackoverflow.com/questions/438146/hibernate-hbm2ddl-auto-possible-values-and-what-they-do) ##
+
+## [75、split a String](http://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java) ##
+
+## [76、LINQ for java](http://stackoverflow.com/questions/1217228/what-is-the-java-equivalent-for-linq) ##
+
+## [77、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) ##
+
+## [78、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) ##
+
+## [79、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) ##
+
+## [80、对ArrayList的对象属性排序](http://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property) ##
+
+## [81、创建和写文件](http://stackoverflow.com/questions/2885173/how-to-create-a-file-and-write-to-a-file-in-java) ##
+
+## [82、Update Eclipse with Android development tools v. 23](http://stackoverflow.com/questions/24437564/update-eclipse-with-android-development-tools-v-23) ##
+
+## [83、Uncatchable ChuckNorrisException](http://stackoverflow.com/questions/13883166/uncatchable-chucknorrisexception) ##
+
+## [84、怎么在一个整数的左边填充0](http://stackoverflow.com/questions/473282/how-can-i-pad-an-integers-with-zeros-on-the-left) ##
+
+## [85、创建泛型数组](http://stackoverflow.com/questions/529085/how-to-create-a-generic-array-in-java) ##
+
+## [86、判断三个以上为true条件](http://stackoverflow.com/questions/3076078/check-if-at-least-two-out-of-three-booleans-are-true) ##
+
+## [87、ThreadLocal variable](http://stackoverflow.com/questions/817856/when-and-how-should-i-use-a-threadlocal-variable) ##
+
+## [88、JSF、Servlet、JSP区别](http://stackoverflow.com/questions/2095397/what-is-the-difference-between-jsf-servlet-and-jsp) ##
+
+## [89、为什么java向量类被认为是过时的和被废弃](http://stackoverflow.com/questions/1386275/why-is-java-vector-class-considered-obsolete-or-deprecated) ##
+
+## [90、反编译DEX为源码](http://stackoverflow.com/questions/1249973/decompiling-dex-into-java-sourcecode) ##
+
+## [91、ArrayList转换为String[]数组](http://stackoverflow.com/questions/5374311/convert-arrayliststring-to-string-array) ##
+
+## [92、软引用和弱引用的区别](http://stackoverflow.com/questions/299659/what-is-the-difference-between-a-soft-reference-and-a-weak-reference-in-java) ##
+
+## [93、Efficiency of Java "Double Brace Initialization"](http://stackoverflow.com/questions/924285/efficiency-of-java-double-brace-initialization) ##
+
+## [94、实现单例模式的最有效方法](http://stackoverflow.com/questions/70689/what-is-an-efficient-way-to-implement-a-singleton-pattern-in-java) ##
+
+## [95、java中什么相当于C++的](http://stackoverflow.com/questions/156275/what-is-the-equivalent-of-the-c-pairl-r-in-java) ##
+
+## [96、从纯文本文件中读](http://stackoverflow.com/questions/4716503/reading-a-plain-text-file-in-java) ##
+
+## [97、Which @NotNull Java annotation should I use](http://stackoverflow.com/questions/4963300/which-notnull-java-annotation-should-i-use) ##
+
+## [98、为什么这段代码陷入无限循环](http://stackoverflow.com/questions/3831341/why-does-this-go-into-an-infinite-loop) ##
+
+## [99、Eclipse: Set maximum line length for auto formatting](http://stackoverflow.com/questions/3697287/eclipse-set-maximum-line-length-for-auto-formatting) ##
+
+## [100、Why does Math.round(0.49999999999999994) return 1](http://stackoverflow.com/questions/9902968/why-does-math-round0-49999999999999994-return-1) ##
diff --git "a/347円233円256円345円275円225円.md" "b/347円233円256円345円275円225円.md"
new file mode 100644
index 0000000..f2911c8
--- /dev/null
+++ "b/347円233円256円345円275円225円.md"
@@ -0,0 +1,202 @@
+# [目录](http://stackoverflow.com/questions/tagged/java?page=1&sort=votes&pagesize=15) #
+
+## [1、为什么处理排序过得数组比没有排序的数组快](http://stackoverflow.com/questions/11227809/why-is-processing-a-sorted-array-faster-than-an-unsorted-array "Why is processing a sorted array faster than an unsorted array?") ##
+
+
+## [2、为什么在1927年的两个时间相减会产生奇怪的结果](http://stackoverflow.com/questions/6841333/why-is-subtracting-these-two-times-in-1927-giving-a-strange-result) ##
+
+## [3、Java是传引用还是传值](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) ##
+
+## [4、Java += 操作符](http://stackoverflow.com/questions/8710619/java-operator) ##
+
+## [5、避免!=null 声明](http://stackoverflow.com/questions/271526/avoiding-null-statements) ##
+
+## [6、Android适当的用例UserManager.isUserAGoat()](http://stackoverflow.com/questions/13375357/proper-use-cases-for-android-usermanager-isuseragoat) ##
+
+## [7、HashMap和HaspTable的区别](http://stackoverflow.com/questions/40471/differences-between-hashmap-and-hashtable) ##
+
+## [8、InputStream转换为String](http://stackoverflow.com/questions/309424/read-convert-an-inputstream-to-a-string) ##
+
+## [9、为什么在存密码时char[]要优于String](http://stackoverflow.com/questions/8881291/why-is-char-preferred-over-string-for-passwords-in-java) ##
+
+## [10、遍历HashMap](http://stackoverflow.com/questions/1066589/iterate-through-a-hashmap) ##
+
+## [11、用数组创建一个ArrayList](http://stackoverflow.com/questions/157944/create-arraylist-from-array) ##
+
+## [12、产生特定范围的随机数](http://stackoverflow.com/questions/363681/generating-random-integers-in-a-specific-range) ##
+
+## [13、创建内存泄露?](http://stackoverflow.com/questions/6470651/creating-a-memory-leak-with-java) ##
+
+## [14、LinkedList和ArrayList区别](http://stackoverflow.com/questions/322715/when-to-use-linkedlist-over-arraylist) ##
+
+## [15、为什么动态的答应B比答应#慢](http://stackoverflow.com/questions/21947452/why-is-printing-b-dramatically-slower-than-printing) ##
+
+## [16、serialVersionUID是什么](http://stackoverflow.com/questions/285793/what-is-a-serialversionuid-and-why-should-i-use-it) ##
+
+## [17、把字符串转换为Int](http://stackoverflow.com/questions/5585779/converting-string-to-int-in-java) ##
+
+## [18、public、default、protected、private的区别](http://stackoverflow.com/questions/215497/difference-between-public-default-protected-and-private) ##
+
+## [19、Android设备是否有唯一的ID](http://stackoverflow.com/questions/2785485/is-there-a-unique-android-device-id) ##
+
+## [20、怎么样测试私有方法,字段和内部类](http://stackoverflow.com/questions/34571/how-to-test-a-class-that-has-private-methods-fields-or-inner-classes) ##
+
+## [21、怎样有效的遍历Map中的entry](http://stackoverflow.com/questions/46898/how-to-efficiently-iterate-over-each-entry-in-a-map) ##
+
+## [22、使用java.net.URLConnection处理HTTP requests](http://stackoverflow.com/questions/2793150/using-java-net-urlconnection-to-fire-and-handle-http-requests) ##
+
+## [23、为什么这段代码使用随机字符串答应"hello world"](http://stackoverflow.com/questions/15182496/why-does-this-code-using-random-strings-print-hello-world) ##
+
+## [24、 如果使用Maven创建外部JAR](http://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven) ##
+
+## [25、怎样判断数组包含一个特定的值](http://stackoverflow.com/questions/1128723/how-can-i-test-if-an-array-contains-a-certain-value) ##
+
+## [26、如何在JSP中避免java代码](http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files) ##
+
+## [27、一行代码初始化ArrayList](http://stackoverflow.com/questions/1005073/initialization-of-an-arraylist-in-one-line) ##
+
+## [28、finally在java一定会被执行吗](http://stackoverflow.com/questions/65035/does-finally-always-execute-in-java) ##
+
+## [29、"implements Runnable"和"extends Thread"](http://stackoverflow.com/questions/541487/implements-runnable-vs-extends-thread) ##
+
+## [30、如何在一个构造函数中调用另一个](http://stackoverflow.com/questions/285177/how-do-i-call-one-constructor-from-another-in-java) ##
+
+## [31、String转换为Enum](http://stackoverflow.com/questions/604424/convert-a-string-to-an-enum-in-java) ##
+
+## [32、在//后代码会被执行](http://stackoverflow.com/questions/30727515/why-is-executing-java-code-in-comments-with-certain-unicode-characters-allowed) ##
+
+## [33、处理java.lang.OutOfMemoryError:PermGen space错误](http://stackoverflow.com/questions/88235/dealing-with-java-lang-outofmemoryerror-permgen-space-error) ##
+
+## [34、退出嵌套循环](http://stackoverflow.com/questions/886955/breaking-out-of-nested-loops-in-java) ##
+
+## [35、Android SDK 安装后找不到JDK](http://stackoverflow.com/questions/4382178/android-sdk-installation-doesnt-find-jdk) ##
+
+## [36、Java内部类和嵌套类](http://stackoverflow.com/questions/70324/java-inner-class-and-static-nested-class) ##
+
+## [37、在导入项目后出现必须覆盖超类方法的错误](http://stackoverflow.com/questions/1678122/must-override-a-superclass-method-errors-after-importing-a-project-into-eclips) ##
+
+## [38、错误Unsupported major.minor version 51.0 error](http://stackoverflow.com/questions/10382929/how-to-fix-unsupported-major-minor-version-51-0-error) ##
+
+## [39、声明数组](http://stackoverflow.com/questions/1200621/declare-array-java) ##
+
+## [40、反射的作用](http://stackoverflow.com/questions/37628/what-is-reflection-and-why-is-it-useful) ##
+
+## [41、Map值排序](http://stackoverflow.com/questions/109383/sort-a-mapkey-value-by-values-java) ##
+
+## [42、产生字母数字型随机数](http://stackoverflow.com/questions/41107/how-to-generate-a-random-alpha-numeric-string) ##
+
+## [43、判断一个数的根号是整数](http://stackoverflow.com/questions/295579/fastest-way-to-determine-if-an-integers-square-root-is-an-integer) ##
+
+## [44、用==或者equals()比较enum成员](http://stackoverflow.com/questions/1750435/comparing-java-enum-members-or-equals) ##
+
+## [45、最简单的答应数组](http://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array) ##
+
+## [46、java是否支持默认参数值](http://stackoverflow.com/questions/997482/does-java-support-default-parameter-values) ##
+
+## [47、接口和抽象类区别](http://stackoverflow.com/questions/1913098/what-is-the-difference-between-an-interface-and-abstract-class) ##
+
+## [48、IntelliJ持久化行号](http://stackoverflow.com/questions/13751/how-can-i-permanently-have-line-numbers-in-intellij) ##
+
+## [49、StringBuilder and StringBuffer](http://stackoverflow.com/questions/355089/stringbuilder-and-stringbuffer) ##
+
+## [50、怎么连接两个数组](http://stackoverflow.com/questions/80476/how-can-i-concatenate-two-arrays-in-java) ##
+
+## [51、在JUit中怎么断言异常](http://stackoverflow.com/questions/156503/how-do-you-assert-that-a-certain-exception-is-thrown-in-junit-4-tests) ##
+
+## [52、字符串比较](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) ##
+
+## [53、根据文件的内容创建字符串](http://stackoverflow.com/questions/326390/how-to-create-a-java-string-from-the-contents-of-a-file) ##
+
+## [54、Failed to load the JNI shared Library (JDK)](http://stackoverflow.com/questions/7352493/failed-to-load-the-jni-shared-library-jdk) ##
+
+## [55、JavaBean](http://stackoverflow.com/questions/3295496/what-is-a-javabean-exactly) ##
+
+## [56、为什么我不能打开一个字符串](http://stackoverflow.com/questions/338206/why-cant-i-switch-on-a-string) ##
+
+## [57、stack trace转换String](http://stackoverflow.com/questions/1149703/how-can-i-convert-a-stack-trace-to-a-string) ##
+
+## [58、java核心代码的Gof设计模式例子](http://stackoverflow.com/questions/1673841/examples-of-gof-design-patterns-in-javas-core-libraries) ##
+
+## [59、Apache Camel](http://stackoverflow.com/questions/8845186/what-exactly-is-apache-camel) ##
+
+## [60、java为什么会有临时变量](http://stackoverflow.com/questions/910374/why-does-java-have-transient-variables) ##
+
+## [61、Servlet 怎么工作](http://stackoverflow.com/questions/3106452/how-do-servlets-work-instantiation-shared-variables-and-multithreading) ##
+
+## [62、重写equals和hashCode](http://stackoverflow.com/questions/27581/what-issues-should-be-considered-when-overriding-equals-and-hashcode-in-java) ##
+
+## [63、得到当前stack trace](http://stackoverflow.com/questions/1069066/get-current-stack-trace-in-java) ##
+
+## [64、foreach工作原理](http://stackoverflow.com/questions/85190/how-does-the-java-for-each-loop-work) ##
+
+## [65、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) ##
+
+## [66、wait()和sleep()区别](http://stackoverflow.com/questions/1036754/difference-between-wait-and-sleep) ##
+
+## [67、产生MD5hash](http://stackoverflow.com/questions/415953/how-can-i-generate-an-md5-hash) ##
+
+## [68、在调用instanceof之前是否要检测is null](http://stackoverflow.com/questions/2950319/is-null-check-needed-before-calling-instanceof) ##
+
+## [69、Download a file with Android, and showing the progress in a ProgressDialog](http://stackoverflow.com/questions/3028306/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog) ##
+
+## [70、初始化静态Map](http://stackoverflow.com/questions/507602/how-can-i-initialize-a-static-map) ##
+
+## [71、怎么发现Android应用的内存只用量](http://stackoverflow.com/questions/2298208/how-do-i-discover-memory-usage-of-my-application-in-android) ##
+
+## [72、在遍历list的时候,remove一个值,避免出现concurrenModificationExcetiion](http://stackoverflow.com/questions/223918/iterating-through-a-list-avoiding-concurrentmodificationexception-when-removing) ##
+
+## [73、在java classpath中设置多行jars](http://stackoverflow.com/questions/219585/setting-multiple-jars-in-java-classpath) ##
+
+## [74、Hibernate hbm2ddl.auto possible values and what they do](http://stackoverflow.com/questions/438146/hibernate-hbm2ddl-auto-possible-values-and-what-they-do) ##
+
+## [75、split a String](http://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java) ##
+
+## [76、LINQ for java](http://stackoverflow.com/questions/1217228/what-is-the-java-equivalent-for-linq) ##
+
+## [77、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) ##
+
+## [78、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) ##
+
+## [79、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) ##
+
+## [80、对ArrayList的对象属性排序](http://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property) ##
+
+## [81、创建和写文件](http://stackoverflow.com/questions/2885173/how-to-create-a-file-and-write-to-a-file-in-java) ##
+
+## [82、Update Eclipse with Android development tools v. 23](http://stackoverflow.com/questions/24437564/update-eclipse-with-android-development-tools-v-23) ##
+
+## [83、Uncatchable ChuckNorrisException](http://stackoverflow.com/questions/13883166/uncatchable-chucknorrisexception) ##
+
+## [84、怎么在一个整数的左边填充0](http://stackoverflow.com/questions/473282/how-can-i-pad-an-integers-with-zeros-on-the-left) ##
+
+## [85、创建泛型数组](http://stackoverflow.com/questions/529085/how-to-create-a-generic-array-in-java) ##
+
+## [86、判断三个以上为true条件](http://stackoverflow.com/questions/3076078/check-if-at-least-two-out-of-three-booleans-are-true) ##
+
+## [87、ThreadLocal variable](http://stackoverflow.com/questions/817856/when-and-how-should-i-use-a-threadlocal-variable) ##
+
+## [88、JSF、Servlet、JSP区别](http://stackoverflow.com/questions/2095397/what-is-the-difference-between-jsf-servlet-and-jsp) ##
+
+## [89、为什么java向量类被认为是过时的和被废弃](http://stackoverflow.com/questions/1386275/why-is-java-vector-class-considered-obsolete-or-deprecated) ##
+
+## [90、反编译DEX为源码](http://stackoverflow.com/questions/1249973/decompiling-dex-into-java-sourcecode) ##
+
+## [91、ArrayList转换为String[]数组](http://stackoverflow.com/questions/5374311/convert-arrayliststring-to-string-array) ##
+
+## [92、软引用和弱引用的区别](http://stackoverflow.com/questions/299659/what-is-the-difference-between-a-soft-reference-and-a-weak-reference-in-java) ##
+
+## [93、Efficiency of Java "Double Brace Initialization"](http://stackoverflow.com/questions/924285/efficiency-of-java-double-brace-initialization) ##
+
+## [94、实现单例模式的最有效方法](http://stackoverflow.com/questions/70689/what-is-an-efficient-way-to-implement-a-singleton-pattern-in-java) ##
+
+## [95、java中什么相当于C++的](http://stackoverflow.com/questions/156275/what-is-the-equivalent-of-the-c-pairl-r-in-java) ##
+
+## [96、从纯文本文件中读](http://stackoverflow.com/questions/4716503/reading-a-plain-text-file-in-java) ##
+
+## [97、Which @NotNull Java annotation should I use](http://stackoverflow.com/questions/4963300/which-notnull-java-annotation-should-i-use) ##
+
+## [98、为什么这段代码陷入无限循环](http://stackoverflow.com/questions/3831341/why-does-this-go-into-an-infinite-loop) ##
+
+## [99、Eclipse: Set maximum line length for auto formatting](http://stackoverflow.com/questions/3697287/eclipse-set-maximum-line-length-for-auto-formatting) ##
+
+## [100、Why does Math.round(0.49999999999999994) return 1](http://stackoverflow.com/questions/9902968/why-does-math-round0-49999999999999994-return-1) ##
From b48d6730131d9a13bcb08c2dbc253e8134de3bf5 Mon Sep 17 00:00:00 2001
From: yann
Date: 2016年2月17日 19:32:04 +0800
Subject: [PATCH 024/195] add question convert-a-string-to-an-enum-in-java
---
.../convert-a-string-to-an-enum-in-java.md | 52 +++++++++++++++++++
1 file changed, 52 insertions(+)
create mode 100644 contents/convert-a-string-to-an-enum-in-java.md
diff --git a/contents/convert-a-string-to-an-enum-in-java.md b/contents/convert-a-string-to-an-enum-in-java.md
new file mode 100644
index 0000000..1f78462
--- /dev/null
+++ b/contents/convert-a-string-to-an-enum-in-java.md
@@ -0,0 +1,52 @@
+##如何将String转换为enum
+
+
+
+### 问题
+一个枚举定义:
+
+```java
+public enum Blah {
+ A, B, C, D
+}
+```
+并且我知道枚举的String值,比如 "A",我想将其转换为Blah.A,我应该怎么做?
+是否有Enum.valueOf() 这样的方法,如果是,那我如何使用?
+
+
+### 答案
+是的,Blah.valueOf("A") 将会得到 Blah.A
+
+静态方法valueOf() 和 values() 会在编译期创建,不过这不会体现在源代码内,他们出现在JavaDoc中,比如 [Dialog.ModalityTyp](http://docs.oracle.com/javase/7/docs/api/java/awt/Dialog.ModalityType.html) 中出现这两个方法。
+
+### 其他答案
+
+我有一个友善的工具方法:
+```java
+/**
+ * A common method for all enums since they can't have another base class
+ * @param Enum type
+ * @param c enum type. All enums must be all caps.
+ * @param string case insensitive
+ * @return corresponding enum, or null
+ */
+public static > T getEnumFromString(Class c, String string) {
+ if( c != null && string != null ) {
+ try {
+ return Enum.valueOf(c, string.trim().toUpperCase());
+ } catch(IllegalArgumentException ex) {
+ }
+ }
+ return null;
+}
+```
+
+你可以这么使用:
+
+```java
+public static MyEnum fromString(String name) {
+ return getEnumFromString(MyEnum.class, name);
+}
+```
+
+stackoverflow链接:http://stackoverflow.com/questions/604424/convert-a-string-to-an-enum-in-java
\ No newline at end of file
From 953f7ec765a37b5fd5cecc865738cf0eb453cff9 Mon Sep 17 00:00:00 2001
From: yann
Date: 2016年2月17日 22:35:42 +0800
Subject: [PATCH 025/195] add readme
---
README.md | 2 +-
contents/convert-a-string-to-an-enum-in-java.md | 2 --
2 files changed, 1 insertion(+), 3 deletions(-)
diff --git a/README.md b/README.md
index a54c7e3..3c1cc6a 100644
--- a/README.md
+++ b/README.md
@@ -31,6 +31,7 @@ stackoverflow-Java-top-qa
* [wait()和sleep()的区别](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/difference-between-wait-and-sleep.md)
* [能否在一个构造器( `constructor` )中调用另一个构造器](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/How-do-i-call-one-constructor-from-another-in-java.md)
* [ `finally` 代码块总会被执行么](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Does-finally-always-execute-in-Java.md)
+* [如何将String转换为enum](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/convert-a-string-to-an-enum-in-java.md)
> 编程技巧
@@ -65,7 +66,6 @@ stackoverflow-Java-top-qa
- [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)
- [Dealing with "java.lang.OutOfMemoryError: PermGen space" error](http://stackoverflow.com/questions/88235/dealing-with-java-lang-outofmemoryerror-permgen-space-error)
- ["implements Runnable" vs. "extends Thread"](http://stackoverflow.com/questions/541487/implements-runnable-vs-extends-thread)
-- [Convert a String to an enum in Java](http://stackoverflow.com/questions/604424/convert-a-string-to-an-enum-in-java)
- [Android SDK installation doesn't find JDK](http://stackoverflow.com/questions/4382178/android-sdk-installation-doesnt-find-jdk)
- [Initialization of an ArrayList in one line](http://stackoverflow.com/questions/1005073/initialization-of-an-arraylist-in-one-line)
- [Java inner class and static nested class](http://stackoverflow.com/questions/70324/java-inner-class-and-static-nested-class)
diff --git a/contents/convert-a-string-to-an-enum-in-java.md b/contents/convert-a-string-to-an-enum-in-java.md
index 1f78462..f3e7edb 100644
--- a/contents/convert-a-string-to-an-enum-in-java.md
+++ b/contents/convert-a-string-to-an-enum-in-java.md
@@ -1,7 +1,5 @@
##如何将String转换为enum
-
-
### 问题
一个枚举定义:
From 9135324977a2cc5013f3b2fd0f301711fffb4878 Mon Sep 17 00:00:00 2001
From: giantray <51961070@qq.com>
Date: 2016年2月21日 23:08:37 +0800
Subject: [PATCH 026/195] =?UTF-8?q?=E4=BC=98=E5=8C=96=E2=80=9C=E5=A6=82?=
=?UTF-8?q?=E4=BD=95=E5=B0=86String=E8=BD=AC=E6=8D=A2=E4=B8=BAenum"?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
contents/convert-a-string-to-an-enum-in-java.md | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/contents/convert-a-string-to-an-enum-in-java.md b/contents/convert-a-string-to-an-enum-in-java.md
index f3e7edb..5e11958 100644
--- a/contents/convert-a-string-to-an-enum-in-java.md
+++ b/contents/convert-a-string-to-an-enum-in-java.md
@@ -1,25 +1,25 @@
##如何将String转换为enum
### 问题
-一个枚举定义:
+假设定义了如下的enum(枚举):
```java
public enum Blah {
A, B, C, D
}
```
-并且我知道枚举的String值,比如 "A",我想将其转换为Blah.A,我应该怎么做?
-是否有Enum.valueOf() 这样的方法,如果是,那我如何使用?
+已知枚举对应的String值,希望得到对应的枚举值。例如,已知"A",希望得到对应的枚举——Blah.A,应该怎么做?
+Enum.valueOf()是否能实现以上目的,如果是,那我如何使用?
### 答案
是的,Blah.valueOf("A") 将会得到 Blah.A
-静态方法valueOf() 和 values() 会在编译期创建,不过这不会体现在源代码内,他们出现在JavaDoc中,比如 [Dialog.ModalityTyp](http://docs.oracle.com/javase/7/docs/api/java/awt/Dialog.ModalityType.html) 中出现这两个方法。
+静态方法valueOf() 和 values() 不存在于源码中,而是在编译时创建,我们也可以在JavaDoc查看到它们,比如 [Dialog.ModalityTyp](http://docs.oracle.com/javase/7/docs/api/java/awt/Dialog.ModalityType.html) 就中出现这两个方法。
### 其他答案
-我有一个友善的工具方法:
+我有一个挺赞的工具方法:
```java
/**
* A common method for all enums since they can't have another base class
From db285206684c09b9e4de2c6bbe8ba6679f1a5547 Mon Sep 17 00:00:00 2001
From: giantray <51961070@qq.com>
Date: 2016年2月21日 23:29:38 +0800
Subject: [PATCH 027/195] =?UTF-8?q?=E4=BC=98=E5=8C=96=E2=80=9Cdownload-a-f?=
=?UTF-8?q?ile-with-android-and-showing-the-progress-in-a-progressdialog"?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...th_Android_and_showing_the_progress_in_a_ProgressDialog.md | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/contents/Download_a_file_with_Android_and_showing_the_progress_in_a_ProgressDialog.md b/contents/Download_a_file_with_Android_and_showing_the_progress_in_a_ProgressDialog.md
index 959213c..fcca4e3 100644
--- a/contents/Download_a_file_with_Android_and_showing_the_progress_in_a_ProgressDialog.md
+++ b/contents/Download_a_file_with_Android_and_showing_the_progress_in_a_ProgressDialog.md
@@ -1,7 +1,7 @@
## 在Android里面下载文件,并在ProgressDialog显示进度
### 问题
-尝试写一个可以获得更新的应用程序。 为了达到这个效果,我写了一个可以下载文件并且在一个`ProgressDialog`里面显示进度的简单方法。我知道怎么使用`ProgressDialog`,但是我不太确定怎么显示当前进度和下载文件。
+尝试写一个可以进行"应用更新"的APP。为了达到这个效果,我写了一个可以下载文件并且在一个`ProgressDialog`里面显示进度的简单方法。我知道怎么使用`ProgressDialog`,但是我不太确定怎么显示当前进度和下载文件。
### 回答
@@ -371,3 +371,5 @@ manager.enqueue(request);
+ 如果可以有暂停或者取消下载的选项,用户会很感激你的!
除非你想对下载过程有绝对的控制权,否则我强烈推荐你使用`DownloadManager`。因为他已经处理好了上面的大部分建议。
+
+stackoverflow链接:http://stackoverflow.com/questions/3028306/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog
From 537acad6acaea27dabf767d7a81d71bbed079634 Mon Sep 17 00:00:00 2001
From: lizeyang
Date: 2016年2月22日 18:14:15 +0800
Subject: [PATCH 028/195] =?UTF-8?q?=E4=BC=98=E5=8C=96=E2=80=9C=E5=9C=A8jav?=
=?UTF-8?q?a=E4=B8=AD=E5=A3=B0=E6=98=8E=E6=95=B0=E7=BB=84=E2=80=9D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 2 +-
contents/Declare-array-in-Java.md | 15 +++++++++------
2 files changed, 10 insertions(+), 7 deletions(-)
diff --git a/README.md b/README.md
index ca8bdf7..8a859f4 100644
--- a/README.md
+++ b/README.md
@@ -32,6 +32,7 @@ stackoverflow-Java-top-qa
* [能否在一个构造器( `constructor` )中调用另一个构造器](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/How-do-i-call-one-constructor-from-another-in-java.md)
* [ `finally` 代码块总会被执行么](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Does-finally-always-execute-in-Java.md)
* [如何将String转换为enum](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/convert-a-string-to-an-enum-in-java.md)
+* [在Java中声明数组](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Declare-array-in-Java.md)
> 编程技巧
@@ -74,7 +75,6 @@ stackoverflow-Java-top-qa
- [Initialization of an ArrayList in one line](http://stackoverflow.com/questions/1005073/initialization-of-an-arraylist-in-one-line)
- [Java inner class and static nested class](http://stackoverflow.com/questions/70324/java-inner-class-and-static-nested-class)
- ['Must Override a Superclass Method' Errors after importing a project into Eclipse](http://stackoverflow.com/questions/1678122/must-override-a-superclass-method-errors-after-importing-a-project-into-eclips)
-- [Declare array in Java?](http://stackoverflow.com/questions/1200621/declare-array-in-java)
- [Fastest way to determine if an integer's square root is an integer](http://stackoverflow.com/questions/295579/fastest-way-to-determine-if-an-integers-square-root-is-an-integer)
- [How to fix: Unsupported major.minor version 51.0 error?](http://stackoverflow.com/questions/10382929/how-to-fix-unsupported-major-minor-version-51-0-error)
- [What is reflection and why is it useful?](http://stackoverflow.com/questions/37628/what-is-reflection-and-why-is-it-useful)
diff --git a/contents/Declare-array-in-Java.md b/contents/Declare-array-in-Java.md
index 0bf87ba..0b8dfe6 100644
--- a/contents/Declare-array-in-Java.md
+++ b/contents/Declare-array-in-Java.md
@@ -1,22 +1,25 @@
-##在java中声明数组
+##在java中声明数组
###问题描述:
-你是如何在jva中声明数组的。
+你是如何在Java中声明数组的。
-你可以进行用数组声明或者数组常量(注意:当你声明或者立即影响了变量,数组常量将不能再用来分配一个数组)
-对于原始类型:
+###回答:
+你可以直接用数组声明,或者通过数组的字面常量(array literal )声明
+
+对于原始类型(primitive types):
```
int[] myIntArray = new int[3];
int[] myIntArray = {1, 2, 3};
int[] myIntArray = new int[]{1, 2, 3};
```
-杜宇类,比如String类,也是相同的:
+
+对于其他类,比如String类,也是相同的:
```
String[] myStringArray = new String[3];
String[] myStringArray = {"a", "b","c"};
String[] myStringArray = new String[]("a", "b", "c");
```
-[stack overflow链接:Declare array in Java?](http://stackoverflow.com/questions/1200621/declare-array-in-java#)
+[stackoverflow链接:Declare array in Java?](http://stackoverflow.com/questions/1200621/declare-array-in-java)
From 3e59fa69548c29e054cc3c981c0a7d4e80b1afda Mon Sep 17 00:00:00 2001
From: lizeyang
Date: 2016年2月23日 16:16:51 +0800
Subject: [PATCH 029/195] =?UTF-8?q?=E4=BC=98=E5=8C=96=E2=80=9C=E5=8F=8D?=
=?UTF-8?q?=E5=B0=84=EF=BC=88reflection=EF=BC=89=E6=98=AF=E4=BB=80?=
=?UTF-8?q?=E4=B9=88=E5=8F=8A=E5=85=B6=E7=94=A8=E9=80=94=3F=E2=80=9D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 3 ++-
.../What-is-reflection-and-why-is-it-useful.md | 15 ++++++++-------
2 files changed, 10 insertions(+), 8 deletions(-)
diff --git a/README.md b/README.md
index 8a859f4..ae731b6 100644
--- a/README.md
+++ b/README.md
@@ -33,6 +33,8 @@ stackoverflow-Java-top-qa
* [ `finally` 代码块总会被执行么](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Does-finally-always-execute-in-Java.md)
* [如何将String转换为enum](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/convert-a-string-to-an-enum-in-java.md)
* [在Java中声明数组](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Declare-array-in-Java.md)
+* [反射是什么及其用途](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/What-is-reflection-and-why-is-it-useful.md.md)
+
> 编程技巧
@@ -77,7 +79,6 @@ stackoverflow-Java-top-qa
- ['Must Override a Superclass Method' Errors after importing a project into Eclipse](http://stackoverflow.com/questions/1678122/must-override-a-superclass-method-errors-after-importing-a-project-into-eclips)
- [Fastest way to determine if an integer's square root is an integer](http://stackoverflow.com/questions/295579/fastest-way-to-determine-if-an-integers-square-root-is-an-integer)
- [How to fix: Unsupported major.minor version 51.0 error?](http://stackoverflow.com/questions/10382929/how-to-fix-unsupported-major-minor-version-51-0-error)
-- [What is reflection and why is it useful?](http://stackoverflow.com/questions/37628/what-is-reflection-and-why-is-it-useful)
- [How to generate a random alpha-numeric string?](http://stackoverflow.com/questions/41107/how-to-generate-a-random-alpha-numeric-string)
- [Examples of GoF Design Patterns in Java's core libraries](http://stackoverflow.com/questions/1673841/examples-of-gof-design-patterns-in-javas-core-libraries)
- [Comparing Java enum members: == or equals()?](http://stackoverflow.com/questions/1750435/comparing-java-enum-members-or-equals)
diff --git a/contents/What-is-reflection-and-why-is-it-useful.md b/contents/What-is-reflection-and-why-is-it-useful.md
index 908c441..1de0ef0 100644
--- a/contents/What-is-reflection-and-why-is-it-useful.md
+++ b/contents/What-is-reflection-and-why-is-it-useful.md
@@ -1,24 +1,25 @@
-# What is reflection, and why is it useful?
+# 反射(reflection)是什么及其用途?
##问题描述
反射是什么,为什么它是有用的?
我特别感兴趣的是java,但我认为任何语言的原理都是相同的。
##回答
-反射是被用来描述代码的,可以检查同一系统(或者自身)的其他代码。
+反射的概念,主要是指程序可以访问、检测和修改它本身状态或行为的一种能力。在java中,通过反射,能够在"运行态"动态获得任意一个类的所有属性和方法,动态地调用对象的方法。
-举个例子,在java中你有一个不知道具体类型的对象,并且你可能会调用它的dosomething的方法(如果存在的话)java的静态类型系统并没有设计来支持此用法,除非对象符合已知的接口。但是用反射,你的代码能查看对象,并找出一个dosomething的方法(如果有的话),然后你可以调用他。
-因此,一个例子如下(想象在问题中的对象foo):
+举个例子,假设你有一个不知道具体类的对象,并且你想调用它的"dosomething"方法(如果存在的话)。java的静态类型系统只能调用一个已知类对象对应的已知接口,在未指定对象类型时,无法调用它的方法。但是通过反射,你的代码能检查这个未知类对象,并试图找出这个dosomething方法。如果存在这个方法,你可以通过反射调用这个方法。
+
+为了进一步说明,请看下面的例子(下面的对象foo,就是上文提到的,我们不知道它对应的类是什么):
```
Method method = foo.getClass().getMethod("dosomething",null);
method.invoke(foo,null); //调用foo的dosomething方法
```
-java中一个非常常见的用例是使用注释。JUnit 4,举个例子,将使用反射来浏览你的标记有@test注释的类方法,之后当运行测试单元时进行调用。
+反射这个特性,经常会用于各种注解中(annotations)。举个例子,Junit4将使用反射来遍历你的代码,查找所有加了@test注解的类方法,之后运行测试单元时就调用这些方法。
[有很多好的反射例子,可以用来入门](http://docs.oracle.com/javase/tutorial/reflect/index.html)
-最后,其概念在其他支持反射的静态类型语言中是非常相似的。在动态语言中,上面描述的用例并不是必要的(因为编译器允许任何方法都能被任何对象调用,在运行时如果不存在就会失败),但是第二种情况,寻找能以某种确定的方式工作的方法任然是常见的。
+最后,其概念在其他支持反射的静态类型语言中也是非常相似的。在动态语言中,无需用到上面说的第一种用法场景——调用未知类的方法(因为动态语言编允许任意对象调用任意方法,如果不存在对应方法,在运行时就会失败),但是第二种情况,查找做了指定标记的方法,这种场景还是很常见的
-[原文链接:What is reflection, and why is it useful?](http://stackoverflow.com/questions/37628/what-is-reflection-and-why-is-it-useful)
+[stackoverflow链接:What is reflection, and why is it useful?](http://stackoverflow.com/questions/37628/what-is-reflection-and-why-is-it-useful)
From 05aceb8932cb3a72a390867c2c586fb967d57e8b Mon Sep 17 00:00:00 2001
From: lizeyang
Date: 2016年2月23日 16:23:15 +0800
Subject: [PATCH 030/195] =?UTF-8?q?=E4=BC=98=E5=8C=96=E2=80=9C=E5=A6=82?=
=?UTF-8?q?=E4=BD=95=E7=94=A8=E4=B8=80=E8=A1=8C=E4=BB=A3=E7=A0=81=E5=88=9D?=
=?UTF-8?q?=E5=A7=8B=E5=8C=96=E4=B8=80=E4=B8=AAArrayList=E2=80=9D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 2 +-
...tialization-of-an-arraylist-in-one-line.md | 47 +++++++++----------
2 files changed, 24 insertions(+), 25 deletions(-)
diff --git a/README.md b/README.md
index ae731b6..ae1269d 100644
--- a/README.md
+++ b/README.md
@@ -40,7 +40,7 @@ stackoverflow-Java-top-qa
* [去掉烦人的"!=null"(判空语句](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/avoiding-null-statements-in-java.md)
* [获取完整的堆栈信息](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/get-current-stack-trace-in-java.md)
-* [如何通过一行代码初始化ArrayList](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/initialization-of-an-arraylist-in-one-line.md)
+* [如何用一行代码初始化一个ArrayList](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/initialization-of-an-arraylist-in-one-line.md)
> 网络
diff --git a/contents/initialization-of-an-arraylist-in-one-line.md b/contents/initialization-of-an-arraylist-in-one-line.md
index ebd4b9b..5541e13 100644
--- a/contents/initialization-of-an-arraylist-in-one-line.md
+++ b/contents/initialization-of-an-arraylist-in-one-line.md
@@ -1,30 +1,34 @@
-如何用一句代码初始化一个ArrayList
-##问题描述
+如何用一行代码初始化一个ArrayList
-我想创建一个用于测试的的选项列表,首先:
-```
+###问题
+为了测试,我需要临时快速创建一个list。一开始我这样做:
+```java
ArrayList places = new ArrayList();
places.add("Buenos Aires");
places.add("Córdoba");
places.add("La Plata");
```
-之后我如下重构了代码:
-```
+之后我重构了下
+```java
ArrayList places = new ArrayList(
Arrays.asList("Buenos Aires", "Córdoba", "La Plata"));
```
-是否还有更好的方法来完成这个功能呢?
+是否有更加简便的方法呢?
-###回答一(750赞)
-实际上,可能初始化ArrayList的"最好"的方法就如你所写的一样,无论如何,都不需要创建一个新的list。
+###回答
+
+####常见方式
+实际上,也许"最好"的方式,就是你写的这个方式,因为它不用再创建新的`List`:
```
ArrayList list = new ArrayList();
list.add("A");
list.add("B");
list.add("C");
```
-但问题是,有相当多多的类型需要参考`list`实例。
-可以有替代的办法,比如使用匿名内部类,初始化实例,也被称为一个"double brace initailzation"。
+只是这个方式看上去要多写些代码,让人郁闷
+
+####匿名内部类
+当然,还有其他方式,例如,写一个匿名内部类,然后在其中做初始化(也被称为 brace initialization):
```
ArrayList list = new ArrayList() {{
add("A");
@@ -32,27 +36,22 @@ ArrayList list = new ArrayList() {{
add("C");
}};
```
-然而,我不是太喜欢这种方法,因为这样得到是一个初始化一个实例的ArrayList的子类,并且这个类只是被用来创建一个对象,好像我有点过分了。
-如果 `the Collection Literals proposal for Project Coin (抱歉,不知怎么翻译)`被接受的话,可能是最好的方式(将被引入Java7,但它不太可能成为Java 8的一部分)。
-[更多关于the Collection Literals proposal的内容链接](http://stackoverflow.com/questions/924285/efficiency-of-java-double-brace-initialization)
-```
-List list = ["A","B","C"];
-```
-不行的是,并没有帮到你。这会初始化成一个不可变的列表,而不是一个ArrayList,而且,现在还不能用。
+但是,我不喜欢这个方式。只是为了做个初始化,却要在`ArrayList`的同一行后面加这么一坨代码。
-###回答二(714赞)
-如果你只是作为一个List声明的话,这样可能会更简单.(必须是ArrayList吗)
+####Arrays.asList
```
List places = Arrays.asList("Buenos Aires", "Córdoba", "La Plata");
```
-或者只有一个元素:
+####Collections.singletonList
```
List places = Collections.singletonList("Buenos Aires");
```
-这意味着places是不可变的(如果改变它,将会抛出一个异常).
-让一个可变列表是一个具体的ArrayList,您可以从不可变列表创建一个ArrayList:
+注意:后面的这两种方式,得到的是一个定长的`List`(如果add操作会抛异常)。如果你需要一个不定长的`List`,可以这样做:
```
ArrayList places = new ArrayList(Arrays.asList("Buenos Aires", "Córdoba", "La Plata"));
+
```
-[原文链接:Initialization of an ArrayList in one line](http://stackoverflow.com/questions/1005073/initialization-of-an-arraylist-in-one-line)
+
+stackoverflow链接:
+http://stackoverflow.com/questions/1005073/initialization-of-an-arraylist-in-one-line
From 0fa06f8f9108615bbed863cbb31049729b23fecd Mon Sep 17 00:00:00 2001
From: lizeyang
Date: 2016年2月23日 18:00:37 +0800
Subject: [PATCH 031/195] =?UTF-8?q?=E4=BC=98=E5=8C=96=E2=80=9C=E4=B8=BA?=
=?UTF-8?q?=E4=BB=80=E4=B9=88=E4=B8=8D=E8=83=BD=E7=94=A8string=E7=B1=BB?=
=?UTF-8?q?=E5=9E=8B=E8=BF=9B=E8=A1=8Cswitch=E5=88=A4=E6=96=AD=E2=80=9D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 2 +-
contents/Why-can't-I-switch-on-a-String.md | 81 ++++++++++++++++++----
2 files changed, 68 insertions(+), 15 deletions(-)
diff --git a/README.md b/README.md
index ae1269d..746e42c 100644
--- a/README.md
+++ b/README.md
@@ -34,7 +34,7 @@ stackoverflow-Java-top-qa
* [如何将String转换为enum](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/convert-a-string-to-an-enum-in-java.md)
* [在Java中声明数组](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Declare-array-in-Java.md)
* [反射是什么及其用途](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/What-is-reflection-and-why-is-it-useful.md.md)
-
+* [为什么不能用string类型进行switch判断](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Why-can't-I-switch-on-a-String.md)
> 编程技巧
diff --git a/contents/Why-can't-I-switch-on-a-String.md b/contents/Why-can't-I-switch-on-a-String.md
index d6d76ab..140a662 100644
--- a/contents/Why-can't-I-switch-on-a-String.md
+++ b/contents/Why-can't-I-switch-on-a-String.md
@@ -1,30 +1,81 @@
-# Why can't I switch on a String
+# 为什么不能用string类型进行switch判断
##问题描述
为什么不能用string类型进行switch判断?
-是否作为一个功能将会被添加如java后续版本中?
-有人能给我一篇文章,或者解释一下为什么不能这样做,说明java中switch语句的运行方式?
+在java的后续版本中,是否会增加这个新特性?
+有人能给我一篇文章,解释一下为什么不能这样做,或者进一步说明java中switch语句的运行方式?
##回答
-用string作为case来使用switch语句已经在java SE7中被实现了,距离这个问题被提出至少有16年了。一个明确的原因迟迟未提供,但是性能要求它必须这样做。
+在switch语句中用string作为case,这个特性已经在java SE7 中被实现了,距离 [这个'bug'](http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=1223179) 被提出至少也有16年了。为何迟迟不提供这个特性,原因不明。但可以推测,可能跟性能有关。
**Implementtation in JDK 7**
-这个特性现在已经被实现,`javac`with a ["de-sugaring peocess"](http://blogs.oracle.com/darcy/entry/project_coin_string_switch_anatomy)。一个高级的,清楚的使用String常量的语法能在case声明中被使用,会在编译时扩展成为更复杂的代码模式,生成的代码使用jvm指令也一直存在。
-switch将string用作case在编译时翻译成两个switch。第一次每一个string映射到一个独一无二的整数,它的位置在原始的switch上。这是通过在case上的hashcode进行switch选择,相应的案例是一个if语句,测试字符串是否相等。如果有哈希碰撞,测试就像`if-else-if`。
-第二次switch反映在最初的源代码中,用相应位置上的整数代替响应的string标签case。这两步过程更容易保留原始switch的流程控制。
+
+在JDK7中,这个特性已经实现了。在编译阶段,以string作为case值的代码,会按照特定的模式,被转换为更加复杂的代码。最终的执行代码将是一些使用了JVM指令的代码。
+
+究竟是如何转换的呢?我们直接看看源码及编译后的代码。源代码:
+```
+public class StringInSwitchCase {
+ public static void main(String[] args) {
+ String mode = args[0];
+ switch (mode) {
+ case "ACTIVE":
+ System.out.println("Application is running on Active mode");
+ break;
+ case "PASSIVE":
+ System.out.println("Application is running on Passive mode");
+ break;
+ case "SAFE":
+ System.out.println("Application is running on Safe mode");
+ }
+ }
+}
+```
+编译后再反编译的代码:
+```
+import java.io.PrintStream;
+
+public class StringInSwitchCase{
+ public StringInSwitchCase() { }
+
+ public static void main(string args[]) {
+ String mode = args[0];
+ String s; switch ((s = mode).hashCode()) {
+ default: break;
+ case -74056953:
+ if (s.equals("PASSIVE")) {
+ System.out.println("Application is running on Passive mode");
+ }
+ break;
+ case 2537357:
+ if (s.equals("SAFE")) {
+ System.out.println("Application is running on Safe mode");
+ }
+ break;
+ case 1925346054:
+ if (s.equals("ACTIVE")) {
+ System.out.println("Application is running on Active mode");
+ }
+ break;
+ }
+ }
+}
+```
+
+包含case string的 switch 语句,在编译时会转为为嵌套代码(switch+if)。第一个switch将 case 中的string转为唯一的integer值。这个integer值就是原先string的hashcode值。在case的逻辑中,会加入if语句,这个if语句用于进一步检查string值是否跟原先的case string匹配。这样可以防止hash碰撞,确保代码的健壮。这本质上是一种语法糖,既支持了string作为case值这一特性,又能确保逻辑正确性。
**Switchs in the JVM**
-在switch的更多深层技术上,可以参考JVM规范,[compliation of switch statements](http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-3.html#jvms-3.10)有响应的描述。简单概括说,根据使用的常量的多少,switch有两种不同的JVM指令来执行,每种情况下都取决于使用整数常量来有效的执行。
-如果常量很多,case会在一个指令表被用作索引(减去最小值后)——`tablewitch`指令
-如果常量相对较少,那么可用二分查找来找到正确的case--`lookupswitch`指令
+switch的更多深层技术实现,可以参考JVM规范,[compliation of switch statements](http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-3.html#jvms-3.10)。简单概括说,根据使用的常量的多寡,switch会对应到两种不同的JVM指令。JVM指令有所不同,归根结底都是为了代码的效率。
-在`de-sugaring`中,一个switch(String)的对象,两种指令都会被用到。`lookupswitch`指令适用于第一次计算哈希值找到初始位置,由此产生的顺序表很自然的用`tableswitch`指令
+如果常量很多,会将case的int值去掉最低位后作为索引,放到一个指针表中——也就是所谓的`tablewitch`指令
-两种指令在编译时都需要将整数常量赋值给每个case。在运行时,虽然`tableswitch`O(1)的性能通常要好于`lookupswitch`O(log(n))的性能,但是前者需要一定的分析来决定是否有足够的case来满足时间空间的平衡。Bill Venners的文章[a great article](http://www.artima.com/underthehood/flowP.html)在细节上讲述的更多,以及涉及到从底层的出发,来看其他java流程控制指令。
+如果常量相对较少,那么可用二分查找来找到正确的case--也就是所谓的`lookupswitch`指令
+
+这两种指令,都要求在编译时确保case的对应值是integer常量。在运行时,虽然`tableswitch`O(1)的性能通常要好于`lookupswitch`O(log(n))的性能。但是前者需要更多的空间开销,因此需要兼顾空间及时间综合考虑性价比。Bill Venners的文章[a great article](http://www.artima.com/underthehood/flowP.html)有更多深入的分析。
**Before JDK 7**
-在JDK之前,枚举近似的用String作为switch的case,这使用了静态方法`valueOf`,编译器生成了一个枚举类型。例子如下:
+
+在JDK之前,可以用枚举来实现类似的需求。它和在case中使用string有异曲同工之妙。例如如下:
```
Pill p = Pill.valueOf(str);
switch(p) {
@@ -33,7 +84,9 @@ switch(p) {
}
```
-[阅读原文:Why can`t I switch on a String](http://stackoverflow.com/questions/338206/why-cant-i-switch-on-a-string)
+[stackoverflow原链接:Why can`t I switch on a String](http://stackoverflow.com/questions/338206/why-cant-i-switch-on-a-string)
+
+[可参考中文文章《Java中字符串switch的实现细节》](http://www.deepinmind.com/java/2014/05/08/how-string-in-switch-works-in-java-7.html)
From b3621158cc1b2998164aa915732a280973305385 Mon Sep 17 00:00:00 2001
From: lizeyang
Date: 2016年2月23日 18:12:55 +0800
Subject: [PATCH 032/195] =?UTF-8?q?=E4=BC=98=E5=8C=96=E2=80=9C=E5=88=9D?=
=?UTF-8?q?=E5=A7=8B=E5=8C=96=E9=9D=99=E6=80=81map=E2=80=9D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 3 ++-
contents/How-can-I-Initialize-a-static-Map.md | 12 +++++++-----
2 files changed, 9 insertions(+), 6 deletions(-)
diff --git a/README.md b/README.md
index 746e42c..6ff0671 100644
--- a/README.md
+++ b/README.md
@@ -41,6 +41,8 @@ stackoverflow-Java-top-qa
* [去掉烦人的"!=null"(判空语句](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/avoiding-null-statements-in-java.md)
* [获取完整的堆栈信息](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/get-current-stack-trace-in-java.md)
* [如何用一行代码初始化一个ArrayList](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/initialization-of-an-arraylist-in-one-line.md)
+* [初始化静态map](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/How-can-I-Initialize-a-static-Map.md)
+
> 网络
@@ -92,7 +94,6 @@ stackoverflow-Java-top-qa
- [How can I permanently have line numbers in IntelliJ?](http://stackoverflow.com/questions/13751/how-can-i-permanently-have-line-numbers-in-intellij)
- [How do servlets work? Instantiation, shared variables and multithreading](http://stackoverflow.com/questions/3106452/how-do-servlets-work-instantiation-shared-variables-and-multithreading)
- [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)
-- [How can I Initialize a static Map?](http://stackoverflow.com/questions/507602/how-can-i-initialize-a-static-map)
- [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)
- [How can I generate an MD5 hash?](http://stackoverflow.com/questions/415953/how-can-i-generate-an-md5-hash)
- [Uncatchable ChuckNorrisException](http://stackoverflow.com/questions/13883166/uncatchable-chucknorrisexception)
diff --git a/contents/How-can-I-Initialize-a-static-Map.md b/contents/How-can-I-Initialize-a-static-Map.md
index cb502f4..71b83a5 100644
--- a/contents/How-can-I-Initialize-a-static-Map.md
+++ b/contents/How-can-I-Initialize-a-static-Map.md
@@ -4,7 +4,9 @@
怎么在Java中初始化一个静态的map
-**方法一**:静态初始化器
+我想到的两种方法如下,大家是否有更好的建议呢?
+
+**方法一**:static初始化器
**方法二**:实例初始化(匿名子类)
@@ -31,9 +33,9 @@
### 答案1 ###
-匿名子类初始化器是java的语法糖,我搞不明白为什么匿名类来初始化,而且如果create的类是final的话,它将不起作用
+匿名子类初始化器是java的语法糖,我搞不明白为什么要用匿名子类来初始化,而且,如果类是final的话,它将不起作用
-我创建固定大小图的时候使用static初始化器
+我使用static初始化器来创建一个固定长度的静态map
public class Test{
private static final Map myMap;
@@ -48,14 +50,14 @@
### 答案2 ###
-我喜欢用Guava(是 Collection 框架的增强和扩张)的方法初始化一个静态的,不可改变的map
+我喜欢用Guava(是 Collection 框架的增强)的方法初始化一个静态的,不可改变的map
static fianl Map myMap = ImmutablMap.of(
1,"one",
2, "two"
)
·
-当你的图entry的个数超过5个时候你就不能使用`ImmutableMap.of`可以试试`ImmutableMap.bulider()`
+当map的 entry个数超过5个时,你就不能使用`ImmutableMap.of`。可以试试`ImmutableMap.bulider()`
static fianl Map myMap = ImmutableMap.builder()
{
From df2d6acc4df182a10e5e7f9070dc8c6890207bdc Mon Sep 17 00:00:00 2001
From: lizeyang
Date: 2016年2月23日 18:21:48 +0800
Subject: [PATCH 033/195] =?UTF-8?q?=E4=BC=98=E5=8C=96=E2=80=9CHashMap?=
=?UTF-8?q?=E9=81=8D=E5=8E=86=E2=80=9D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
contents/iterate-through-a-hashmap.md | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/contents/iterate-through-a-hashmap.md b/contents/iterate-through-a-hashmap.md
index 8eb27c7..91807ac 100644
--- a/contents/iterate-through-a-hashmap.md
+++ b/contents/iterate-through-a-hashmap.md
@@ -1,6 +1,6 @@
# HashMap遍历 #
-在Java中有多种遍历HashMAp的方法。让我们回顾一下最常见的方法和它们各自的优缺点。由于所有的Map都实现了Map接口,所以接下来方法适用于所有Map接口的图的实现(如:HaspMap,TreeMap,LinkedMap,HashTable,etc)
+在Java中有多种遍历HashMAp的方法。让我们回顾一下最常见的方法和它们各自的优缺点。由于所有的Map都实现了Map接口,所以接下来方法适用于所有Map(如:HaspMap,TreeMap,LinkedMap,HashTable,etc)
## 方法#1 使用For-Each迭代entries ##
@@ -42,7 +42,7 @@
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
-不适用泛型
+不使用泛型
Map map = new HashMap();
Iterator entries = map.entrySet().iterator();
@@ -71,4 +71,7 @@
## 总结 ##
-如果你只需要使用key或者value使用方法#2,如果你坚持使用java的老版本(java 5 以前的版本)或者打算在迭代的时候移除entries使用方法#3,另外的话使用方法#1,尽量避免出现方法#1的情况
\ No newline at end of file
+如果你只需要使用key或者value使用方法#2,如果你坚持使用java的老版本(java 5 以前的版本)或者打算在迭代的时候移除entries,使用方法#3。其他情况请使用#1方法。避免使用#4方法。
+
+stackoverflow链接:
+http://stackoverflow.com/questions/1066589/iterate-through-a-hashmap
\ No newline at end of file
From fdee4815cab6f9f550c55d6782a3658b7e90b7f2 Mon Sep 17 00:00:00 2001
From: lizeyang
Date: 2016年2月23日 18:28:05 +0800
Subject: [PATCH 034/195] =?UTF-8?q?=E4=BC=98=E5=8C=96=E2=80=9C=E7=BB=993?=
=?UTF-8?q?=E4=B8=AA=E5=B8=83=E5=B0=94=E5=8F=98=E9=87=8F=EF=BC=8C=E5=BD=93?=
=?UTF-8?q?=E5=85=B6=E4=B8=AD=E6=9C=892=E4=B8=AA=E6=88=96=E8=80=852?=
=?UTF-8?q?=E4=B8=AA=E4=BB=A5=E4=B8=8A=E4=B8=BAtrue=E6=89=8D=E8=BF=94?=
=?UTF-8?q?=E5=9B=9Etrue=E2=80=9D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 1 +
...Check-if-at-least-two-out-of-three-booleans-are-true.md | 7 ++++---
2 files changed, 5 insertions(+), 3 deletions(-)
diff --git a/README.md b/README.md
index 6ff0671..c039605 100644
--- a/README.md
+++ b/README.md
@@ -42,6 +42,7 @@ stackoverflow-Java-top-qa
* [获取完整的堆栈信息](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/get-current-stack-trace-in-java.md)
* [如何用一行代码初始化一个ArrayList](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/initialization-of-an-arraylist-in-one-line.md)
* [初始化静态map](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/How-can-I-Initialize-a-static-Map.md)
+* [给3个布尔变量,当其中有2个或者2个以上为true才返回true](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Check-if-at-least-two-out-of-three-booleans-are-true.md)
> 网络
diff --git a/contents/Check-if-at-least-two-out-of-three-booleans-are-true.md b/contents/Check-if-at-least-two-out-of-three-booleans-are-true.md
index 07ac501..dbb84e2 100644
--- a/contents/Check-if-at-least-two-out-of-three-booleans-are-true.md
+++ b/contents/Check-if-at-least-two-out-of-three-booleans-are-true.md
@@ -1,8 +1,8 @@
-给3个布尔变量,当其中有2个或者2个以上为true菜返回true
+##给3个布尔变量,当其中有2个或者2个以上为true才返回true
===
-问题
+###问题
给3个boolean变量,a,b,c,当其中有2个或2个以上为true时才返回true?
-最笨的方法:
+* 最笨的方法:
```java
boolean atLeastTwo(boolean a, boolean b, boolean c)
{
@@ -36,4 +36,5 @@ boolean atLeastTwo(boolean a, boolean b, boolean c)
return a ? (b || c) : (b && c);
```
+
stackoverflow链接: http://stackoverflow.com/questions/3076078/check-if-at-least-two-out-of-three-booleans-are-true
From fac2c0dd9a0a30bf011f7931883bfeadbeb8ae80 Mon Sep 17 00:00:00 2001
From: lizeyang
Date: 2016年2月23日 18:28:30 +0800
Subject: [PATCH 035/195] =?UTF-8?q?=E4=BC=98=E5=8C=96=E2=80=9C=E7=BB=993?=
=?UTF-8?q?=E4=B8=AA=E5=B8=83=E5=B0=94=E5=8F=98=E9=87=8F=EF=BC=8C=E5=BD=93?=
=?UTF-8?q?=E5=85=B6=E4=B8=AD=E6=9C=892=E4=B8=AA=E6=88=96=E8=80=852?=
=?UTF-8?q?=E4=B8=AA=E4=BB=A5=E4=B8=8A=E4=B8=BAtrue=E6=89=8D=E8=BF=94?=
=?UTF-8?q?=E5=9B=9Etrue"?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../Check-if-at-least-two-out-of-three-booleans-are-true.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/contents/Check-if-at-least-two-out-of-three-booleans-are-true.md b/contents/Check-if-at-least-two-out-of-three-booleans-are-true.md
index dbb84e2..f2f6eb0 100644
--- a/contents/Check-if-at-least-two-out-of-three-booleans-are-true.md
+++ b/contents/Check-if-at-least-two-out-of-three-booleans-are-true.md
@@ -1,5 +1,5 @@
##给3个布尔变量,当其中有2个或者2个以上为true才返回true
-===
+
###问题
给3个boolean变量,a,b,c,当其中有2个或2个以上为true时才返回true?
* 最笨的方法:
From 8d1455368c4fc2f578c5a51d864639a213bf54da Mon Sep 17 00:00:00 2001
From: lizeyang
Date: 2016年2月23日 18:34:01 +0800
Subject: [PATCH 036/195] =?UTF-8?q?=E4=BC=98=E5=8C=96=E2=80=9CJava?=
=?UTF-8?q?=E4=B8=AD=E6=89=93=E5=8D=B0=E4=B8=80=E4=B8=AA=E6=95=B0=E7=BB=84?=
=?UTF-8?q?=E6=9C=80=E7=AE=80=E5=8D=95=E7=9A=84=E6=96=B9=E6=B3=95=E6=98=AF?=
=?UTF-8?q?=E4=BB=80=E4=B9=88=E2=80=9D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 1 +
contents/What's-the-simplest-way-to-print-a-Java-array.md | 4 ++--
2 files changed, 3 insertions(+), 2 deletions(-)
diff --git a/README.md b/README.md
index c039605..b78ddb8 100644
--- a/README.md
+++ b/README.md
@@ -43,6 +43,7 @@ stackoverflow-Java-top-qa
* [如何用一行代码初始化一个ArrayList](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/initialization-of-an-arraylist-in-one-line.md)
* [初始化静态map](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/How-can-I-Initialize-a-static-Map.md)
* [给3个布尔变量,当其中有2个或者2个以上为true才返回true](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Check-if-at-least-two-out-of-three-booleans-are-true.md)
+* [Java中打印一个数组最简单的方法是什么](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/What's-the-simplest-way-to-print-a-Java-array.md)
> 网络
diff --git a/contents/What's-the-simplest-way-to-print-a-Java-array.md b/contents/What's-the-simplest-way-to-print-a-Java-array.md
index 6e3ac63..85673d1 100644
--- a/contents/What's-the-simplest-way-to-print-a-Java-array.md
+++ b/contents/What's-the-simplest-way-to-print-a-Java-array.md
@@ -1,8 +1,8 @@
-java中打印一个数组最简单的方法是什么
+Java中打印一个数组最简单的方法是什么
===
问题
---
-在java中,数组没有重写toString()方法,所以我如果直接调用数组toStrign()方法的话,我会知道得到它的内存地址。像这样:
+在java中,数组没有重写toString()方法,所以我如果直接调用数组toStrign()方法的话,只会得到它的内存地址。像这样:
```java
int[] intArray = new int[] {1, 2, 3, 4, 5};
System.out.println(intArray); // 有时候会输出 '[I@3343c8b3'
From 6cfb7b5a7f26599535956945aa80447146955e24 Mon Sep 17 00:00:00 2001
From: yann
Date: 2016年2月23日 22:07:14 +0800
Subject: [PATCH 037/195] del invalid question, it's translated.
---
README.md | 1 -
1 file changed, 1 deletion(-)
diff --git a/README.md b/README.md
index 3c1cc6a..9aaa9e0 100644
--- a/README.md
+++ b/README.md
@@ -67,7 +67,6 @@ stackoverflow-Java-top-qa
- [Dealing with "java.lang.OutOfMemoryError: PermGen space" error](http://stackoverflow.com/questions/88235/dealing-with-java-lang-outofmemoryerror-permgen-space-error)
- ["implements Runnable" vs. "extends Thread"](http://stackoverflow.com/questions/541487/implements-runnable-vs-extends-thread)
- [Android SDK installation doesn't find JDK](http://stackoverflow.com/questions/4382178/android-sdk-installation-doesnt-find-jdk)
-- [Initialization of an ArrayList in one line](http://stackoverflow.com/questions/1005073/initialization-of-an-arraylist-in-one-line)
- [Java inner class and static nested class](http://stackoverflow.com/questions/70324/java-inner-class-and-static-nested-class)
- ['Must Override a Superclass Method' Errors after importing a project into Eclipse](http://stackoverflow.com/questions/1678122/must-override-a-superclass-method-errors-after-importing-a-project-into-eclips)
- [Declare array in Java?](http://stackoverflow.com/questions/1200621/declare-array-in-java)
From 2367fd16e70a8f60393a984d338a72bca0fcee5a Mon Sep 17 00:00:00 2001
From: yann
Date: 2016年2月23日 23:02:24 +0800
Subject: [PATCH 038/195] add question comparing-java-enum-members-or-equals
---
README.md | 1 +
.../comparing-java-enum-members-or-equals.md | 83 +++++++++++++++++++
2 files changed, 84 insertions(+)
create mode 100644 contents/comparing-java-enum-members-or-equals.md
diff --git a/README.md b/README.md
index 9aaa9e0..77340e8 100644
--- a/README.md
+++ b/README.md
@@ -32,6 +32,7 @@ stackoverflow-Java-top-qa
* [能否在一个构造器( `constructor` )中调用另一个构造器](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/How-do-i-call-one-constructor-from-another-in-java.md)
* [ `finally` 代码块总会被执行么](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Does-finally-always-execute-in-Java.md)
* [如何将String转换为enum](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/convert-a-string-to-an-enum-in-java.md)
+* [比较java枚举成员使用equal还是==](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/comparing-java-enum-members-or-equals.md)
> 编程技巧
diff --git a/contents/comparing-java-enum-members-or-equals.md b/contents/comparing-java-enum-members-or-equals.md
new file mode 100644
index 0000000..07fb49b
--- /dev/null
+++ b/contents/comparing-java-enum-members-or-equals.md
@@ -0,0 +1,83 @@
+## 比较java枚举成员使用equal还是==
+
+### 问题
+我知道Java枚举会被编译成私有构造参数和一堆静态方法的一个类,当去比较两个枚举的时候,总是使用equals()方法,例如:
+```java
+public useEnums(SomeEnum a)
+{
+ if(a.equals(SomeEnum.SOME_ENUM_VALUE))
+ {
+ ...
+ }
+ ...
+}
+```
+除此之外,我也可以使用 == 替代equals() 方法
+```java
+public useEnums2(SomeEnum a)
+{
+ if(a == SomeEnum.SOME_ENUM_VALUE)
+ {
+ ...
+ }
+ ...
+}
+```
+我已经Java编程5年以上了,并且我想我也懂得 == 和 equals() 之间的区别,但是我仍然觉得困扰在这个问题上,哪一个操作符才是我该使用的。
+
+### 答案
+
+二者皆对,如果你看过枚举的源码,你会发现在源码中,equals也仅仅非常简单的 == 。
+我使用 == ,无论如何,这个左值是可以为 null的
+
+
+译者补充 java.lang.Enum 中Equals 代码:
+```java
+public final boolean equals(Object other) {
+ return this==other;
+}
+```
+
+
+### 额外答案
+#### 能使用 == 在枚举判断中?
+答案是肯定的,因为枚举有着严格的实例化控制,所以你可以用 == 去比较实力,这在语言标准内也是有保证的。
+
+>JLS 8.9 Enums
+>An enum type has no instances other than those defined by its enum constants.
+>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.
+>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.)
+
+#### 什么时候 == 和 equals 不一样?
+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:
+通常来说 == 不是一个 equals的一个备选方案,无论如何有2个重要的不同处需要考虑:
+
+##### == 不会抛出 NullPointerException
+```java
+enum Color { BLACK, WHITE };
+
+Color nothing = null;
+if (nothing == Color.BLACK); // runs fine
+if (nothing.equals(Color.BLACK)); // throws NullPointerException
+```
+##### == 在编译期检测类型兼容性
+```java
+enum Color { BLACK, WHITE };
+enum Chiral { LEFT, RIGHT };
+
+if (Color.BLACK.equals(Chiral.LEFT)); // compiles fine
+if (Color.BLACK == Chiral.LEFT); // DOESN'T COMPILE!!! Incompatible types!
+```
+
+#### 什么时候使用 == ?
+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.
+具体来说,那些提供恰当实例控制的不可变类能够保证 == 是可用的,枚举刚好符合这个条件。
+
+> Item 1: Consider static factory methods instead of constructors
+[...] 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.
+
+总而言之,在枚举比较上使用 == , 因为:
+1. 能正常工作
+2. 更快
+3. 运行时是安全的
+4. 编译期也是安全的
\ No newline at end of file
From 8952e53550ad0f7bb957a18d82061e14d08bdedb Mon Sep 17 00:00:00 2001
From: lizeyang
Date: 2016年2月24日 11:00:18 +0800
Subject: [PATCH 039/195] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E2=80=9C=E5=A6=82?=
=?UTF-8?q?=E4=BD=95=E5=8F=96=E5=BE=97android=E5=94=AF=E4=B8=80=E7=A0=81?=
=?UTF-8?q?=E2=80=9D=E3=80=81=E2=80=9C=E4=B8=BA=E4=BB=80=E4=B9=88=E4=BB=A5?=
=?UTF-8?q?=E4=B8=8B=E7=94=A8=E9=9A=8F=E6=9C=BA=E7=94=9F=E6=88=90=E7=9A=84?=
=?UTF-8?q?=E6=96=87=E5=AD=97=E4=BC=9A=E5=BE=97=E5=87=BA=20=E2=80=9Chello?=
=?UTF-8?q?=20world=E2=80=9D"?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 13 +-
contents/Declare-array-in-Java.md | 1 -
.../is-there-a-unique-android-device-id.md | 115 ++++++++++++++++++
...-using-random-strings-print-hello-world.md | 64 ++++++++++
4 files changed, 185 insertions(+), 8 deletions(-)
create mode 100644 contents/is-there-a-unique-android-device-id.md
create mode 100644 contents/why-does-this-code-using-random-strings-print-hello-world.md
diff --git a/README.md b/README.md
index b78ddb8..d59958c 100644
--- a/README.md
+++ b/README.md
@@ -36,6 +36,7 @@ stackoverflow-Java-top-qa
* [反射是什么及其用途](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/What-is-reflection-and-why-is-it-useful.md.md)
* [为什么不能用string类型进行switch判断](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Why-can't-I-switch-on-a-String.md)
+
> 编程技巧
* [去掉烦人的"!=null"(判空语句](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/avoiding-null-statements-in-java.md)
@@ -44,7 +45,7 @@ stackoverflow-Java-top-qa
* [初始化静态map](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/How-can-I-Initialize-a-static-Map.md)
* [给3个布尔变量,当其中有2个或者2个以上为true才返回true](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Check-if-at-least-two-out-of-three-booleans-are-true.md)
* [Java中打印一个数组最简单的方法是什么](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/What's-the-simplest-way-to-print-a-Java-array.md)
-
+* [为什么以下用随机生成的文字会得出 "hello world"?](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/why-does-this-code-using-random-strings-print-hello-world.md)
> 网络
@@ -52,7 +53,8 @@ stackoverflow-Java-top-qa
> 性能
-* [LinkedList、ArrayList各自的使用场景,如何确认应该用哪一个呢?](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/when-to-use-linkedlist-over-arraylist.md)
+* [LinkedList、ArrayList各自的使用场景,如何确认应该用哪一个呢?](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/w
+* hen-to-use-linkedlist-over-arraylist.md)
* [StringBuilder和StringBuffer有哪些区别呢](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/stringbuilder-and-stringbuffer.md)
* [为什么处理排序的数组要比非排序的快](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Why-is-processing-a-sorted-array-faster-than-an-unsorted-array.md)
@@ -61,8 +63,8 @@ stackoverflow-Java-top-qa
* [如何测试 private 方法,变量或者内部类](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/How_to_test_a_class_that_has_private_methods,_fields_or_inner_classes.md)
> Android
-
-* [在Android里面下载文件,并在ProgressDialog显示进度](https://github.com/troyliu0105/stackoverflow-java-top-qa/blob/master/contents/Download_a_file_with_Android_and_showing_the_progress_in_a_ProgressDialog.md)
+* [在Android里面下载文件,并在ProgressDialog显示进度](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Download_a_file_with_Android_and_showing_the_progress_in_a_ProgressDialog.md)
+* [如何取得android唯一码?](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/is-there-a-unique-android-device-id.md)
### 待翻译问题链接(还剩x问题)
- [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)
@@ -70,15 +72,12 @@ stackoverflow-Java-top-qa
- [Creating a memory leak with Java [closed]](http://stackoverflow.com/questions/6470651/creating-a-memory-leak-with-java)
- [Why is char[] preferred over String for passwords?](http://stackoverflow.com/questions/8881291/why-is-char-preferred-over-string-for-passwords)
- [Why is printing "B" dramatically slower than printing "#"?](http://stackoverflow.com/questions/21947452/why-is-printing-b-dramatically-slower-than-printing)
-- [Is there a unique Android device ID?](http://stackoverflow.com/questions/2785485/is-there-a-unique-android-device-id)
-- [Why does this code using random strings print "hello world"?](http://stackoverflow.com/questions/15182496/why-does-this-code-using-random-strings-print-hello-world)
- [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)
- [How to avoid Java code in JSP files?](http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files)
- [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)
- [Dealing with "java.lang.OutOfMemoryError: PermGen space" error](http://stackoverflow.com/questions/88235/dealing-with-java-lang-outofmemoryerror-permgen-space-error)
- ["implements Runnable" vs. "extends Thread"](http://stackoverflow.com/questions/541487/implements-runnable-vs-extends-thread)
- [Android SDK installation doesn't find JDK](http://stackoverflow.com/questions/4382178/android-sdk-installation-doesnt-find-jdk)
-- [Initialization of an ArrayList in one line](http://stackoverflow.com/questions/1005073/initialization-of-an-arraylist-in-one-line)
- [Java inner class and static nested class](http://stackoverflow.com/questions/70324/java-inner-class-and-static-nested-class)
- ['Must Override a Superclass Method' Errors after importing a project into Eclipse](http://stackoverflow.com/questions/1678122/must-override-a-superclass-method-errors-after-importing-a-project-into-eclips)
- [Fastest way to determine if an integer's square root is an integer](http://stackoverflow.com/questions/295579/fastest-way-to-determine-if-an-integers-square-root-is-an-integer)
diff --git a/contents/Declare-array-in-Java.md b/contents/Declare-array-in-Java.md
index 0b8dfe6..0d5019a 100644
--- a/contents/Declare-array-in-Java.md
+++ b/contents/Declare-array-in-Java.md
@@ -23,4 +23,3 @@ String[] myStringArray = new String[]("a", "b", "c");
-
diff --git a/contents/is-there-a-unique-android-device-id.md b/contents/is-there-a-unique-android-device-id.md
new file mode 100644
index 0000000..7a0fbb8
--- /dev/null
+++ b/contents/is-there-a-unique-android-device-id.md
@@ -0,0 +1,115 @@
+##如何取得android唯一码?
+
+###问题
+每一个android装置都有唯一ID吗?如果有?怎么用java最简单取得呢?
+
+###回答1(最佳)
+
+如何取得android唯一码?
+好处:
+1.不需要特定权限.
+2.在99.5% Android装置(包括root过的)上,即API => 9,保证唯一性.
+3.重装app之后仍能取得相同唯一值.
+
+伪代码:
+
+```
+if API => 9/10: (99.5% of devices)
+
+return unique ID containing serial id (rooted devices may be different)
+
+else
+
+return unique ID of build information (may overlap data - API < 9) +``` + +代码: + +```java + +/** + * Return pseudo unique ID + * @return ID + */public static String getUniquePsuedoID() { + // If all else fails, if the user does have lower than API 9 (lower + // than Gingerbread), has reset their device or 'Secure.ANDROID_ID' + // returns 'null', then simply the ID returned will be solely based + // off their Android device information. This is where the collisions + // can happen. + // Thanks http://www.pocketmagic.net/?p=1662! + // Try not to use DISPLAY, HOST or ID - these items could change. + // If there are collisions, there will be overlapping data + String m_szDevIDShort = "35" + (Build.BOARD.length() % 10) + (Build.BRAND.length() % 10) + (Build.CPU_ABI.length() % 10) + (Build.DEVICE.length() % 10) + (Build.MANUFACTURER.length() % 10) + (Build.MODEL.length() % 10) + (Build.PRODUCT.length() % 10); + + // Thanks to @Roman SL! + // http://stackoverflow.com/a/4789483/950427 + // Only devices with API>= 9 have android.os.Build.SERIAL
+ // http://developer.android.com/reference/android/os/Build.html#SERIAL
+ // If a user upgrades software or roots their device, there will be a duplicate entry
+ String serial = null;
+ try {
+ serial = android.os.Build.class.getField("SERIAL").get(null).toString();
+
+ // Go ahead and return the serial for api => 9
+ return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
+ } catch (Exception exception) {
+ // String needs to be initialized
+ serial = "serial"; // some value
+ }
+
+ // Thanks @Joe!
+ // http://stackoverflow.com/a/2853253/950427
+ // Finally, combine the values we have found by using the UUID class to create a unique identifier
+ return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();}
+```
+###回答2
+好处:
+1.不需要特定权限.
+2.在100% Android装置(包括root过的)上,保证唯一性.
+
+坏处
+1.重装app之后不能取得相同唯一值.
+
+```java
+private static String uniqueID = null;
+private static final String PREF_UNIQUE_ID = "PREF_UNIQUE_ID";
+
+public synchronized static String id(Context context) {
+ if (uniqueID == null) {
+ SharedPreferences sharedPrefs = context.getSharedPreferences(
+ PREF_UNIQUE_ID, Context.MODE_PRIVATE);
+ uniqueID = sharedPrefs.getString(PREF_UNIQUE_ID, null);
+ if (uniqueID == null) {
+ uniqueID = UUID.randomUUID().toString();
+ Editor editor = sharedPrefs.edit();
+ editor.putString(PREF_UNIQUE_ID, uniqueID);
+ editor.commit();
+ }
+ }
+ return uniqueID;
+}
+```
+
+###回答3(需要有电话卡)
+
+好处:
+1.重装app之后仍能取得相同唯一值.
+
+代码:
+
+```java
+ final TelephonyManager tm = (TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
+ final String tmDevice, tmSerial, androidId;
+ tmDevice = "" + tm.getDeviceId();
+ tmSerial = "" + tm.getSimSerialNumber();
+ androidId = "" + android.provider.Settings.Secure.getString(getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
+ UUID deviceUuid = new UUID(androidId.hashCode(), ((long)tmDevice.hashCode() << 32) | tmSerial.hashCode()); + String deviceId = deviceUuid.toString(); +``` + +谨记:要取得以下权限 +``` +
+```
+stackoverflow链接:
+http://stackoverflow.com/questions/2785485/is-there-a-unique-android-device-id
\ No newline at end of file
diff --git a/contents/why-does-this-code-using-random-strings-print-hello-world.md b/contents/why-does-this-code-using-random-strings-print-hello-world.md
new file mode 100644
index 0000000..c546367
--- /dev/null
+++ b/contents/why-does-this-code-using-random-strings-print-hello-world.md
@@ -0,0 +1,64 @@
+##为什么以下用随机生成的文字会得出 "hello world"?
+
+###问题
+为什么以下用随机生成的文字会得出"hello world".
+有人能解释一下吗?
+
+```
+System.out.println(randomString(-229985452) + " " + randomString(-147909649));
+
+public static String randomString(int i)
+{
+ Random ran = new Random(i);
+ StringBuilder sb = new StringBuilder();
+ while (true)
+ {
+ int k = ran.nextInt(27);
+ if (k == 0)
+ break;
+
+ sb.append((char)('`' + k));
+ }
+ return sb.toString();
+}
+```
+###回答1(最佳)
+在JAVA 里面,随机类的实现不是真正的随机,是伪随机.
+就是说如果随机类的种子是一样的话,他们会生成同一组的数字。
+
+比如说这个问题:
+
+ new Random(-229985452).nextInt(27)
+
+首6个生成的数字一定是:
+
+ 8
+ 5
+ 12
+ 12
+ 15
+ 0
+
+
+而 `new Random(-147909649).nextInt(27)` 首6个生成的数字一定是:
+
+ 23
+ 15
+ 18
+ 12
+ 4
+ 0
+
+而把每一个数目字加 `
(which is 96),就会得到了相应的英文字母:
+
+ 8 + 96 = 104 --> h
+ 5 + 96 = 101 --> e
+ 12 + 96 = 108 --> l
+ 12 + 96 = 108 --> l
+ 15 + 96 = 111 --> o
+
+ 23 + 96 = 119 --> w
+ 15 + 96 = 111 --> o
+ 18 + 96 = 114 --> r
+ 12 + 96 = 108 --> l
+ 4 + 96 = 100 --> d
\ No newline at end of file
From 6d3b7562ecfe3065c117df2b6b496c100778bcee Mon Sep 17 00:00:00 2001
From: lizeyang
Date: 2016年2月24日 11:01:41 +0800
Subject: [PATCH 040/195] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dreadme=E6=96=87?=
=?UTF-8?q?=E6=A1=A3bug?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/README.md b/README.md
index d59958c..3662904 100644
--- a/README.md
+++ b/README.md
@@ -53,8 +53,7 @@ stackoverflow-Java-top-qa
> 性能
-* [LinkedList、ArrayList各自的使用场景,如何确认应该用哪一个呢?](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/w
-* hen-to-use-linkedlist-over-arraylist.md)
+* [LinkedList、ArrayList各自的使用场景,如何确认应该用哪一个呢?](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/when-to-use-linkedlist-over-arraylist.md)
* [StringBuilder和StringBuffer有哪些区别呢](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/stringbuilder-and-stringbuffer.md)
* [为什么处理排序的数组要比非排序的快](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Why-is-processing-a-sorted-array-faster-than-an-unsorted-array.md)
From c940bc0d0a7efa18a83e394313cd51930563ce85 Mon Sep 17 00:00:00 2001
From: lizeyang
Date: 2016年2月24日 11:25:02 +0800
Subject: [PATCH 041/195] =?UTF-8?q?=E4=BC=98=E5=8C=96=E2=80=9C=E5=A6=82?=
=?UTF-8?q?=E4=BD=95=E8=8E=B7=E5=8F=96Android=E8=AE=BE=E5=A4=87=E5=94=AF?=
=?UTF-8?q?=E4=B8=80ID=E2=80=9D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 2 +-
contents/is-there-a-unique-android-device-id.md | 17 +++++++++--------
2 files changed, 10 insertions(+), 9 deletions(-)
diff --git a/README.md b/README.md
index 3662904..800dd68 100644
--- a/README.md
+++ b/README.md
@@ -63,7 +63,7 @@ stackoverflow-Java-top-qa
> Android
* [在Android里面下载文件,并在ProgressDialog显示进度](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Download_a_file_with_Android_and_showing_the_progress_in_a_ProgressDialog.md)
-* [如何取得android唯一码?](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/is-there-a-unique-android-device-id.md)
+* [如何获取Android设备唯一ID](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/is-there-a-unique-android-device-id.md)
### 待翻译问题链接(还剩x问题)
- [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)
diff --git a/contents/is-there-a-unique-android-device-id.md b/contents/is-there-a-unique-android-device-id.md
index 7a0fbb8..4853256 100644
--- a/contents/is-there-a-unique-android-device-id.md
+++ b/contents/is-there-a-unique-android-device-id.md
@@ -1,15 +1,16 @@
-##如何取得android唯一码?
+##如何获取Android设备唯一ID?
###问题
-每一个android装置都有唯一ID吗?如果有?怎么用java最简单取得呢?
+每一个android设备都有唯一ID吗?如果有?怎么用java最简单取得呢?
###回答1(最佳)
如何取得android唯一码?
+
好处:
-1.不需要特定权限.
-2.在99.5% Android装置(包括root过的)上,即API => 9,保证唯一性.
-3.重装app之后仍能取得相同唯一值.
+- 1.不需要特定权限.
+- 2.在99.5% Android装置(包括root过的)上,即API => 9,保证唯一性.
+- 3.重装app之后仍能取得相同唯一值.
伪代码:
@@ -64,11 +65,11 @@ return unique ID of build information (may overlap data - API < 9) ``` ###回答2 好处: -1.不需要特定权限. -2.在100% Android装置(包括root过的)上,保证唯一性. +- 1.不需要特定权限. +- 2.在100% Android装置(包括root过的)上,保证唯一性. 坏处 -1.重装app之后不能取得相同唯一值. +- 1.重装app之后不能取得相同唯一值. ```java private static String uniqueID = null; From b8a64ab2d27332b8f642d10b378db5bed1138948 Mon Sep 17 00:00:00 2001 From: lizeyang
Date: 2016年2月24日 11:27:30 +0800
Subject: [PATCH 042/195] =?UTF-8?q?=E4=BC=98=E5=8C=96=E2=80=9C=E4=B8=BA?=
=?UTF-8?q?=E4=BB=80=E4=B9=88=E4=BB=A5=E4=B8=8B=E7=94=A8=E9=9A=8F=E6=9C=BA?=
=?UTF-8?q?=E7=94=9F=E6=88=90=E7=9A=84=E6=96=87=E5=AD=97=E4=BC=9A=E5=BE=97?=
=?UTF-8?q?=E5=87=BA=20=E2=80=9Chello=20world=E2=80=9D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...y-does-this-code-using-random-strings-print-hello-world.md | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/contents/why-does-this-code-using-random-strings-print-hello-world.md b/contents/why-does-this-code-using-random-strings-print-hello-world.md
index c546367..d1d4850 100644
--- a/contents/why-does-this-code-using-random-strings-print-hello-world.md
+++ b/contents/why-does-this-code-using-random-strings-print-hello-world.md
@@ -61,4 +61,6 @@ public static String randomString(int i)
15 + 96 = 111 --> o
18 + 96 = 114 --> r
12 + 96 = 108 --> l
- 4 + 96 = 100 --> d
\ No newline at end of file
+ 4 + 96 = 100 --> d
+
+stackoverflow链接:http://stackoverflow.com/questions/15182496/why-does-this-code-using-random-strings-print-hello-world
\ No newline at end of file
From fea342ee62640595b7a994fc4fb2a08c1d6669fb Mon Sep 17 00:00:00 2001
From: lizeyang
Date: 2016年2月24日 11:33:18 +0800
Subject: [PATCH 043/195] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E2=80=9C=E4=BB=80?=
=?UTF-8?q?=E4=B9=88=E5=9C=A8java=E4=B8=AD=E5=AD=98=E6=94=BE=E5=AF=86?=
=?UTF-8?q?=E7=A0=81=E6=9B=B4=E5=80=BE=E5=90=91=E4=BA=8Echar[]=E8=80=8C?=
=?UTF-8?q?=E4=B8=8D=E6=98=AFString=E2=80=9D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 4 ++--
...preferred-over-String-for-passwords-in-java.md | 15 +++++++++++++++
2 files changed, 17 insertions(+), 2 deletions(-)
create mode 100644 contents/why-is-cha[]-preferred-over-String-for-passwords-in-java.md
diff --git a/README.md b/README.md
index 800dd68..bff5d1a 100644
--- a/README.md
+++ b/README.md
@@ -36,7 +36,6 @@ stackoverflow-Java-top-qa
* [反射是什么及其用途](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/What-is-reflection-and-why-is-it-useful.md.md)
* [为什么不能用string类型进行switch判断](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Why-can't-I-switch-on-a-String.md)
-
> 编程技巧
* [去掉烦人的"!=null"(判空语句](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/avoiding-null-statements-in-java.md)
@@ -46,6 +45,7 @@ stackoverflow-Java-top-qa
* [给3个布尔变量,当其中有2个或者2个以上为true才返回true](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Check-if-at-least-two-out-of-three-booleans-are-true.md)
* [Java中打印一个数组最简单的方法是什么](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/What's-the-simplest-way-to-print-a-Java-array.md)
* [为什么以下用随机生成的文字会得出 "hello world"?](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/why-does-this-code-using-random-strings-print-hello-world.md)
+* [什么在java中存放密码更倾向于char[]而不是String](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/why-is-cha[]-preferred-over-String-for-passwords-in-java.md)
> 网络
@@ -62,6 +62,7 @@ stackoverflow-Java-top-qa
* [如何测试 private 方法,变量或者内部类](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/How_to_test_a_class_that_has_private_methods,_fields_or_inner_classes.md)
> Android
+
* [在Android里面下载文件,并在ProgressDialog显示进度](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Download_a_file_with_Android_and_showing_the_progress_in_a_ProgressDialog.md)
* [如何获取Android设备唯一ID](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/is-there-a-unique-android-device-id.md)
@@ -69,7 +70,6 @@ stackoverflow-Java-top-qa
- [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)
- [Proper use cases for Android UserManager.isUserAGoat()?](http://stackoverflow.com/questions/13375357/proper-use-cases-for-android-usermanager-isuseragoat)
- [Creating a memory leak with Java [closed]](http://stackoverflow.com/questions/6470651/creating-a-memory-leak-with-java)
-- [Why is char[] preferred over String for passwords?](http://stackoverflow.com/questions/8881291/why-is-char-preferred-over-string-for-passwords)
- [Why is printing "B" dramatically slower than printing "#"?](http://stackoverflow.com/questions/21947452/why-is-printing-b-dramatically-slower-than-printing)
- [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)
- [How to avoid Java code in JSP files?](http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files)
diff --git a/contents/why-is-cha[]-preferred-over-String-for-passwords-in-java.md b/contents/why-is-cha[]-preferred-over-String-for-passwords-in-java.md
new file mode 100644
index 0000000..79a9ecf
--- /dev/null
+++ b/contents/why-is-cha[]-preferred-over-String-for-passwords-in-java.md
@@ -0,0 +1,15 @@
+## 为什么在java中存放密码更倾向于char[]相对于String
+
+### 问题
+
+在Swing中,password字段有一个getPassword()方法(返回char[]),而不是通常的getText()方法(返回字符串)。同样的,我遇到一个建议不要使用字符串处理密码。
+为什么在谈论passwords时,认为字符串会对安全构成威胁?感觉使用char[]不是那么的方便。
+
+### 回答
+String是不可变的。这意味着,一旦你创建了一个String,如果另一个线程可以进行内存转存,在GC回收之前,没有办法可以摆脱数据(除了反射)。(这段翻译的不好,希望大家帮助改正)
+然而对于数组,你可以在使用完就明确的擦除它,你可以用任何你喜欢的数据覆盖这个数组,而且password不会出现在系统的任何地方,甚至在垃圾回收之前。
+所以,这是一个安全性的问题--但是,即使使用char[]也仅仅是降低了攻击者攻击的机会,而且仅仅对这种特定的攻击有效。
+编辑:正如评论中指出的,垃圾收集器在移动数组数据时可能会在内存中留下杂散的数据副本。我认为这是特定于实现的--GC会清除所有的将要清除的数据,避免这种情况。即使是这样,还是会存在char[]保存有password字段的时间可以被攻击。
+
+**stackoverflow链接**:
+http://stackoverflow.com/questions/8881291/why-is-char-preferred-over-string-for-passwords-in-java
From f756ffa62d066091d2fa7aa9b41fa10b5094f9be Mon Sep 17 00:00:00 2001
From: lizeyang
Date: 2016年2月24日 11:53:31 +0800
Subject: [PATCH 044/195] =?UTF-8?q?=E4=BC=98=E5=8C=96=E2=80=9C=E4=B8=BA?=
=?UTF-8?q?=E4=BB=80=E4=B9=88=E5=9C=A8java=E4=B8=AD=E5=AD=98=E6=94=BE?=
=?UTF-8?q?=E5=AF=86=E7=A0=81=E6=9B=B4=E5=80=BE=E5=90=91=E4=BA=8Echar[]?=
=?UTF-8?q?=E8=80=8C=E4=B8=8D=E6=98=AFString=E2=80=9D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...referred-over-String-for-passwords-in-java.md | 16 ++++++++++------
1 file changed, 10 insertions(+), 6 deletions(-)
diff --git a/contents/why-is-cha[]-preferred-over-String-for-passwords-in-java.md b/contents/why-is-cha[]-preferred-over-String-for-passwords-in-java.md
index 79a9ecf..6a9ed50 100644
--- a/contents/why-is-cha[]-preferred-over-String-for-passwords-in-java.md
+++ b/contents/why-is-cha[]-preferred-over-String-for-passwords-in-java.md
@@ -1,15 +1,19 @@
-## 为什么在java中存放密码更倾向于char[]相对于String
+## 为什么在java中存放密码更倾向于char[]而不是String
### 问题
-在Swing中,password字段有一个getPassword()方法(返回char[]),而不是通常的getText()方法(返回字符串)。同样的,我遇到一个建议不要使用字符串处理密码。
-为什么在谈论passwords时,认为字符串会对安全构成威胁?感觉使用char[]不是那么的方便。
+在Swing中,password字段有一个getPassword()方法(返回char[]),而不是通常的getText()方法(返回String字符串)。同样的,我看到一个建议说不要使用字符串处理密码。
+为什么在涉及passwords时,都说字符串会对安全构成威胁?感觉使用char[]不是那么的方便。
### 回答
-String是不可变的。这意味着,一旦你创建了一个String,如果另一个线程可以进行内存转存,在GC回收之前,没有办法可以摆脱数据(除了反射)。(这段翻译的不好,希望大家帮助改正)
-然而对于数组,你可以在使用完就明确的擦除它,你可以用任何你喜欢的数据覆盖这个数组,而且password不会出现在系统的任何地方,甚至在垃圾回收之前。
+String是不可变的。虽然String加载密码之后可以把这个变量扔掉,但是字符串并不会马上被GC回收,一但进程在GC执行到这个字符串之前被dump,dump出的的转储中就会含有这个明文的字符串。那如果我去"修改"这个字符串,比如把它赋一个新值,那么是不是就没有这个问题了?答案是否定的,因为String本身是不可修改的,任何基于String的修改函数都是返回一个新的字符串,原有的还会在内存里。
+
+然而对于数组,你可以在抛弃它之前直接修改掉它里面的内容或者置为乱码,密码就不会存在了。但是如果你什么也不做直接交给gc的话,也会存在上面一样的问题。
+
所以,这是一个安全性的问题--但是,即使使用char[]也仅仅是降低了攻击者攻击的机会,而且仅仅对这种特定的攻击有效。
-编辑:正如评论中指出的,垃圾收集器在移动数组数据时可能会在内存中留下杂散的数据副本。我认为这是特定于实现的--GC会清除所有的将要清除的数据,避免这种情况。即使是这样,还是会存在char[]保存有password字段的时间可以被攻击。
+
**stackoverflow链接**:
http://stackoverflow.com/questions/8881291/why-is-char-preferred-over-string-for-passwords-in-java
+**知乎上也有相关讨论**:
+https://www.zhihu.com/question/36734157
From d7041c214c7a901794a5c23bdc08a25e4b816a67 Mon Sep 17 00:00:00 2001
From: lizeyang
Date: 2016年2月24日 12:00:50 +0800
Subject: [PATCH 045/195] =?UTF-8?q?=E6=96=B0=E5=A2=9E"=E5=A6=82=E4=BD=95?=
=?UTF-8?q?=E9=81=BF=E5=85=8D=E5=9C=A8JSP=E6=96=87=E4=BB=B6=E4=B8=AD?=
=?UTF-8?q?=E4=BD=BF=E7=94=A8Java=E4=BB=A3=E7=A0=81"?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 2 +-
.../how-to-avoid-java-code-in-jsp-files.md | 173 ++++++++++++++++++
...erred-over-String-for-passwords-in-java.md | 1 +
3 files changed, 175 insertions(+), 1 deletion(-)
create mode 100644 contents/how-to-avoid-java-code-in-jsp-files.md
diff --git a/README.md b/README.md
index bff5d1a..f25b0ec 100644
--- a/README.md
+++ b/README.md
@@ -46,6 +46,7 @@ stackoverflow-Java-top-qa
* [Java中打印一个数组最简单的方法是什么](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/What's-the-simplest-way-to-print-a-Java-array.md)
* [为什么以下用随机生成的文字会得出 "hello world"?](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/why-does-this-code-using-random-strings-print-hello-world.md)
* [什么在java中存放密码更倾向于char[]而不是String](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/why-is-cha[]-preferred-over-String-for-passwords-in-java.md)
+* [如何避免在JSP文件中使用Java代码](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-to-avoid-java-code-in-jsp-files.md)
> 网络
@@ -72,7 +73,6 @@ stackoverflow-Java-top-qa
- [Creating a memory leak with Java [closed]](http://stackoverflow.com/questions/6470651/creating-a-memory-leak-with-java)
- [Why is printing "B" dramatically slower than printing "#"?](http://stackoverflow.com/questions/21947452/why-is-printing-b-dramatically-slower-than-printing)
- [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)
-- [How to avoid Java code in JSP files?](http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files)
- [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)
- [Dealing with "java.lang.OutOfMemoryError: PermGen space" error](http://stackoverflow.com/questions/88235/dealing-with-java-lang-outofmemoryerror-permgen-space-error)
- ["implements Runnable" vs. "extends Thread"](http://stackoverflow.com/questions/541487/implements-runnable-vs-extends-thread)
diff --git a/contents/how-to-avoid-java-code-in-jsp-files.md b/contents/how-to-avoid-java-code-in-jsp-files.md
new file mode 100644
index 0000000..c95cc31
--- /dev/null
+++ b/contents/how-to-avoid-java-code-in-jsp-files.md
@@ -0,0 +1,173 @@
+##如何避免在JSP文件中使用Java代码
+
+###问题
+如何避免在JSP文件中使用Java代码?
+
+我对Java EE不是很熟悉,我知道类似如下的三行代码
+```jsp
+<%= x+1 %>
+<%= request.getParameter("name") %>
+<%! counter++; %>
+```
+这三行代码是学校教的老式代码。在JSP 2,存在一些方法可以避免在JSP文件中使用Java代码。有人可以告诉我在JSP 2中如何避免使用Java代码吗,这些方法该如何使用?
+
+###回答
+在大约十年前,taglibs(比如JSTL)和EL(EL表达式,`${}`)诞生的时候,在JSP中使用scriptlets(类似`<% %>`)这种做法,就确实已经是不被鼓励使用的做法了。
+
+scriptlets 主要的缺点有:
+1. **重用性** :你不可以重用scriptlets
+2. **可替换性** :你不可以让scriptlets抽象化
+3. **面向对象能力** :你不可以使用继承或组合
+4. **调试性** :如果scriptlets中途抛出了异常,你只能获得一个空白页
+5. **可测试性** :scriptlets不能进行单元测试
+6. **可维护性** :(这句有些词语不确定)需要更多的时间去维护混合的/杂乱的/冲突的 代码逻辑
+
+Oracle自己也在 [JSP coding conventions](http://www.oracle.com/technetwork/articles/javase/code-convention-138726.html)一文中推荐在功能可以被标签库所替代的时候避免使用scriptlets语法。以下引用它提出的几个观点:
+
+> 在JSP 1.2规范中,强烈推荐使用JSTL来减少JSP scriptlets语法的使用。一个使用JSTL的页面,总得来说会更加地容易阅读和维护。
+
+>...
+
+>在任何可能的地方,当标签库能够提供相同的功能时,尽量避免使用JSP scriptlets语法。这会让页面更加容易阅读和维护,帮助将 业务逻辑 从 表现层逻辑 中分离,也会让页面往更符合JSP 2.0风格的方向发展(JSP 2.0规范中,支持但是极大弱化了JSP scriptlets语法)
+
+>...
+
+>本着适应 模型-显示层-控制器(MVC) 设计模式中关于减少业务逻辑层与显示层之间的耦合的精神,**JSP scriptlets语法不应该**被用来编写业务逻辑。相应的,JSP scriptlets语法在传送一些服务端返回的处理客户端请求的数据(也叫做 声明对象?。这里翻译得不清楚)的时候会被使用。尽管如此,使用一个controller servlet来处理或者用自定义标签来处理会更好。
+
+-----------
+
+**如何替换scriptlets语句,取决于代码/逻辑的目的。更常见的是,被替换的语句会被放在另外的一些更值得放的Java类里**(这里翻译得不一定清楚)
+
+* 如果你想在每个请求运行**相同的**Java代码,less-or-more regardless of the requested page(不会翻译),比如说 检查一个用户是否在登录状态,就要实现一个 过滤器,在doFilter()方法中编写正确的代码,例如
+
+```java
+public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
+ if (((HttpServletRequest) request).getSession().getAttribute("user") == null) {
+ ((HttpServletResponse) response).sendRedirect("login"); // Not logged in, redirect to login page.
+ } else {
+ chain.doFilter(request, response); // Logged in, just continue request.
+ }
+}
+```
+当你在``中做好恰当的地址映射,覆盖所有应该被覆盖的JSP文件,也就不需要再JSP文件中添加这些相同的Java代码
+
+----------------
+
+* 如果你想执行一些Java代码来**预处理**一个请求,例如,预加载某些从数据库加载的数据来显示在一些表格里,可能还会有一些查询参数,那么,实现一个Servlet,在doGet()方法里编写正确的代码,例如
+
+```java
+protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+ try {
+ List products = productService.list(); // Obtain all products.
+ request.setAttribute("products", products); // Store products in request scope.
+ request.getRequestDispatcher("/WEB-INF/products.jsp").forward(request, response); // Forward to JSP page to display them in a HTML table.
+ } catch (SQLException e) {
+ throw new ServletException("Retrieving products failed!", e);
+ }
+}
+```
+这个方法能够更方便地处理异常。The DB is not accessed in the midst of JSP rendering, but far before the JSP is been displayed.(不会翻译)在数据库抛出异常的时候,你还是有改变response的可能。在上面的例子,默认的500页会显示,你还可以改变`web.xml`的``来自定义异常处理错误页。
+
+----------
+
+* 如果你想执行一些Java代码来**后置处理(postprocess)**一个请求,例如处理表单提交,那么,实现一个Servlet,在doPost()里写上正确的代码:
+
+```java
+protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+ String username = request.getParameter("username");
+ String password = request.getParameter("password");
+ User user = userService.find(username, password);
+
+ if (user != null) {
+ request.getSession().setAttribute("user", user); // Login user.
+ response.sendRedirect("home"); // Redirect to home page.
+ } else {
+ request.setAttribute("message", "Unknown username/password. Please retry."); // Store error message in request scope.
+ request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response); // Forward to JSP page to redisplay login form with error.
+ }
+}
+```
+这个处理不同目标结果页的方法会比原来更加简单: 可以显示一个带有表单验证错误提示的表单(在这个特别的例子中,你可以用EL表达式`${message}`来显示错误提示),或者仅仅跳转到成功的页面
+
+---------
+
+* 如果你想执行一些Java代码来**控制**执行计划(control the execution plan) 和/或 request和response的目标,用[MVC模式](http://stackoverflow.com/questions/3541077/design-patterns-web-based-applications/3542297#3542297)实现一个Servlet,例如:
+```java
+protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+ try {
+ Action action = ActionFactory.getAction(request);
+ String view = action.execute(request, response);
+
+ if (view.equals(request.getPathInfo().substring(1)) {
+ request.getRequestDispatcher("/WEB-INF/" + view + ".jsp").forward(request, response);
+ } else {
+ response.sendRedirect(view);
+ }
+ } catch (Exception e) {
+ throw new ServletException("Executing action failed.", e);
+ }
+}
+```
+或者使用一些MVC框架例如[JSF](http://stackoverflow.com/tags/jsf/info), [Spring MVC](http://stackoverflow.com/tags/spring-mvc/info), [Wicket](http://stackoverflow.com/tags/wicket/info) end up with just a JSP/Facelets page and a Javabean class without the need for a custom servlet.(不知道如何翻译准确)
+
+---------
+
+* 如果你想执行一些Java代码来**控制JSP页面的执行流程(control the flow inside a JSP page)**,那么你需要使用一些(已经存在的)流程控制标签库,比如[JSTL core](http://docs.oracle.com/javaee/5/jstl/1.1/docs/tlddocs/c/tld-summary.html),例如,在一个表格显示`List`
+
+```java
+<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
+...
+
+
+ + ${product.name}
+ ${product.description}
+ ${product.price}
+
+
+
+```
+这些XML风格的标签可以很好地适应HTML代码,代码变得更好阅读(也因此更好地维护),相比于杂乱无章的scriptlets 的分支大括号(Where the heck does this closing brace belong to?"(到底这个结束大括号是属于哪个代码段的?))。一个简单的设置可以配置你的Web程序让在使用scriptlets 的时候自动抛出异常
+
+```xml
+
+
+ *.jsp
+ ing-invalid>true
+
+
+```
+
+在JSP的继承者[Facelets](http://stackoverflow.com/tags/facelets/info)里(Java EE提供的MVC框架[JSF](http://stackoverflow.com/tags/jsf/info)),已经**不**可能使用scriptlets语法了。这是一个让你强制使用"正确的方法"的方法
+
+-----------
+
+* 如果你想执行一些Java代码来在JSP中 **访问和显示** 一些"后端"数据,你需要使用EL(表达式),`${}`,例如,显示已经提交了的数值:
+
+```jsp
+
+```
+
+`${param.foo}`会显示`request.getParameter("foo")`这句话的输出结果。
+
+--------------
+
+* 如果你想在JSP直接执行一些工具类Java代码(典型的,一些public static方法),你需要定义它,并使用EL表达式函数。这是JSTL里的标准函数标签库,但是你也可以[轻松地创建自己需要的功能](http://docs.oracle.com/javaee/5/tutorial/doc/bnahq.html#bnaiq),下面是一个使用有用的`fn:escapeXml`来避免XSS攻击的例子。
+
+```java
+<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
+...
+
+```
+
+注意,XSS并不是Java/JSP/JSTL/EL/任何技术相关的东西,这个问题是**任何**Web应用程序都需要关心的问题,scriptlets 并没有为这个问题提供良好的解决方案,至少没有标准的Java API的解决方案。JSP的继承者Facelets内含了HTML转义功能,所以在Facelets里你不用担心XSS攻击的问题。
+
+See Also:
+* [JSP, Servlet, JSF的不同点在哪里?](http://stackoverflow.com/questions/2095397/what-is-the-difference-between-jsf-servlet-and-jsp/2097732#2097732)
+* [Servlet, ServletContext, HttpSession 和 HttpServletRequest/Response 是如何工作的?](http://stackoverflow.com/questions/3106452/java-servlet-instantiation-and-session-variables/3106909#3106909)
+* [JSP, Servlet and JDBC的基本MVC例子](http://stackoverflow.com/questions/5003142/jsp-using-mvc-and-jdbc)
+* [Java Web应用程序中的设计模式](http://stackoverflow.com/questions/3541077/design-patterns-web-based-applications/)
+* [JSP/Servlet中的隐藏功能](http://balusc.blogspot.com/2010/01/hidden-features-of-jspservlet.html)
+
+
+stackoverflow原址:http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files
\ No newline at end of file
diff --git a/contents/why-is-cha[]-preferred-over-String-for-passwords-in-java.md b/contents/why-is-cha[]-preferred-over-String-for-passwords-in-java.md
index 6a9ed50..f3ee5c1 100644
--- a/contents/why-is-cha[]-preferred-over-String-for-passwords-in-java.md
+++ b/contents/why-is-cha[]-preferred-over-String-for-passwords-in-java.md
@@ -15,5 +15,6 @@ String是不可变的。虽然String加载密码之后可以把这个变量扔
**stackoverflow链接**:
http://stackoverflow.com/questions/8881291/why-is-char-preferred-over-string-for-passwords-in-java
+
**知乎上也有相关讨论**:
https://www.zhihu.com/question/36734157
From 3803c6f751a23820e967dfc8c3cf15243b482880 Mon Sep 17 00:00:00 2001
From: lizeyang
Date: 2016年2月24日 14:30:46 +0800
Subject: [PATCH 046/195] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E7=BF=BB=E8=AF=91?=
=?UTF-8?q?=E8=A6=81=E6=B1=82?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/README.md b/README.md
index f25b0ec..317b4bf 100644
--- a/README.md
+++ b/README.md
@@ -8,6 +8,13 @@ stackoverflow-Java-top-qa
对于参与翻译的人,这也是很好的一个学习、理解过程,欢迎大家一起来翻译
-------------
+### 如何参与翻译
+请从下文"待翻译问题链接"中寻找你感兴趣的问答进行翻译。翻译好的问答,放到contents目录下,无需更新readme.md文档。之后提一个PR,我会对你的翻译做一个审校,并更新到readme中。
+
+一些基本的约定:
+- 文档的文件名,和stackoverflow上的url保持一致。例如,http://stackoverflow.com/questions/8710619/java-operator 的文件名, 就是java-operator.md
+- 在每篇翻译文档内容的最后,要附上stackoverflow的原文链接
+
### 目录
> 基础语法
From c1115ee70ab04904151cf402f4b26ec77361c5b3 Mon Sep 17 00:00:00 2001
From: lizeyang
Date: 2016年2月25日 11:20:54 +0800
Subject: [PATCH 047/195] =?UTF-8?q?=E4=BC=98=E5=8C=96=E2=80=9C=E5=A6=82?=
=?UTF-8?q?=E4=BD=95=E9=81=BF=E5=85=8D=E5=9C=A8JSP=E6=96=87=E4=BB=B6?=
=?UTF-8?q?=E4=B8=AD=E4=BD=BF=E7=94=A8Java=E4=BB=A3=E7=A0=81=E2=80=9D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
contents/how-to-avoid-java-code-in-jsp-files.md | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/contents/how-to-avoid-java-code-in-jsp-files.md b/contents/how-to-avoid-java-code-in-jsp-files.md
index c95cc31..d0fc7e5 100644
--- a/contents/how-to-avoid-java-code-in-jsp-files.md
+++ b/contents/how-to-avoid-java-code-in-jsp-files.md
@@ -32,13 +32,13 @@ Oracle自己也在 [JSP coding conventions](http://www.oracle.com/technetwork/ar
>...
->本着适应 模型-显示层-控制器(MVC) 设计模式中关于减少业务逻辑层与显示层之间的耦合的精神,**JSP scriptlets语法不应该**被用来编写业务逻辑。相应的,JSP scriptlets语法在传送一些服务端返回的处理客户端请求的数据(也叫做 声明对象?。这里翻译得不清楚)的时候会被使用。尽管如此,使用一个controller servlet来处理或者用自定义标签来处理会更好。
+>本着适应 模型-显示层-控制器(MVC) 设计模式中关于减少业务逻辑层与显示层之间的耦合的精神,**JSP scriptlets语法不应该**被用来编写业务逻辑。相应的,JSP scriptlets语法在传送一些服务端返回的处理客户端请求的数据(也称为value objects)的时候会被使用。尽管如此,使用一个controller servlet来处理或者用自定义标签来处理会更好。
-----------
**如何替换scriptlets语句,取决于代码/逻辑的目的。更常见的是,被替换的语句会被放在另外的一些更值得放的Java类里**(这里翻译得不一定清楚)
-* 如果你想在每个请求运行**相同的**Java代码,less-or-more regardless of the requested page(不会翻译),比如说 检查一个用户是否在登录状态,就要实现一个 过滤器,在doFilter()方法中编写正确的代码,例如
+* 如果你想在每个请求、每个页面请求都运行**相同的**Java代码,,比如说 检查一个用户是否在登录状态,就要实现一个 过滤器,在doFilter()方法中编写正确的代码,例如
```java
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
@@ -66,7 +66,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
}
}
```
-这个方法能够更方便地处理异常。The DB is not accessed in the midst of JSP rendering, but far before the JSP is been displayed.(不会翻译)在数据库抛出异常的时候,你还是有改变response的可能。在上面的例子,默认的500页会显示,你还可以改变`web.xml`的``来自定义异常处理错误页。
+这个方法能够更方便地处理异常。这样会在渲染、展示JSP页面时访问数据库。在数据库抛出异常的时候,你可以根据情况返回不同的响应或页面。在上面的例子,出错时默认会展示500页面,你也可以改变`web.xml`的``来自定义异常处理错误页。
----------
@@ -91,7 +91,7 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
---------
-* 如果你想执行一些Java代码来**控制**执行计划(control the execution plan) 和/或 request和response的目标,用[MVC模式](http://stackoverflow.com/questions/3541077/design-patterns-web-based-applications/3542297#3542297)实现一个Servlet,例如:
+* 如果你想执行一些Java代码来**控制**执行计划(control the execution plan) 和/或 request和response的跳转目标,用[MVC模式](http://stackoverflow.com/questions/3541077/design-patterns-web-based-applications/3542297#3542297)实现一个Servlet,例如:
```java
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
@@ -108,11 +108,11 @@ protected void service(HttpServletRequest request, HttpServletResponse response)
}
}
```
-或者使用一些MVC框架例如[JSF](http://stackoverflow.com/tags/jsf/info), [Spring MVC](http://stackoverflow.com/tags/spring-mvc/info), [Wicket](http://stackoverflow.com/tags/wicket/info) end up with just a JSP/Facelets page and a Javabean class without the need for a custom servlet.(不知道如何翻译准确)
+或者使用一些MVC框架例如[JSF](http://stackoverflow.com/tags/jsf/info), [Spring MVC](http://stackoverflow.com/tags/spring-mvc/info), [Wicket](http://stackoverflow.com/tags/wicket/info) 这样你就不用自定义servlet,只要写一些页面和javabean class就可以了。
---------
-* 如果你想执行一些Java代码来**控制JSP页面的执行流程(control the flow inside a JSP page)**,那么你需要使用一些(已经存在的)流程控制标签库,比如[JSTL core](http://docs.oracle.com/javaee/5/jstl/1.1/docs/tlddocs/c/tld-summary.html),例如,在一个表格显示`List`
+* 如果你想执行一些Java代码来**控制JSP页面的数据渲染流程(control the flow inside a JSP page)**,那么你需要使用一些(已经存在的)流程控制标签库,比如[JSTL core](http://docs.oracle.com/javaee/5/jstl/1.1/docs/tlddocs/c/tld-summary.html),例如,在一个表格显示`List`
```java
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
From 91656967f99b869f430557793cb4600e767efdac Mon Sep 17 00:00:00 2001
From: lizeyang
Date: 2016年2月25日 11:45:08 +0800
Subject: [PATCH 048/195] =?UTF-8?q?=E4=BC=98=E5=8C=96=E2=80=9C=E6=AF=94?=
=?UTF-8?q?=E8=BE=83java=E6=9E=9A=E4=B8=BE=E6=88=90=E5=91=98=E4=BD=BF?=
=?UTF-8?q?=E7=94=A8equal=E8=BF=98=E6=98=AF=3D=3D=E2=80=9D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
contents/comparing-java-enum-members-or-equals.md | 14 ++++++++------
1 file changed, 8 insertions(+), 6 deletions(-)
diff --git a/contents/comparing-java-enum-members-or-equals.md b/contents/comparing-java-enum-members-or-equals.md
index 07fb49b..287f68c 100644
--- a/contents/comparing-java-enum-members-or-equals.md
+++ b/contents/comparing-java-enum-members-or-equals.md
@@ -1,7 +1,7 @@
## 比较java枚举成员使用equal还是==
### 问题
-我知道Java枚举会被编译成私有构造参数和一堆静态方法的一个类,当去比较两个枚举的时候,总是使用equals()方法,例如:
+我知道Java枚举会被编译成一个包含私有构造参数和一堆静态方法的类,当去比较两个枚举的时候,总是使用equals()方法,例如:
```java
public useEnums(SomeEnum a)
{
@@ -23,12 +23,12 @@ public useEnums2(SomeEnum a)
...
}
```
-我已经Java编程5年以上了,并且我想我也懂得 == 和 equals() 之间的区别,但是我仍然觉得困扰在这个问题上,哪一个操作符才是我该使用的。
+我有5年以上的java编程经验,并且我想我也懂得 == 和 equals() 之间的区别,但是我仍然觉得很困惑,哪一个操作符才是我该使用的。
### 答案
二者皆对,如果你看过枚举的源码,你会发现在源码中,equals也仅仅非常简单的 == 。
-我使用 == ,无论如何,这个左值是可以为 null的
+我使用 == ,因为无论如何,这个左值是可以为 null的
译者补充 java.lang.Enum 中Equals 代码:
@@ -40,8 +40,8 @@ public final boolean equals(Object other) {
### 额外答案
-#### 能使用 == 在枚举判断中?
-答案是肯定的,因为枚举有着严格的实例化控制,所以你可以用 == 去比较实力,这在语言标准内也是有保证的。
+#### 能在枚举中使用 == 进行判断?
+答案是肯定的,因为枚举有着严格的实例化控制,所以你可以用 == 去做比较符,这个用法,在官方文档中也有明确的说明。
>JLS 8.9 Enums
>An enum type has no instances other than those defined by its enum constants.
@@ -80,4 +80,6 @@ Bloch specifically mentions that immutable classes that have proper control over
1. 能正常工作
2. 更快
3. 运行时是安全的
-4. 编译期也是安全的
\ No newline at end of file
+4. 编译期也是安全的
+
+stackoverlfow链接:http://stackoverflow.com/questions/1750435/comparing-java-enum-members-or-equals
From 14c797f5dfc30d4fc74d4efbfd0e050f5c3549b0 Mon Sep 17 00:00:00 2001
From: lizeyang
Date: 2016年2月25日 16:34:05 +0800
Subject: [PATCH 049/195] =?UTF-8?q?=E4=BC=98=E5=8C=96=E2=80=9C=E5=A6=82?=
=?UTF-8?q?=E4=BD=95=E4=BA=A7=E7=94=9F=E4=B8=80=E4=B8=AA=E9=9A=8F=E6=9C=BA?=
=?UTF-8?q?=E7=9A=84=E5=AD=97=E6=AF=8D=E6=95=B0=E5=AD=97=E4=B8=B2=E4=BD=9C?=
=?UTF-8?q?=E4=B8=BA=20session=20=E7=9A=84=E5=94=AF=E4=B8=80=E6=A0=87?=
=?UTF-8?q?=E8=AF=86=E7=AC=A6=E2=80=9D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 1 +
..._generate_a_random_alpha-numeric_string.md | 54 +++++++++++++++++++
2 files changed, 55 insertions(+)
create mode 100644 contents/How_to_generate_a_random_alpha-numeric_string.md
diff --git a/README.md b/README.md
index 17193b7..51ebc87 100644
--- a/README.md
+++ b/README.md
@@ -56,6 +56,7 @@ stackoverflow-Java-top-qa
* [什么在java中存放密码更倾向于char[]而不是String](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/why-is-cha[]-preferred-over-String-for-passwords-in-java.md)
* [如何避免在JSP文件中使用Java代码](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-to-avoid-java-code-in-jsp-files.md)
* [Java 源码里的设计模式](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Examples_of_GoF_Design_Patterns_in_Java's_core_libraries.md)
+* [如何产生一个随机的字母数字串作为 session 的唯一标识符](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/How_to_generate_a_random_alpha-numeric_string.md)
> 网络
diff --git a/contents/How_to_generate_a_random_alpha-numeric_string.md b/contents/How_to_generate_a_random_alpha-numeric_string.md
new file mode 100644
index 0000000..79906f6
--- /dev/null
+++ b/contents/How_to_generate_a_random_alpha-numeric_string.md
@@ -0,0 +1,54 @@
+# 如何产生一个随机的字母数字串作为 session 的唯一标识符?
+
+如果允许产生的随机字符串是可猜测的(随机字符串比较都短,或者使用有缺陷的随机数生成器),进而导致攻击者可能会劫持到会话的,可以使用一个相对简单随机数生成代码,如下所示:
+```
+public class RandomString {
+
+ private static final char[] symbols;
+
+ static {
+ StringBuilder tmp = new StringBuilder();
+ for (char ch = '0'; ch <= '9'; ++ch) + tmp.append(ch); + for (char ch = 'a'; ch <= 'z'; ++ch) + tmp.append(ch); + symbols = tmp.toString().toCharArray(); + } + + private final Random random = new Random(); + + private final char[] buf; + + public RandomString(int length) { + if (length < 1) + throw new IllegalArgumentException("length < 1: " + length); + buf = new char[length]; + } + + public String nextString() { + for (int idx = 0; idx < buf.length; ++idx) + buf[idx] = symbols[random.nextInt(symbols.length)]; + return new String(buf); + } +} +``` + +为了安全,可以考虑使用下面这段简洁且安全的代码,不过用其作为 session 的标识符,倒显得有点大材小用了(比较耗时): +``` +import java.security.SecureRandom; + +public final class SessionIdentifierGenerator { + private SecureRandom random = new SecureRandom(); + + public String nextSessionId() { + return new BigInteger(130, random).toString(32); + } +} +``` + +其工作原理就是,使用一个 130 位的安全的随机数生成器生成一个随机数,接着转化为 32 进制。我们知道,128 位安全随机数的生成已经是足够安全的,不过以 32 进制编码的每一个数字可编码 5 位,所以需要取大于 128 且是 5 的倍数,所以就选择了 130 位。相对于 随机 UUID 来说(在标准输出中,每个字符使用 3.4 bit,共 122 bit),每个字符使用 5 个随机的 bit 来编码的方式,显得更为简洁和高效。 + +译者注:上面两段代码,生成26位随机字符串,第一段代码每次耗时不到1ms,第二段耗时约100ms。也就是说第一段代码更快,但第二段代码更安全,但更耗时。 + +stackoverflow原链接: +http://stackoverflow.com/questions/41107/how-to-generate-a-random-alpha-numeric-string \ No newline at end of file From 059ed8f3dd407ba88ea559928ad8a733611afc7b Mon Sep 17 00:00:00 2001 From: lizeyang
Date: 2016年2月25日 16:39:45 +0800
Subject: [PATCH 050/195] =?UTF-8?q?=E4=BC=98=E5=8C=96=E2=80=9CserialVersio?=
=?UTF-8?q?nUID=20=E6=9C=89=E4=BB=80=E4=B9=88=E4=BD=9C=E7=94=A8=EF=BC=9F?=
=?UTF-8?q?=E8=AF=A5=E5=A6=82=E4=BD=95=E4=BD=BF=E7=94=A8=E2=80=9D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
contents/What_is_a_serialVersionUID_and_why_should_I_use_it.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/contents/What_is_a_serialVersionUID_and_why_should_I_use_it.md b/contents/What_is_a_serialVersionUID_and_why_should_I_use_it.md
index c9ee44c..192d129 100644
--- a/contents/What_is_a_serialVersionUID_and_why_should_I_use_it.md
+++ b/contents/What_is_a_serialVersionUID_and_why_should_I_use_it.md
@@ -15,8 +15,8 @@ serialVersionUID 是实现 Serializable 接口而来的,而 Serializable 则
```
ANY-ACCESS-MODIFIER static final long serialVersionUID = 1L;
```
+ 当显式定义 serialVersionUID 的值时,Java 根据类的多个方面(具体可参考 Java 序列化规范)动态生成一个默认的 serialVersionUID 。尽管这样,还是建议你在每一个序列化的类中显式指定 serialVersionUID 的值,因为不同的 jdk 编译很可能会生成不同的 serialVersionUID 默认值,进而导致在反序列化时抛出 InvalidClassExceptions 异常。所以,为了保证在不同的 jdk 编译实现中,其 serialVersionUID 的值也一致,可序列化的类必须显式指定 serialVersionUID 的值。另外,serialVersionUID 的修饰符最好是 private,因为 serialVersionUID 不能被继承,所以建议使用 private 修饰 serialVersionUID。
-文档上还建议将 serialVersionUID 置为 private。
举例说明如下:
现在尝试通过将一个类 Person 序列化到磁盘和反序列化来说明 serialVersionUID 的作用: Person 类如下:
From 77c0a54f998ac2b3ea15557f0d2f0eff66534eb5 Mon Sep 17 00:00:00 2001
From: lizeyang
Date: 2016年2月25日 16:44:39 +0800
Subject: [PATCH 051/195] =?UTF-8?q?=E4=BC=98=E5=8C=96=E2=80=9C=E8=BE=93?=
=?UTF-8?q?=E5=87=BA=20Java=20=E6=95=B0=E7=BB=84=E6=9C=80=E7=AE=80?=
=?UTF-8?q?=E5=8D=95=E7=9A=84=E6=96=B9=E5=BC=8F=E2=80=9D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 2 +-
...What's-the-simplest-way-to-print-a-Java-array.md | 13 +++++--------
2 files changed, 6 insertions(+), 9 deletions(-)
diff --git a/README.md b/README.md
index 51ebc87..f668e69 100644
--- a/README.md
+++ b/README.md
@@ -51,7 +51,7 @@ stackoverflow-Java-top-qa
* [如何用一行代码初始化一个ArrayList](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/initialization-of-an-arraylist-in-one-line.md)
* [初始化静态map](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/How-can-I-Initialize-a-static-Map.md)
* [给3个布尔变量,当其中有2个或者2个以上为true才返回true](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Check-if-at-least-two-out-of-three-booleans-are-true.md)
-* [Java中打印一个数组最简单的方法是什么](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/What's-the-simplest-way-to-print-a-Java-array.md)
+* [输出 Java 数组最简单的方式](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/What's-the-simplest-way-to-print-a-Java-array.md)
* [为什么以下用随机生成的文字会得出 "hello world"?](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/why-does-this-code-using-random-strings-print-hello-world.md)
* [什么在java中存放密码更倾向于char[]而不是String](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/why-is-cha[]-preferred-over-String-for-passwords-in-java.md)
* [如何避免在JSP文件中使用Java代码](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-to-avoid-java-code-in-jsp-files.md)
diff --git a/contents/What's-the-simplest-way-to-print-a-Java-array.md b/contents/What's-the-simplest-way-to-print-a-Java-array.md
index 85673d1..f6428ab 100644
--- a/contents/What's-the-simplest-way-to-print-a-Java-array.md
+++ b/contents/What's-the-simplest-way-to-print-a-Java-array.md
@@ -1,17 +1,14 @@
-Java中打印一个数组最简单的方法是什么
+输出 Java 数组最简单的方式
===
问题
---
-在java中,数组没有重写toString()方法,所以我如果直接调用数组toStrign()方法的话,只会得到它的内存地址。像这样:
+因为 Java 数组中没有 toString() 方法,所以我如果直接调用数组toStrign()方法的话,只会得到它的内存地址。像这样,显得并不人性化:
```java
int[] intArray = new int[] {1, 2, 3, 4, 5};
System.out.println(intArray); // 有时候会输出 '[I@3343c8b3'
```
-但是实际上我想要的输出效果是
-```java
-[1, 2, 3, 4, 5]
-```
-所以打印一个数组最简单的方法是什么?我想要的效果是
+
+所以输出一个数组最简单的方法是什么?我想要的效果是
```java
// 数字数组:
int[] intArray = new int[] {1, 2, 3, 4, 5};
@@ -24,7 +21,7 @@ String[] strArray = new String[] {"John", "Mary", "Bob"};
回答
---
-在JAVA5中使用 Arrays.toString(arr) 或 Arrays.deepToString(arr)来打印数组。
+在 Java 5+ 以上中使用 Arrays.toString(arr) 或 Arrays.deepToString(arr)来打印(输出)数组。
不要忘了引入import java.util.Arrays;
```java
From 78af333c0ab51806906a63e5179b7e2c864d750a Mon Sep 17 00:00:00 2001
From: lizeyang
Date: 2016年2月25日 16:48:46 +0800
Subject: [PATCH 052/195] =?UTF-8?q?=E4=BC=98=E5=8C=96=E2=80=9C=E5=A6=82?=
=?UTF-8?q?=E4=BD=95=E5=88=9B=E5=BB=BA=E5=8D=95=E4=BE=8B=E2=80=9D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 1 +
...ent_way_to_implement_a_singleton_in_Java.md | 18 ++++++++++++++++++
2 files changed, 19 insertions(+)
diff --git a/README.md b/README.md
index f668e69..78e13d4 100644
--- a/README.md
+++ b/README.md
@@ -57,6 +57,7 @@ stackoverflow-Java-top-qa
* [如何避免在JSP文件中使用Java代码](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-to-avoid-java-code-in-jsp-files.md)
* [Java 源码里的设计模式](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Examples_of_GoF_Design_Patterns_in_Java's_core_libraries.md)
* [如何产生一个随机的字母数字串作为 session 的唯一标识符](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/How_to_generate_a_random_alpha-numeric_string.md)
+* [如何创建单例](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/What_is_an_efficient_way_to_implement_a_singleton_in_Java.md)
> 网络
diff --git a/contents/What_is_an_efficient_way_to_implement_a_singleton_in_Java.md b/contents/What_is_an_efficient_way_to_implement_a_singleton_in_Java.md
index f81fe32..b3e2d78 100644
--- a/contents/What_is_an_efficient_way_to_implement_a_singleton_in_Java.md
+++ b/contents/What_is_an_efficient_way_to_implement_a_singleton_in_Java.md
@@ -145,6 +145,24 @@ public enum Foo {
INSTANCE;
}
```
+08 年 google 开发者年会中,Joshua Bloch
+Joshua Bloch 在 [高效 Java 话题中](http://sites.google.com/site/io/effective-java-reloaded) 解释了这种方法,视频请戳 [这里](http://www.youtube.com/watch?v=pi_I7oD_uGI#t=28m50s).在 他[演讲的ppt](https://14b1424d-a-62cb3a1a-s-sites.googlegroups.com/site/io/effective-java-reloaded/effective_java_reloaded.pdf?attachauth=ANoY7crKCOet2NEUGW7RV1XfM-Jn4z8YJhs0qJM11OhLRnFW_JbExkJtvJ3UJvTE40dhAciyWcRIeGJ-n3FLGnMOapHShHINh8IY05YViOJoZWzaohMtM-s4HCi5kjREagi8awWtcYD0_6G7GhKr2BndToeqLk5sBhZcQfcYIyAE5A4lGNosDCjODcBAkJn8EuO6572t2wU1LMSEUgjvqcf4I-Fp6VDhDvih_XUEmL9nuVJQynd2DRpxyuNH1SpJspEIdbLw-WWZ&attredirects=0) 30-32 页提到:
+
+ 实现单例正确的方式如下:
+ ```
+ public enum Elvis {
+ INSTANCE;
+ private final String[] favoriteSongs =
+ { "Hound Dog", "Heartbreak Hotel" };
+ public void printFavorites() {
+ System.out.println(Arrays.toString(favoriteSongs));
+ }
+ }
+ ```
+
+在 [高效 Java 线上部分](http://www.ddj.com/java/208403883?pgno=3) 有说到:
+
+ 上述实现单例的方式,其实等同于,将 INSTANCE 设置为 public static final 的方式,不同之处在于,使用枚举的方式显得更为简洁,且默认提供了序列化机制,也保证了多线程访问的安全。虽然这种单例的实现方式还未被广泛使用,可实现单例的最好方式就是使用一个单元素的枚举。
为什么可以这么简洁?因为 Java 中每一个枚举类型都默认继承了 java.lang.Enum ,而 Enum 实现了 Serializable 接口,所以枚举类型对象都是默认可以被序列化的。通过反编译,也可以知道枚举常量本质上就是一个
```
From 0dd7bc92d96ee0bfcce575a4f2a388f92eec4f63 Mon Sep 17 00:00:00 2001
From: lizeyang
Date: 2016年2月25日 16:50:32 +0800
Subject: [PATCH 053/195] =?UTF-8?q?=E6=9C=AA=E7=BF=BB=E8=AF=91=E9=97=AE?=
=?UTF-8?q?=E9=A2=98=E4=B8=AD=E5=8E=BB=E6=8E=89=E2=80=9Ccheck-if-at-least-?=
=?UTF-8?q?two-out-of-three-booleans-are-true=E2=80=9D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 1 -
1 file changed, 1 deletion(-)
diff --git a/README.md b/README.md
index 78e13d4..20dacb6 100644
--- a/README.md
+++ b/README.md
@@ -115,7 +115,6 @@ stackoverflow-Java-top-qa
- [How does the Java for each loop work?](http://stackoverflow.com/questions/85190/how-does-the-java-for-each-loop-work)
- [What is the Java equivalent for LINQ?](http://stackoverflow.com/questions/1217228/what-is-the-java-equivalent-for-linq)
- [Hibernate hbm2ddl.auto possible values and what they do?](http://stackoverflow.com/questions/438146/hibernate-hbm2ddl-auto-possible-values-and-what-they-do)
-- [Check if at least two out of three booleans are true](http://stackoverflow.com/questions/3076078/check-if-at-least-two-out-of-three-booleans-are-true)
- [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)
- [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)
- [Sort ArrayList of custom Objects by property](http://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property)
From 6553d7fe3942778dd81e4ee2ba41bea91cd6850e Mon Sep 17 00:00:00 2001
From: lizeyang
Date: 2016年2月25日 16:54:08 +0800
Subject: [PATCH 054/195] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E2=80=9C=E5=AE=9E?=
=?UTF-8?q?=E7=8E=B0Runnable=E6=8E=A5=E5=8F=A3=20=20VS.=20=E7=BB=A7?=
=?UTF-8?q?=E6=89=BFThread=E7=B1=BB=20=E2=80=9D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 3 ++-
.../implements-runnable-vs-extends-thread.md | 23 +++++++++++++++++++
2 files changed, 25 insertions(+), 1 deletion(-)
create mode 100644 contents/implements-runnable-vs-extends-thread.md
diff --git a/README.md b/README.md
index 20dacb6..873615e 100644
--- a/README.md
+++ b/README.md
@@ -58,6 +58,7 @@ stackoverflow-Java-top-qa
* [Java 源码里的设计模式](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Examples_of_GoF_Design_Patterns_in_Java's_core_libraries.md)
* [如何产生一个随机的字母数字串作为 session 的唯一标识符](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/How_to_generate_a_random_alpha-numeric_string.md)
* [如何创建单例](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/What_is_an_efficient_way_to_implement_a_singleton_in_Java.md)
+* [实现Runnable接口 VS. 继承Thread类](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/implements-runnable-vs-extends-thread.md)
> 网络
@@ -86,7 +87,7 @@ stackoverflow-Java-top-qa
- [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)
- [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)
- [Dealing with "java.lang.OutOfMemoryError: PermGen space" error](http://stackoverflow.com/questions/88235/dealing-with-java-lang-outofmemoryerror-permgen-space-error)
-- ["implements Runnable" vs. "extends Thread"](http://stackoverflow.com/questions/541487/implements-runnable-vs-extends-thread)
+- [Convert a String to an enum in Java](http://stackoverflow.com/questions/604424/convert-a-string-to-an-enum-in-java)
- [Android SDK installation doesn't find JDK](http://stackoverflow.com/questions/4382178/android-sdk-installation-doesnt-find-jdk)
- [Java inner class and static nested class](http://stackoverflow.com/questions/70324/java-inner-class-and-static-nested-class)
- ['Must Override a Superclass Method' Errors after importing a project into Eclipse](http://stackoverflow.com/questions/1678122/must-override-a-superclass-method-errors-after-importing-a-project-into-eclips)
diff --git a/contents/implements-runnable-vs-extends-thread.md b/contents/implements-runnable-vs-extends-thread.md
new file mode 100644
index 0000000..57e1863
--- /dev/null
+++ b/contents/implements-runnable-vs-extends-thread.md
@@ -0,0 +1,23 @@
+##ɻ��Runnable�ӿ� VS. �̳�Thread�� ��
+
+��Java�У�����ִ������һ�������ַ�ʽ��
+��1��ɻ��Runnable�ӿ�
+��2���̳�Thread��
+
+һ�����ԣ��Ƽ�ʹ�÷�ʽ��1��,��Ҫ�����ڴ����������£����Dz������ر�ȥ��ע�̵߳���Ϊ��Ҳ����ȥ��дThread���е���Ϊ��������������ִ���������ѡ�
+���x�ʹ�ýӿڵķ�ʽ�ܱ�������һЩ������Ҫ�Ķ�����ͬɦҲ����Ӱ���̳������࣬��ʹ�����������
+
+
+###������tips��
+��1��Runnable��Thread���ǶԵȵĸ���
+��Thinking in Java�У������2۹�Runnable����������������Task��Ϊ����
+��Java�У�Runnableֻ��һ���������������Ĵ����ζ��ѣ��Ǿ�̬�ĸ����Ҫͨ���߳���ִ�С���Thread������һ�����壬�����;��кܶ���Ϊ���ܹ�����ִ��������
+
+��2����������ȷɻ��Ҫ��д��override��һЩ������Ϊɦ����ʹ�ü̳У�������ʹ�ýӿڡ�
+
+��3����Java 5֮ǰ��������Threadȴû������start()���������ܵ����ڴ�й¶��
+
+
+stackoverflow���ӣ�
+http://stackoverflow.com/questions/541487/implements-runnable-vs-extends-thread
+
From 257a57da0ee0497a67d33e7dac3e1a6b7f0254af Mon Sep 17 00:00:00 2001
From: lizeyang
Date: 2016年2月25日 17:14:44 +0800
Subject: [PATCH 055/195] =?UTF-8?q?=E4=BB=8E=E9=A6=96=E9=A1=B5=E4=B8=AD?=
=?UTF-8?q?=E5=8E=BB=E6=8E=89=E4=B8=A4=E4=B8=AA=E5=B7=B2=E7=BF=BB=E8=AF=91?=
=?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 2 --
1 file changed, 2 deletions(-)
diff --git a/README.md b/README.md
index 873615e..1ae1bd9 100644
--- a/README.md
+++ b/README.md
@@ -96,8 +96,6 @@ stackoverflow-Java-top-qa
- [How to generate a random alpha-numeric string?](http://stackoverflow.com/questions/41107/how-to-generate-a-random-alpha-numeric-string)
- [Comparing Java enum members: == or equals()?](http://stackoverflow.com/questions/1750435/comparing-java-enum-members-or-equals)
- [Failed to load the JNI shared Library (JDK)](http://stackoverflow.com/questions/7352493/failed-to-load-the-jni-shared-library-jdk)
-- [What's the simplest way to print a Java array?](http://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array)
-- [Why can't I switch on a String?](http://stackoverflow.com/questions/338206/why-cant-i-switch-on-a-string)
- [How to create a Java String from the contents of a file?](http://stackoverflow.com/questions/326390/how-to-create-a-java-string-from-the-contents-of-a-file)
- [How can I convert a stack trace to a string?](http://stackoverflow.com/questions/1149703/how-can-i-convert-a-stack-trace-to-a-string)
- [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)
From 5f26494bc3723e1b714a3b96d35fd1c7c61e2c5e Mon Sep 17 00:00:00 2001
From: lizeyang
Date: 2016年2月25日 17:23:12 +0800
Subject: [PATCH 056/195] =?UTF-8?q?=E4=BC=98=E5=8C=96=E2=80=9C=E5=A6=82?=
=?UTF-8?q?=E4=BD=95=E5=8F=82=E4=B8=8E=E7=BF=BB=E8=AF=91=E2=80=9D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
diff --git a/README.md b/README.md
index 1ae1bd9..5137b66 100644
--- a/README.md
+++ b/README.md
@@ -8,13 +8,21 @@ stackoverflow-Java-top-qa
对于参与翻译的人,这也是很好的一个学习、理解过程,欢迎大家一起来翻译
-------------
-### 如何参与翻译
+### 如何参与翻译(欢迎加入翻译组QQ群485011036)
请从下文"待翻译问题链接"中寻找你感兴趣的问答进行翻译。翻译好的问答,放到contents目录下,无需更新readme.md文档。之后提一个PR,我会对你的翻译做一个审校,并更新到readme中。
一些基本的约定:
- 文档的文件名,和stackoverflow上的url保持一致。例如,http://stackoverflow.com/questions/8710619/java-operator 的文件名, 就是java-operator.md
- 在每篇翻译文档内容的最后,要附上stackoverflow的原文链接
+每个人可以做(但不限于):
+- 找未翻译的问题进行翻译
+- 优化已翻译的问题
+- 输出问答精简汇总版(把所有的问答集中到一个md文件,然后尽量精简,让别人可以在一天内把这100个问题的精髓都看完)
+- 输出gitbook版本(现在直接在github上查看,体验不好)
+
+
+
### 目录
> 基础语法
From 16319144ef8982f5f610b559f021c6620e27ce99 Mon Sep 17 00:00:00 2001
From: "baolongf@yahoo.com"
Date: 2016年2月26日 22:41:47 +0800
Subject: [PATCH 057/195] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E2=80=9C=E5=9C=A8jav?=
=?UTF-8?q?a=E4=B8=AD=E5=A6=82=E4=BD=95=E5=88=9B=E5=BB=BA=E6=96=87?=
=?UTF-8?q?=E4=BB=B6=E5=92=8C=E5=86=99=E6=96=87=E4=BB=B6=E2=80=9D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...eate-a-file-and-write-to-a-file-in-java.md | 149 ++++++++++++++++++
1 file changed, 149 insertions(+)
create mode 100644 contents/how-to-create-a-file-and-write-to-a-file-in-java.md
diff --git a/contents/how-to-create-a-file-and-write-to-a-file-in-java.md b/contents/how-to-create-a-file-and-write-to-a-file-in-java.md
new file mode 100644
index 0000000..daf03ce
--- /dev/null
+++ b/contents/how-to-create-a-file-and-write-to-a-file-in-java.md
@@ -0,0 +1,149 @@
+## ��java��ô��һ���ļ��������ļ�д�ı�����
+
+### �ʣ���java�����Ĵ����ļ�д�ļ��ķ�����jô
+
+### ���Ѵ���:
+����һ���ı��ļ���ע�⣺�������ļ����ڣ����Ḳ�Ǹ��ļ���
+````java
+PrintWriter writer = new PrintWriter("the-file-name.txt", "UTF-8");
+writer.println("The first line");
+writer.println("The second line");
+writer.close();
+````
+����һ��λ�������ļ���ͬ���Ḳ�����ļ���
+````java
+byte data[] = ...
+FileOutputStream out = new FileOutputStream("the-file-name");
+out.write(data);
+out.close();
+````
+
+Java 7+ �û�������[`File`](http://docs.oracle.com/javase/7/docs/api/index.html?java/nio/file/Files.html)����д�ļ�
+����һ���ı��ļ�
+````java
+List lines = Arrays.asList("The first line", "The second line");
+Path file = Paths.get("the-file-name.txt");
+Files.write(file, lines, Charset.forName("UTF-8"));
+//Files.write(file, lines, Charset.forName("UTF-8"), StandardOpenOption.APPEND);
+````
+����һ���������ļ�
+````java
+byte data[] = ...
+Path file = Paths.get("the-file-name");
+Files.write(file, data);
+//Files.write(file, data, StandardOpenOption.APPEND);
+````
+
+### �����Ĵ𰸣�1��:
+��Java 7+��
+````java
+try (Writer writer = new BufferedWriter(new OutputStreamWriter(
+ new FileOutputStream("filename.txt"), "utf-8"))) {
+ writer.write("something");
+}
+````
+����һЩɻ�õķ������£�
+* [`FileUtils.writeStringtoFile(..)`](https://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html#writeStringToFile%28java.io.File,%20java.lang.String,%20java.nio.charset.Charset%29) ������ commons-io ��
+* [`Files.write(..)`](http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/io/Files.html#write%28java.lang.CharSequence,%20java.io.File,%20java.nio.charset.Charset%29) ������ guava
+Note also that you can use a FileWriter, but it uses the default encoding,
+which is often a bad idea - it's best to specify the encoding explicitly.
+��Ҫע������ʹ�� `FileWriter`��������ʹ�õ���Ĭ�ρ��룬�ⲻ�Ǻܺõķ�������������ȷָ������
+
+
+������������prior-to-java-7��ԭʼ����
+````java
+Writer writer = null;
+
+try {
+ writer = new BufferedWriter(new OutputStreamWriter(
+ new FileOutputStream("filename.txt"), "utf-8"));
+ writer.write("Something");
+} catch (IOException ex) {
+ // report
+} finally {
+ try {writer.close();} catch (Exception ex) {/*ignore*/}
+}
+````
+���Կ�[`Reading, Writing, and Creating Files`](http://docs.oracle.com/javase/tutorial/essential/io/file.html)(����NIO2)
+
+### �����𰸣�2����
+````java
+public class Program {
+ public static void main(String[] args) {
+ String text = "Hello world";
+ BufferedWriter output = null;
+ try {
+ File file = new File("example.txt");
+ output = new BufferedWriter(new FileWriter(file));
+ output.write(text);
+ } catch ( IOException e ) {
+ e.printStackTrace();
+ } finally {
+ if ( output != null ) output.close();
+ }
+ }
+}
+````
+
+### �����𰸣�3����
+�����Ѿ�����Ҫд���ļ��е����ݣ�[`java.nio.file.Files`](https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html) ��Ϊ Java 7 ���Ӳ��ֵ�native I/O���ṩ�˼�Ч�ķ�����ɻ������L·��
+
+�����Θ����ļ���д�ļ�ֻ��Ҫһ�У�������ֻ��һ������ã�
+���������Ӵ�������д��6����ͬ���ļ���չʾ����ôʹ�õ�
+
+````java
+Charset utf8 = StandardCharsets.UTF_8;
+List lines = Arrays.asList("1st line", "2nd line");
+byte[] data = {1, 2, 3, 4, 5};
+
+try {
+ Files.write(Paths.get("file1.bin"), data);
+ Files.write(Paths.get("file2.bin"), data,
+ StandardOpenOption.CREATE, StandardOpenOption.APPEND);
+ Files.write(Paths.get("file3.txt"), "content".getBytes());
+ Files.write(Paths.get("file4.txt"), "content".getBytes(utf8));
+ Files.write(Paths.get("file5.txt"), lines, utf8);
+ Files.write(Paths.get("file6.txt"), lines, utf8,
+ StandardOpenOption.CREATE, StandardOpenOption.APPEND);
+} catch (IOException e) {
+ e.printStackTrace();
+}
+````
+
+### �����𰸣�4����
+������һ��С������������д�ļ����ð汾�Ĵ����Ƚϳ������ǿ�����������
+````java
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
+
+public class writer {
+ public void writing() {
+ try {
+ //Whatever the file path is.
+ File statText = new File("E:/Java/Reference/bin/images/statsTest.txt");
+ FileOutputStream is = new FileOutputStream(statText);
+ OutputStreamWriter osw = new OutputStreamWriter(is);
+ Writer w = new BufferedWriter(osw);
+ w.write("POTATO!!!");
+ w.close();
+ } catch (IOException e) {
+ System.err.println("Problem writing to the file statsTest.txt");
+ }
+ }
+
+ public static void main(String[]args) {
+ writer write = new writer();
+ write.writing();
+ }
+}
+````
+
+
+
+
+stackoverflow���ӣ�
+http://stackoverflow.com/questions/2885173/how-to-create-a-file-and-write-to-a-file-in-java
From 62775a8ca5f2047fdfdf49320475c20624392060 Mon Sep 17 00:00:00 2001
From: "baolongf@yahoo.com"
Date: 2016年2月28日 14:13:15 +0800
Subject: [PATCH 058/195] modify get-current-stack-trace-in-java
---
contents/get-current-stack-trace-in-java.md | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/contents/get-current-stack-trace-in-java.md b/contents/get-current-stack-trace-in-java.md
index 1e2fa83..9b17cc6 100644
--- a/contents/get-current-stack-trace-in-java.md
+++ b/contents/get-current-stack-trace-in-java.md
@@ -8,5 +8,9 @@
String fullStackTrace = org.apache.commons.lang.exception.ExceptionUtils.getFullStackTrace(e)
```
+````java
+Thread.currentThread().getStackTrace();
+````
+
stackoverflow原址:
http://stackoverflow.com/questions/1069066/how-can-i-get-the-current-stack-trace
\ No newline at end of file
From cdee8a74046ae20aecb9016797515d073717a5b4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B8=B0=E5=AE=9D=E9=BE=99=20fengbaolong=20=2888342=29?=
Date: 2016年2月29日 08:57:25 +0800
Subject: [PATCH 059/195] =?UTF-8?q?=E6=96=87=E4=BB=B6=E7=BC=96=E7=A0=81?=
=?UTF-8?q?=E6=94=B9=E4=B8=BAUTF-8?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...eate-a-file-and-write-to-a-file-in-java.md | 48 +++++++++----------
1 file changed, 24 insertions(+), 24 deletions(-)
diff --git a/contents/how-to-create-a-file-and-write-to-a-file-in-java.md b/contents/how-to-create-a-file-and-write-to-a-file-in-java.md
index daf03ce..b94e173 100644
--- a/contents/how-to-create-a-file-and-write-to-a-file-in-java.md
+++ b/contents/how-to-create-a-file-and-write-to-a-file-in-java.md
@@ -1,16 +1,16 @@
-## ��java��ô��һ���ļ��������ļ�д�ı�����
+## 用java怎么样创建一个文件并向该文件写文本内容
-### �ʣ���java�����Ĵ����ļ�д�ļ��ķ�����jô
+### 问:在java里最简单的创建文件写文件的方法是什么
-### ���Ѵ���:
-����һ���ı��ļ���ע�⣺�������ļ����ڣ����Ḳ�Ǹ��ļ���
+### 最佳答案:
+创建一个文本文件(注意:如果该文件存在,则会覆盖该文件)
````java
PrintWriter writer = new PrintWriter("the-file-name.txt", "UTF-8");
writer.println("The first line");
writer.println("The second line");
writer.close();
````
-����һ��λ�������ļ���ͬ���Ḳ�����ļ���
+创建一个位二进制文件(同样会覆盖这文件)
````java
byte data[] = ...
FileOutputStream out = new FileOutputStream("the-file-name");
@@ -18,15 +18,15 @@ out.write(data);
out.close();
````
-Java 7+ �û�������[`File`](http://docs.oracle.com/javase/7/docs/api/index.html?java/nio/file/Files.html)����д�ļ�
-����һ���ı��ļ�
+Java 7+ 用户可以用[`File`](http://docs.oracle.com/javase/7/docs/api/index.html?java/nio/file/Files.html)类来写文件
+创建一个文本文件
````java
List lines = Arrays.asList("The first line", "The second line");
Path file = Paths.get("the-file-name.txt");
Files.write(file, lines, Charset.forName("UTF-8"));
//Files.write(file, lines, Charset.forName("UTF-8"), StandardOpenOption.APPEND);
````
-����һ���������ļ�
+创建一个二进制文件
````java
byte data[] = ...
Path file = Paths.get("the-file-name");
@@ -34,23 +34,23 @@ Files.write(file, data);
//Files.write(file, data, StandardOpenOption.APPEND);
````
-### �����Ĵ𰸣�1��:
-��Java 7+��
+### 其他的答案(1):
+在Java 7+中
````java
try (Writer writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream("filename.txt"), "utf-8"))) {
writer.write("something");
}
````
-����һЩɻ�õķ������£�
-* [`FileUtils.writeStringtoFile(..)`](https://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html#writeStringToFile%28java.io.File,%20java.lang.String,%20java.nio.charset.Charset%29) ������ commons-io ��
-* [`Files.write(..)`](http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/io/Files.html#write%28java.lang.CharSequence,%20java.io.File,%20java.nio.charset.Charset%29) ������ guava
+还有一些实用的方法如下:
+* [`FileUtils.writeStringtoFile(..)`](https://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html#writeStringToFile%28java.io.File,%20java.lang.String,%20java.nio.charset.Charset%29) 来自于 commons-io 包
+* [`Files.write(..)`](http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/io/Files.html#write%28java.lang.CharSequence,%20java.io.File,%20java.nio.charset.Charset%29) 来自于 guava
Note also that you can use a FileWriter, but it uses the default encoding,
which is often a bad idea - it's best to specify the encoding explicitly.
-��Ҫע������ʹ�� `FileWriter`��������ʹ�õ���Ĭ�ρ��룬�ⲻ�Ǻܺõķ�������������ȷָ������
+还要注意可以使用 `FileWriter`,但是它使用的是默认编码,这不是很好的方法,最好是明确指定编码
-������������prior-to-java-7��ԭʼ����
+下面是来自于prior-to-java-7的原始方法
````java
Writer writer = null;
@@ -64,9 +64,9 @@ try {
try {writer.close();} catch (Exception ex) {/*ignore*/}
}
````
-���Կ�[`Reading, Writing, and Creating Files`](http://docs.oracle.com/javase/tutorial/essential/io/file.html)(����NIO2)
+可以看[`Reading, Writing, and Creating Files`](http://docs.oracle.com/javase/tutorial/essential/io/file.html)(包含NIO2)
-### �����𰸣�2����
+### 其他答案(2):
````java
public class Program {
public static void main(String[] args) {
@@ -85,11 +85,11 @@ public class Program {
}
````
-### �����𰸣�3����
-�����Ѿ�����Ҫд���ļ��е����ݣ�[`java.nio.file.Files`](https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html) ��Ϊ Java 7 ���Ӳ��ֵ�native I/O���ṩ�˼�Ч�ķ�����ɻ������L·��
+### 其他答案(3):
+如果已经有想要写到文件中的内容,[`java.nio.file.Files`](https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html) 作为 Java 7 附加部分的native I/O,提供了简单高效的方法来实现你的目标
-�����Θ����ļ���д�ļ�ֻ��Ҫһ�У�������ֻ��һ������ã�
-���������Ӵ�������д��6����ͬ���ļ���չʾ����ôʹ�õ�
+基本上创建文件,写文件只需要一行,而且是只需一个方法调用!
+下面的例子创建并且写了6个不同的文件来展示是怎么使用的
````java
Charset utf8 = StandardCharsets.UTF_8;
@@ -110,8 +110,8 @@ try {
}
````
-### �����𰸣�4����
-������һ��С������������д�ļ����ð汾�Ĵ����Ƚϳ������ǿ�����������
+### 其他答案(4):
+下面是一个小程序来创建和写文件。该版本的代码比较长,但是可以容易理解
````java
import java.io.BufferedWriter;
import java.io.File;
@@ -145,5 +145,5 @@ public class writer {
-stackoverflow���ӣ�
+stackoverflow链接:
http://stackoverflow.com/questions/2885173/how-to-create-a-file-and-write-to-a-file-in-java
From ce4e265dec4e12c03d10c3b3a11b1e2cf18af30d Mon Sep 17 00:00:00 2001
From: lizeyang
Date: 2016年2月29日 18:29:13 +0800
Subject: [PATCH 060/195] =?UTF-8?q?=E4=BC=98=E5=8C=96=E2=80=9C=E5=A6=82?=
=?UTF-8?q?=E4=BD=95=E5=8F=82=E4=B8=8E=E7=BF=BB=E8=AF=91=E2=80=9C?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/README.md b/README.md
index 5137b66..85dba7e 100644
--- a/README.md
+++ b/README.md
@@ -9,7 +9,9 @@ stackoverflow-Java-top-qa
-------------
### 如何参与翻译(欢迎加入翻译组QQ群485011036)
-请从下文"待翻译问题链接"中寻找你感兴趣的问答进行翻译。翻译好的问答,放到contents目录下,无需更新readme.md文档。之后提一个PR,我会对你的翻译做一个审校,并更新到readme中。
+如何参与:
+- 请从下文"待翻译问题链接"中寻找你感兴趣的问答进行翻译。翻译好的问答,放到contents目录下,无需更新readme.md文档。之后提一个PR,我会对你的翻译做一个审校,并更新到readme中。
+- 另外,为了避免多人重复新翻译一个问题,你可以提issue,说明你计划翻译的问题及时间点,我可以先更新到下面的"未翻译问题"中,说明已有人领了这个问题。当然,也不一定要提issue,一般情况下,只要及时提pr,我及时审核,出现"撞车"的概率并不高。
一些基本的约定:
- 文档的文件名,和stackoverflow上的url保持一致。例如,http://stackoverflow.com/questions/8710619/java-operator 的文件名, 就是java-operator.md
@@ -20,6 +22,7 @@ stackoverflow-Java-top-qa
- 优化已翻译的问题
- 输出问答精简汇总版(把所有的问答集中到一个md文件,然后尽量精简,让别人可以在一天内把这100个问题的精髓都看完)
- 输出gitbook版本(现在直接在github上查看,体验不好)
+-
From b7ce650495daa0dad97901ed04eedd48399834d3 Mon Sep 17 00:00:00 2001
From: lxn348567248
Date: Tue, 1 Mar 2016 11:35:14 +0800
Subject: [PATCH 061/195] =?UTF-8?q?=E6=88=91=E5=BA=94=E8=AF=A5=E7=94=A8?=
=?UTF-8?q?=E5=93=AA=E4=B8=80=E4=B8=AA@NotNull=E6=B3=A8=E8=A7=A3?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...h @NotNull Java annotation should I use.md | 53 +++++++++++++++++++
1 file changed, 53 insertions(+)
create mode 100644 contents/Which @NotNull Java annotation should I use.md
diff --git a/contents/Which @NotNull Java annotation should I use.md b/contents/Which @NotNull Java annotation should I use.md
new file mode 100644
index 0000000..979f7a3
--- /dev/null
+++ b/contents/Which @NotNull Java annotation should I use.md
@@ -0,0 +1,53 @@
+我应该用哪一个@NotNull注解?
+===
+
+我试图代码的可读性像我们的开发工具IDE一样进行代码检查、静态代码分析(FindBugs,Sonar)避免空指针.大多数的开发工具是不兼容的,对于@NotNull/@NonNull/@Nonnull注解,对于理解它们是非常困难的。那么那一个是最好的选择?下面是我找到的对于注解的说明:
+
+
+1.javax.validation.constraints.NotNull
+
+ 运行时进验证,不静态分析
+
+
+2.edu.umd.cs.findbugs.annotations.NonNull
+
+ 用于finbugs和Sonar静态分析
+
+3.javax.annotation.Nonnull
+
+ 只适用FindBugs,JSR-305不适用
+
+4.org.jetbrains.annotations.NotNull
+
+ 适用用于IntelliJ IDEA静态分析
+
+5.lombok.NonNull
+
+ 适用Lombok项目中代码生成器。不是一个标准的占位符注解.
+
+6.android.support.annotation.NonNull
+
+ 适用于Android项目的标记注解,位于support-annotations包中
+
+回答
+---
+
+我只用javax命名空间下的注解(虽然我喜欢Lombok和Intelij做的事情).
+
+我用javax.validation.constraints.NotNull,因为它已经在Java EE 6中定义
+
+javax.annotation.NonNull可能直到java 8都不存在(正如Stephen指出)。其他的都不是标准的注解 .你可能在语法上得到帮助而不能在运行时给你帮助,对于许多项目,这是好的,但是对于我这不是合适的.
+
+如果注解是可扩展的,那将是一件美好的事情.这样,我们会能过封装和继承定义的的null注解.当标准解决了这个问题,你就不得不重新定义你的注解。
+
+不幸的,这种事情没有发生.
+
+
+
+
+stackoverflow链接: http://stackoverflow.com/questions/4963300/which-notnull-java-annotation-should-i-use
+
+
+
+
+
\ No newline at end of file
From 18b2564c140a5a756757b31a87de47e1037de489 Mon Sep 17 00:00:00 2001
From: lizeyang
Date: Tue, 1 Mar 2016 15:03:49 +0800
Subject: [PATCH 062/195] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dreamme=E6=98=BE?=
=?UTF-8?q?=E7=A4=BA=E7=9A=84=E4=B8=80=E4=B8=AAbug?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 3 ---
1 file changed, 3 deletions(-)
diff --git a/README.md b/README.md
index 85dba7e..d6c79dd 100644
--- a/README.md
+++ b/README.md
@@ -22,9 +22,6 @@ stackoverflow-Java-top-qa
- 优化已翻译的问题
- 输出问答精简汇总版(把所有的问答集中到一个md文件,然后尽量精简,让别人可以在一天内把这100个问题的精髓都看完)
- 输出gitbook版本(现在直接在github上查看,体验不好)
--
-
-
### 目录
> 基础语法
From 83f795a8038c5800d52cc6c3670db6bcd44ff123 Mon Sep 17 00:00:00 2001
From: lizeyang
Date: Wed, 2 Mar 2016 11:22:42 +0800
Subject: [PATCH 063/195] =?UTF-8?q?=E4=BC=98=E5=8C=96"=E6=88=91=E5=BA=94?=
=?UTF-8?q?=E8=AF=A5=E7=94=A8=E5=93=AA=E4=B8=80=E4=B8=AA@NotNull=E6=B3=A8?=
=?UTF-8?q?=E8=A7=A3=3F"?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 5 +-
...h @NotNull Java annotation should I use.md | 53 -------------------
...ch-notnull-java-annotation-should-i-use.md | 49 +++++++++++++++++
3 files changed, 52 insertions(+), 55 deletions(-)
delete mode 100644 contents/Which @NotNull Java annotation should I use.md
create mode 100644 contents/which-notnull-java-annotation-should-i-use.md
diff --git a/README.md b/README.md
index d6c79dd..71f7358 100644
--- a/README.md
+++ b/README.md
@@ -14,7 +14,7 @@ stackoverflow-Java-top-qa
- 另外,为了避免多人重复新翻译一个问题,你可以提issue,说明你计划翻译的问题及时间点,我可以先更新到下面的"未翻译问题"中,说明已有人领了这个问题。当然,也不一定要提issue,一般情况下,只要及时提pr,我及时审核,出现"撞车"的概率并不高。
一些基本的约定:
-- 文档的文件名,和stackoverflow上的url保持一致。例如,http://stackoverflow.com/questions/8710619/java-operator 的文件名, 就是java-operator.md
+- 文档的文件名,和stackoverflowhich-notnull-java-annotation-should-i-usew上的url保持一致。例如,http://stackoverflow.com/questions/8710619/java-operator 的文件名, 就是java-operator.md
- 在每篇翻译文档内容的最后,要附上stackoverflow的原文链接
每个人可以做(但不限于):
@@ -67,6 +67,8 @@ stackoverflow-Java-top-qa
* [如何产生一个随机的字母数字串作为 session 的唯一标识符](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/How_to_generate_a_random_alpha-numeric_string.md)
* [如何创建单例](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/What_is_an_efficient_way_to_implement_a_singleton_in_Java.md)
* [实现Runnable接口 VS. 继承Thread类](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/implements-runnable-vs-extends-thread.md)
+* [我应该用哪一个@NotNull注解](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/which-notnull-java-annotation-should-i-use.md)
+
> 网络
@@ -140,7 +142,6 @@ stackoverflow-Java-top-qa
- [What is the difference between JSF, Servlet and JSP?](http://stackoverflow.com/questions/2095397/what-is-the-difference-between-jsf-servlet-and-jsp)
- [How do I "decompile" Java class files?](http://stackoverflow.com/questions/272535/how-do-i-decompile-java-class-files)
- [Useful Eclipse Java Code Templates [closed]](http://stackoverflow.com/questions/1028858/useful-eclipse-java-code-templates)
-- [Which @NotNull Java annotation should I use?](http://stackoverflow.com/questions/4963300/which-notnull-java-annotation-should-i-use)
- [How to call SOAP web service in Android](http://stackoverflow.com/questions/297586/how-to-call-soap-web-service-in-android)
### contributors
diff --git a/contents/Which @NotNull Java annotation should I use.md b/contents/Which @NotNull Java annotation should I use.md
deleted file mode 100644
index 979f7a3..0000000
--- a/contents/Which @NotNull Java annotation should I use.md
+++ /dev/null
@@ -1,53 +0,0 @@
-我应该用哪一个@NotNull注解?
-===
-
-我试图代码的可读性像我们的开发工具IDE一样进行代码检查、静态代码分析(FindBugs,Sonar)避免空指针.大多数的开发工具是不兼容的,对于@NotNull/@NonNull/@Nonnull注解,对于理解它们是非常困难的。那么那一个是最好的选择?下面是我找到的对于注解的说明:
-
-
-1.javax.validation.constraints.NotNull
-
- 运行时进验证,不静态分析
-
-
-2.edu.umd.cs.findbugs.annotations.NonNull
-
- 用于finbugs和Sonar静态分析
-
-3.javax.annotation.Nonnull
-
- 只适用FindBugs,JSR-305不适用
-
-4.org.jetbrains.annotations.NotNull
-
- 适用用于IntelliJ IDEA静态分析
-
-5.lombok.NonNull
-
- 适用Lombok项目中代码生成器。不是一个标准的占位符注解.
-
-6.android.support.annotation.NonNull
-
- 适用于Android项目的标记注解,位于support-annotations包中
-
-回答
----
-
-我只用javax命名空间下的注解(虽然我喜欢Lombok和Intelij做的事情).
-
-我用javax.validation.constraints.NotNull,因为它已经在Java EE 6中定义
-
-javax.annotation.NonNull可能直到java 8都不存在(正如Stephen指出)。其他的都不是标准的注解 .你可能在语法上得到帮助而不能在运行时给你帮助,对于许多项目,这是好的,但是对于我这不是合适的.
-
-如果注解是可扩展的,那将是一件美好的事情.这样,我们会能过封装和继承定义的的null注解.当标准解决了这个问题,你就不得不重新定义你的注解。
-
-不幸的,这种事情没有发生.
-
-
-
-
-stackoverflow链接: http://stackoverflow.com/questions/4963300/which-notnull-java-annotation-should-i-use
-
-
-
-
-
\ No newline at end of file
diff --git a/contents/which-notnull-java-annotation-should-i-use.md b/contents/which-notnull-java-annotation-should-i-use.md
new file mode 100644
index 0000000..214a47e
--- /dev/null
+++ b/contents/which-notnull-java-annotation-should-i-use.md
@@ -0,0 +1,49 @@
+我应该用哪一个@NotNull注解?
+===
+我希望能通过注解的方式,尽量避免程序中出现空指针问题,同时既能保障代码的可读性,又能和IDE的代码检查,静态代码扫描工具结合起来。相关的注解,我看到有好多种@NotNull/@NonNull/@Nonnull,而他们彼此间又有冲突,不能共用,下面是我找到的一些注解,哪个是最好的选择呢?
+
+1.javax.validation.constraints.NotNull
+
+ 运行时进验证,不静态分析
+
+2.edu.umd.cs.findbugs.annotations.NonNull
+
+ 用于finbugs和Sonar静态分析
+
+3.javax.annotation.Nonnull
+
+ 只适用FindBugs,JSR-305不适用
+
+4.org.jetbrains.annotations.NotNull
+
+ 适用用于IntelliJ IDEA静态分析
+
+5.lombok.NonNull
+
+ 适用Lombok项目中代码生成器。不是一个标准的占位符注解.
+
+6.android.support.annotation.NonNull
+
+ 适用于Android项目的标记注解,位于support-annotations包中
+
+回答
+---
+
+我推荐用javax命名空间下的注解(虽然我喜欢Lombok和Intelij做的事情),使用其他命名空间的注解,等于你还需要引入其他依赖。
+
+我用javax.validation.constraints.NotNull,因为它已经在Java EE 6中定义
+
+javax.annotation.NonNull可能直到java 8都不存在(正如Stephen指出)。其他的都不是标准的注解 .
+
+如果注解是可扩展的,那将是一件美好的事情.你可以自己写一个`non-null`注解,然后继承上面说的这些注解。如果标准的注解不支持某个特性,你就可以在自己定义的注解里面扩展。
+
+
+
+
+
+stackoverflow链接: http://stackoverflow.com/questions/4963300/which-notnull-java-annotation-should-i-use
+
+
+
+
+
\ No newline at end of file
From 5e1558d0d8a369d18ac0c528b337889aaa0a9a5a Mon Sep 17 00:00:00 2001
From: lizeyang
Date: Wed, 2 Mar 2016 11:26:35 +0800
Subject: [PATCH 064/195] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dreamd=E6=A0=BC?=
=?UTF-8?q?=E5=BC=8F=E9=97=AE=E9=A2=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/README.md b/README.md
index 71f7358..6240471 100644
--- a/README.md
+++ b/README.md
@@ -48,7 +48,7 @@ stackoverflow-Java-top-qa
* [ `finally` 代码块总会被执行么](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Does-finally-always-execute-in-Java.md)
* [如何将String转换为enum](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/convert-a-string-to-an-enum-in-java.md)
* [在Java中声明数组](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Declare-array-in-Java.md)
-* [反射是什么及其用途](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/What-is-reflection-and-why-is-it-useful.md.md)
+* [反射是什么及其用途](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/What-is-reflection-and-why-is-it-useful.md)
* [为什么不能用string类型进行switch判断](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Why-can't-I-switch-on-a-String.md)
* [比较java枚举成员使用equal还是==](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/comparing-java-enum-members-or-equals.md)
From 532ff0c4c0b38847976e40f77d638a9d6056a7ce Mon Sep 17 00:00:00 2001
From: lizeyang
Date: Wed, 2 Mar 2016 11:34:42 +0800
Subject: [PATCH 065/195] =?UTF-8?q?=E4=BC=98=E5=8C=96=E2=80=9C=E7=94=A8jav?=
=?UTF-8?q?a=E6=80=8E=E4=B9=88=E5=88=9B=E5=BB=BA=E4=B8=80=E4=B8=AA?=
=?UTF-8?q?=E6=96=87=E4=BB=B6=E5=B9=B6=E5=90=91=E8=AF=A5=E6=96=87=E4=BB=B6?=
=?UTF-8?q?=E5=86=99=E6=96=87=E6=9C=AC=E5=86=85=E5=AE=B9=E2=80=9D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 2 +-
contents/how-to-create-a-file-and-write-to-a-file-in-java.md | 5 +++--
2 files changed, 4 insertions(+), 3 deletions(-)
diff --git a/README.md b/README.md
index 6240471..48fe6e6 100644
--- a/README.md
+++ b/README.md
@@ -51,6 +51,7 @@ stackoverflow-Java-top-qa
* [反射是什么及其用途](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/What-is-reflection-and-why-is-it-useful.md)
* [为什么不能用string类型进行switch判断](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Why-can't-I-switch-on-a-String.md)
* [比较java枚举成员使用equal还是==](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/comparing-java-enum-members-or-equals.md)
+* [用java怎么创建一个文件并向该文件写文本内容](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-to-create-a-file-and-write-to-a-file-in-java.md)
> 编程技巧
@@ -137,7 +138,6 @@ stackoverflow-Java-top-qa
- [Why does Math.round(0.49999999999999994) return 1](http://stackoverflow.com/questions/9902968/why-does-math-round0-49999999999999994-return-1)
- [Eclipse: Set maximum line length for auto formatting?](http://stackoverflow.com/questions/3697287/eclipse-set-maximum-line-length-for-auto-formatting)
- [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)
-- [How to create a file and write to a file in Java?](http://stackoverflow.com/questions/2885173/how-to-create-a-file-and-write-to-a-file-in-java)
- [What is the equivalent of the C++ Pair in Java?](http://stackoverflow.com/questions/156275/what-is-the-equivalent-of-the-c-pairl-r-in-java)
- [What is the difference between JSF, Servlet and JSP?](http://stackoverflow.com/questions/2095397/what-is-the-difference-between-jsf-servlet-and-jsp)
- [How do I "decompile" Java class files?](http://stackoverflow.com/questions/272535/how-do-i-decompile-java-class-files)
diff --git a/contents/how-to-create-a-file-and-write-to-a-file-in-java.md b/contents/how-to-create-a-file-and-write-to-a-file-in-java.md
index b94e173..b447395 100644
--- a/contents/how-to-create-a-file-and-write-to-a-file-in-java.md
+++ b/contents/how-to-create-a-file-and-write-to-a-file-in-java.md
@@ -1,4 +1,4 @@
-## 用java怎么样创建一个文件并向该文件写文本内容
+## 用java怎么创建一个文件并向该文件写文本内容
### 问:在java里最简单的创建文件写文件的方法是什么
@@ -10,7 +10,7 @@ writer.println("The first line");
writer.println("The second line");
writer.close();
````
-创建一个位二进制文件(同样会覆盖这文件)
+创建一个二进制文件(同样会覆盖这文件)
````java
byte data[] = ...
FileOutputStream out = new FileOutputStream("the-file-name");
@@ -45,6 +45,7 @@ try (Writer writer = new BufferedWriter(new OutputStreamWriter(
还有一些实用的方法如下:
* [`FileUtils.writeStringtoFile(..)`](https://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html#writeStringToFile%28java.io.File,%20java.lang.String,%20java.nio.charset.Charset%29) 来自于 commons-io 包
* [`Files.write(..)`](http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/io/Files.html#write%28java.lang.CharSequence,%20java.io.File,%20java.nio.charset.Charset%29) 来自于 guava
+
Note also that you can use a FileWriter, but it uses the default encoding,
which is often a bad idea - it's best to specify the encoding explicitly.
还要注意可以使用 `FileWriter`,但是它使用的是默认编码,这不是很好的方法,最好是明确指定编码
From bb703e7a7a9c732fc7f27032f9bab7bb21bec83d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B8=B0=E5=AE=9D=E9=BE=99=20fengbaolong=20=2888342=29?=
Date: Mon, 7 Mar 2016 15:01:36 +0800
Subject: [PATCH 066/195] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=80=8E=E4=B9=88?=
=?UTF-8?q?=E6=A0=B7=E5=B0=86=E5=A0=86=E6=A0=88=E8=BF=BD=E8=B8=AA=E4=BF=A1?=
=?UTF-8?q?=E6=81=AF=E8=BD=AC=E6=8D=A2=E4=B8=BA=E5=AD=97=E7=AC=A6=E4=B8=B2?=
=?UTF-8?q?=E7=B1=BB=E5=9E=8B?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
contents/how-can-i-convert-a-stack-trace-to-a-string.md | 0
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 contents/how-can-i-convert-a-stack-trace-to-a-string.md
diff --git a/contents/how-can-i-convert-a-stack-trace-to-a-string.md b/contents/how-can-i-convert-a-stack-trace-to-a-string.md
new file mode 100644
index 0000000..e69de29
From 8d750127e1e2620212819d546f6e865fa7288fef Mon Sep 17 00:00:00 2001
From: lxn348567248
Date: Mon, 7 Mar 2016 17:48:24 +0800
Subject: [PATCH 067/195] =?UTF-8?q?=E5=A6=82=E4=BD=95=E5=A4=84=E7=90=86=20?=
=?UTF-8?q?java.lang.outOfMemoryError=20PermGen=20space=20error?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...ng.OutOfMemoryError PermGen space error.md | 28 +++++++++++++++++++
1 file changed, 28 insertions(+)
create mode 100644 contents/Dealing with java.lang.OutOfMemoryError PermGen space error.md
diff --git a/contents/Dealing with java.lang.OutOfMemoryError PermGen space error.md b/contents/Dealing with java.lang.OutOfMemoryError PermGen space error.md
new file mode 100644
index 0000000..3cbb6c3
--- /dev/null
+++ b/contents/Dealing with java.lang.OutOfMemoryError PermGen space error.md
@@ -0,0 +1,28 @@
+##如何处理 java.lang.outOfMemoryError PermGen space error
+
+###问题
+
+最近,我在过运行我的web应用时得到:java.lang.OutOfMemoryError: PermGen space。
+我的应用用一个典型的 Hibernate/JPA + IceFaces/JSF的应用.运行于Tomcat6.0和jdk1.6.我发布了多次以后,产生了这个错误。
+
+是什么原因造成的,我如何避免?我怎样修复?
+
+
+#回答
+
+
+解决的方案是当TomeCat启时,在jvm的的命令行添加参数
+
+ -XX:+CMSClassUnloadingEnabled -XX:+CMSPermGenSweepingEnabled
+
+
+你也可以停止tomcat的服务,直接进入Tomcat/bin目录,运行tomcat6w.exe.在Java的标签下,参加上面的参数。单击"OK",重新启动Tomcat的服务.
+
+
+如果你得到不一个未安装的服务,你可以运行:
+
+ tomcat6w //ES//servicename
+
+servicename的名字你右以在services.msc中查看。
+
+
From 7bdabe13509082f82a3d8b0ca7fdccede0ebdde5 Mon Sep 17 00:00:00 2001
From: Tneciv
Date: Wed, 9 Mar 2016 15:25:48 +0800
Subject: [PATCH 068/195] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=94=99=E8=AF=AF?=
=?UTF-8?q?=E6=8B=BC=E5=86=99"break"=20at=20line=2017?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
contents/breaking-out-of-nested-loops-in-java.md | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/contents/breaking-out-of-nested-loops-in-java.md b/contents/breaking-out-of-nested-loops-in-java.md
index c970ccd..d145828 100644
--- a/contents/breaking-out-of-nested-loops-in-java.md
+++ b/contents/breaking-out-of-nested-loops-in-java.md
@@ -14,7 +14,7 @@ for (Type type : types) {
###回答
-可以用brea+label的语法,例子如下
+可以用break+label的语法,例子如下
```java
public class Test {
public static void main(String[] args) {
@@ -36,4 +36,4 @@ public class Test {
首先在for循环前加标签,如例子中的outerloop,然后在for循环内break label(如本例的outerloop),就会跳出该label指定的for循环。
stackoverflow链接:
-http://stackoverflow.com/questions/886955/breaking-out-of-nested-loops-in-java
\ No newline at end of file
+http://stackoverflow.com/questions/886955/breaking-out-of-nested-loops-in-java
From c93bef03d1eb3e5bb5b9f12c07bb886cc53858f5 Mon Sep 17 00:00:00 2001
From: Tneciv
Date: Wed, 9 Mar 2016 15:34:12 +0800
Subject: [PATCH 069/195] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E2=80=9C=E5=A6=82?=
=?UTF-8?q?=E4=BD=95=E2=80=9D=E4=B8=BA=E2=80=9C=E5=A6=82=E6=9E=9C=E2=80=9D?=
=?UTF-8?q?=20at=20line=2043?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
contents/how-do-i-compare-strings-in-java.md | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/contents/how-do-i-compare-strings-in-java.md b/contents/how-do-i-compare-strings-in-java.md
index cd99a5c..54d17e1 100644
--- a/contents/how-do-i-compare-strings-in-java.md
+++ b/contents/how-do-i-compare-strings-in-java.md
@@ -40,5 +40,5 @@ new String("test") == new String("test") // --> false
```
###其他
-- 如何你重写了equal方法,记得相对应地修改hashcode方法,否则将会违反这两个方法的对等关系,如果两个对象是相等(equal)的,那么两个对象调用hashCode必须产生相同的整数结果,即:equal为true,hashCode必须为true,equal为false,hashCode也必须为false
-- 如果要忽略大小写进行对比,可以用equalsIgnoreCase()方法
\ No newline at end of file
+- 如果你重写了equal方法,记得相对应地修改hashcode方法,否则将会违反这两个方法的对等关系,如果两个对象是相等(equal)的,那么两个对象调用hashCode必须产生相同的整数结果,即:equal为true,hashCode必须为true,equal为false,hashCode也必须为false
+- 如果要忽略大小写进行对比,可以用equalsIgnoreCase()方法
From 116bdcc967b24ae70ed50f8a6cb680db5b777221 Mon Sep 17 00:00:00 2001
From: Tneciv
Date: Wed, 9 Mar 2016 16:17:51 +0800
Subject: [PATCH 070/195] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E2=80=9C=E3=80=8A=3D?=
=?UTF-8?q?=E2=80=9D=E4=B8=BA=E2=80=9C<=3d=e2=80=9d=20at=20line=2016?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contents/generating-random-integers-in-a-range-with-Java.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contents/generating-random-integers-in-a-range-with-Java.md b/contents/generating-random-integers-in-a-range-with-Java.md index 3984e69..fa04ca8 100644 --- a/contents/generating-random-integers-in-a-range-with-Java.md +++ b/contents/generating-random-integers-in-a-range-with-Java.md @@ -10,7 +10,7 @@ int num =(int)(Math.random() * 11); ``` -那如何产生 "5《= 随机数 <= 10" 的随机数呢? +那如何产生 "5 <= 随机数 <= 10" 的随机数呢? ``` int num = 5 + (int)(Math.random() * 6); @@ -29,7 +29,7 @@ int num = min + (int)(Math.random() * (max-min+1)); Random 是 java 提供的一个伪随机数生成器。 -生成 "min <= 随机数 <= max " 的随机数: +生成 " min <= 随机数 <= max " 的随机数: ``` import java.util.Random; From 09c3cfa53b21b9db41099edc2a35614d81d54ef0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=B0=E5=AE=9D=E9=BE=99=20fengbaolong=20=2888342=29?=
Date: Wed, 9 Mar 2016 16:23:39 +0800
Subject: [PATCH 071/195] =?UTF-8?q?=E6=80=8E=E4=B9=88=E6=A0=B7=E5=B0=86?=
=?UTF-8?q?=E5=A0=86=E6=A0=88=E8=BF=BD=E8=B8=AA=E4=BF=A1=E6=81=AF=E8=BD=AC?=
=?UTF-8?q?=E6=8D=A2=E4=B8=BA=E5=AD=97=E7=AC=A6=E4=B8=B2=E7=B1=BB=E5=9E=8B?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...can-i-convert-a-stack-trace-to-a-string.md | 36 +++++++++++++++++++
1 file changed, 36 insertions(+)
diff --git a/contents/how-can-i-convert-a-stack-trace-to-a-string.md b/contents/how-can-i-convert-a-stack-trace-to-a-string.md
index e69de29..c601cfc 100644
--- a/contents/how-can-i-convert-a-stack-trace-to-a-string.md
+++ b/contents/how-can-i-convert-a-stack-trace-to-a-string.md
@@ -0,0 +1,36 @@
+#怎么样将堆栈追踪信息转换为字符串类型
+##问题
+将`Throwable.getStackTrace()`的结果转换为一个字符串来来描述堆栈信息的最简单的方法是什么
+
+
+###最佳答案
+可以用下面的方法将异常堆栈信息转换为字符串类型。该类在Apache commons-lang-2.2.jar中可以找到:
+[`org.apache.commons.lang.exception.ExceptionUtils.getStackTrace(Throwable)`](org.apache.commons.lang.exception.ExceptionUtils.getStackTrace\(Throwable\))
+
+###答案二
+用 [`Throwable.printStackTrace(PrintWriter pw)`](https://docs.oracle.com/javase/8/docs/api/java/lang/Throwable.html#printStackTrace-java.io.PrintWriter-)将对堆栈信息发送到一个输出中:
+````java
+StringWriter sw = new StringWriter();
+PrintWriter pw = new PrintWriter(sw);
+t.printStackTrace(pw);
+sw.toString(); // stack trace as a string
+````
+
+###答案三
+````java
+StringWriter sw = new StringWriter();
+e.printStackTrace(new PrintWriter(sw));
+String exceptionAsString = sw.toString();
+````
+
+###答案四
+````java
+public String stackTraceToString(Throwable e) {
+ StringBuilder sb = new StringBuilder();
+ for (StackTraceElement element : e.getStackTrace()) {
+ sb.append(element.toString());
+ sb.append("\n");
+ }
+ return sb.toString();
+}
+````
\ No newline at end of file
From d9982089167152585f15dc4f3c918daa705f9e8f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B8=B0=E5=AE=9D=E9=BE=99=20fengbaolong=20=2888342=29?=
Date: Wed, 9 Mar 2016 16:36:28 +0800
Subject: [PATCH 072/195] add stackoverflow link
---
contents/how-can-i-convert-a-stack-trace-to-a-string.md | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/contents/how-can-i-convert-a-stack-trace-to-a-string.md b/contents/how-can-i-convert-a-stack-trace-to-a-string.md
index c601cfc..8e8df0a 100644
--- a/contents/how-can-i-convert-a-stack-trace-to-a-string.md
+++ b/contents/how-can-i-convert-a-stack-trace-to-a-string.md
@@ -33,4 +33,7 @@ public String stackTraceToString(Throwable e) {
}
return sb.toString();
}
-````
\ No newline at end of file
+````
+
+stackoverflow链接:
+http://stackoverflow.com/questions/1149703/how-can-i-convert-a-stack-trace-to-a-string
\ No newline at end of file
From e9a6e23dacd1526010e96f1528a41d760f7348c4 Mon Sep 17 00:00:00 2001
From: giantray <51961070@qq.com>
Date: 2016年3月17日 18:31:56 +0800
Subject: [PATCH 073/195] =?UTF-8?q?=E4=BC=98=E5=8C=96=E2=80=9Chow-can-i-co?=
=?UTF-8?q?nvert-a-stack-trace-to-a-string=E2=80=9D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 2 +-
contents/how-can-i-convert-a-stack-trace-to-a-string.md | 4 ++--
2 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/README.md b/README.md
index 48fe6e6..1241de8 100644
--- a/README.md
+++ b/README.md
@@ -69,6 +69,7 @@ stackoverflow-Java-top-qa
* [如何创建单例](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/What_is_an_efficient_way_to_implement_a_singleton_in_Java.md)
* [实现Runnable接口 VS. 继承Thread类](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/implements-runnable-vs-extends-thread.md)
* [我应该用哪一个@NotNull注解](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/which-notnull-java-annotation-should-i-use.md)
+* [怎样将堆栈追踪信息转换为字符串](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-can-i-convert-a-stack-trace-to-a-string.md)
> 网络
@@ -108,7 +109,6 @@ stackoverflow-Java-top-qa
- [Comparing Java enum members: == or equals()?](http://stackoverflow.com/questions/1750435/comparing-java-enum-members-or-equals)
- [Failed to load the JNI shared Library (JDK)](http://stackoverflow.com/questions/7352493/failed-to-load-the-jni-shared-library-jdk)
- [How to create a Java String from the contents of a file?](http://stackoverflow.com/questions/326390/how-to-create-a-java-string-from-the-contents-of-a-file)
-- [How can I convert a stack trace to a string?](http://stackoverflow.com/questions/1149703/how-can-i-convert-a-stack-trace-to-a-string)
- [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)
- [What exactly is Apache Camel?](http://stackoverflow.com/questions/8845186/what-exactly-is-apache-camel)
- [How can I permanently have line numbers in IntelliJ?](http://stackoverflow.com/questions/13751/how-can-i-permanently-have-line-numbers-in-intellij)
diff --git a/contents/how-can-i-convert-a-stack-trace-to-a-string.md b/contents/how-can-i-convert-a-stack-trace-to-a-string.md
index 8e8df0a..7a15500 100644
--- a/contents/how-can-i-convert-a-stack-trace-to-a-string.md
+++ b/contents/how-can-i-convert-a-stack-trace-to-a-string.md
@@ -1,4 +1,4 @@
-#怎么样将堆栈追踪信息转换为字符串类型
+#怎样将堆栈追踪信息转换为字符串
##问题
将`Throwable.getStackTrace()`的结果转换为一个字符串来来描述堆栈信息的最简单的方法是什么
@@ -8,7 +8,7 @@
[`org.apache.commons.lang.exception.ExceptionUtils.getStackTrace(Throwable)`](org.apache.commons.lang.exception.ExceptionUtils.getStackTrace\(Throwable\))
###答案二
-用 [`Throwable.printStackTrace(PrintWriter pw)`](https://docs.oracle.com/javase/8/docs/api/java/lang/Throwable.html#printStackTrace-java.io.PrintWriter-)将对堆栈信息发送到一个输出中:
+用 [`Throwable.printStackTrace(PrintWriter pw)`](https://docs.oracle.com/javase/8/docs/api/java/lang/Throwable.html#printStackTrace-java.io.PrintWriter-)可以输出堆栈信息:
````java
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
From 89fc50e6133f7ea8bac5438770707bf4fe9ed0b0 Mon Sep 17 00:00:00 2001
From: giantray <51961070@qq.com>
Date: 2016年3月17日 18:48:52 +0800
Subject: [PATCH 074/195] =?UTF-8?q?=E4=BC=98=E5=8C=96=E2=80=9Cdealing-with?=
=?UTF-8?q?-java-lang-outofmemoryerror-permgen-space-error=E2=80=9C?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 2 +-
...ng.OutOfMemoryError PermGen space error.md | 28 -------------------
2 files changed, 1 insertion(+), 29 deletions(-)
delete mode 100644 contents/Dealing with java.lang.OutOfMemoryError PermGen space error.md
diff --git a/README.md b/README.md
index 1241de8..51e7259 100644
--- a/README.md
+++ b/README.md
@@ -70,6 +70,7 @@ stackoverflow-Java-top-qa
* [实现Runnable接口 VS. 继承Thread类](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/implements-runnable-vs-extends-thread.md)
* [我应该用哪一个@NotNull注解](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/which-notnull-java-annotation-should-i-use.md)
* [怎样将堆栈追踪信息转换为字符串](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-can-i-convert-a-stack-trace-to-a-string.md)
+* [如何处理 java.lang.outOfMemoryError PermGen space error](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/dealing-with-java-lang-outofmemoryerror-permgen-space-error.md)
> 网络
@@ -98,7 +99,6 @@ stackoverflow-Java-top-qa
- [Why is printing "B" dramatically slower than printing "#"?](http://stackoverflow.com/questions/21947452/why-is-printing-b-dramatically-slower-than-printing)
- [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)
- [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)
-- [Dealing with "java.lang.OutOfMemoryError: PermGen space" error](http://stackoverflow.com/questions/88235/dealing-with-java-lang-outofmemoryerror-permgen-space-error)
- [Convert a String to an enum in Java](http://stackoverflow.com/questions/604424/convert-a-string-to-an-enum-in-java)
- [Android SDK installation doesn't find JDK](http://stackoverflow.com/questions/4382178/android-sdk-installation-doesnt-find-jdk)
- [Java inner class and static nested class](http://stackoverflow.com/questions/70324/java-inner-class-and-static-nested-class)
diff --git a/contents/Dealing with java.lang.OutOfMemoryError PermGen space error.md b/contents/Dealing with java.lang.OutOfMemoryError PermGen space error.md
deleted file mode 100644
index 3cbb6c3..0000000
--- a/contents/Dealing with java.lang.OutOfMemoryError PermGen space error.md
+++ /dev/null
@@ -1,28 +0,0 @@
-##如何处理 java.lang.outOfMemoryError PermGen space error
-
-###问题
-
-最近,我在过运行我的web应用时得到:java.lang.OutOfMemoryError: PermGen space。
-我的应用用一个典型的 Hibernate/JPA + IceFaces/JSF的应用.运行于Tomcat6.0和jdk1.6.我发布了多次以后,产生了这个错误。
-
-是什么原因造成的,我如何避免?我怎样修复?
-
-
-#回答
-
-
-解决的方案是当TomeCat启时,在jvm的的命令行添加参数
-
- -XX:+CMSClassUnloadingEnabled -XX:+CMSPermGenSweepingEnabled
-
-
-你也可以停止tomcat的服务,直接进入Tomcat/bin目录,运行tomcat6w.exe.在Java的标签下,参加上面的参数。单击"OK",重新启动Tomcat的服务.
-
-
-如果你得到不一个未安装的服务,你可以运行:
-
- tomcat6w //ES//servicename
-
-servicename的名字你右以在services.msc中查看。
-
-
From 288d62cb360ed3870b6d1fc5c87579bede3e84c6 Mon Sep 17 00:00:00 2001
From: giantray <51961070@qq.com>
Date: 2016年3月17日 18:49:18 +0800
Subject: [PATCH 075/195] =?UTF-8?q?=E4=BC=98=E5=8C=96=E2=80=9Cdealing-with?=
=?UTF-8?q?-java-lang-outofmemoryerror-permgen-space-error=E2=80=9D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...ng-outofmemoryerror-permgen-space-error.md | 31 +++++++++++++++++++
1 file changed, 31 insertions(+)
create mode 100644 contents/dealing-with-java-lang-outofmemoryerror-permgen-space-error.md
diff --git a/contents/dealing-with-java-lang-outofmemoryerror-permgen-space-error.md b/contents/dealing-with-java-lang-outofmemoryerror-permgen-space-error.md
new file mode 100644
index 0000000..ffa7f7a
--- /dev/null
+++ b/contents/dealing-with-java-lang-outofmemoryerror-permgen-space-error.md
@@ -0,0 +1,31 @@
+##如何处理 java.lang.outOfMemoryError PermGen space error
+
+###问题
+
+最近,我在过运行我的web应用时得到:java.lang.OutOfMemoryError: PermGen space。
+我的应用是一个典型的 Hibernate/JPA + IceFaces/JSF的应用.运行于Tomcat6.0和jdk1.6.我发布了多次以后,产生了这个错误。
+
+是什么原因造成的,我如何避免?我怎样修复?
+
+
+#回答
+
+
+解决的方案是当TomeCat启时,在jvm的的命令行添加参数
+
+ -XX:+CMSClassUnloadingEnabled -XX:+CMSPermGenSweepingEnabled
+
+
+你也可以停止tomcat的服务,直接进入Tomcat/bin目录,运行tomcat6w.exe.在Java的标签下,加好上面的参数。单击"OK",重新启动Tomcat的服务.
+
+
+如果系统返回错误,提示指定的服务不存在,你可以运行:
+
+ tomcat6w //ES//servicename
+
+servicename的名字你可以在services.msc中查看。
+
+stackoverflow链接:
+http://stackoverflow.com/questions/88235/dealing-with-java-lang-outofmemoryerror-permgen-space-error
+
+
From 75341402cfb7ac1a9adb3873548e49b2af07ffdc Mon Sep 17 00:00:00 2001
From: NothingOne
Date: 2016年3月19日 22:12:01 +0800
Subject: [PATCH 076/195] Update Declare-array-in-Java.md
---
contents/Declare-array-in-Java.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/contents/Declare-array-in-Java.md b/contents/Declare-array-in-Java.md
index 0d5019a..1247686 100644
--- a/contents/Declare-array-in-Java.md
+++ b/contents/Declare-array-in-Java.md
@@ -17,7 +17,7 @@ int[] myIntArray = new int[]{1, 2, 3};
```
String[] myStringArray = new String[3];
String[] myStringArray = {"a", "b","c"};
-String[] myStringArray = new String[]("a", "b", "c");
+String[] myStringArray = new String[]{"a", "b", "c"};
```
[stackoverflow链接:Declare array in Java?](http://stackoverflow.com/questions/1200621/declare-array-in-java)
From 67d69fa6559005a581a32a7d12654a6f484293e1 Mon Sep 17 00:00:00 2001
From: giantray <51961070@qq.com>
Date: 2016年3月19日 23:36:50 +0800
Subject: [PATCH 077/195] Update README.md
---
README.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/README.md b/README.md
index 51e7259..35486fb 100644
--- a/README.md
+++ b/README.md
@@ -95,7 +95,7 @@ stackoverflow-Java-top-qa
### 待翻译问题链接(还剩x问题)
- [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)
- [Proper use cases for Android UserManager.isUserAGoat()?](http://stackoverflow.com/questions/13375357/proper-use-cases-for-android-usermanager-isuseragoat)
-- [Creating a memory leak with Java [closed]](http://stackoverflow.com/questions/6470651/creating-a-memory-leak-with-java)
+- [Creating a memory leak with Java [closed]](http://stackoverflow.com/questions/6470651/creating-a-memory-leak-with-java) 用户AutumnLight正在翻译该问题
- [Why is printing "B" dramatically slower than printing "#"?](http://stackoverflow.com/questions/21947452/why-is-printing-b-dramatically-slower-than-printing)
- [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)
- [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)
From 2117800db205c2ea067de6d5fc98a46e1d6b95c2 Mon Sep 17 00:00:00 2001
From: Zhao Lei
Date: 2016年3月21日 12:40:36 +0800
Subject: [PATCH 078/195] Creating a memory leak with Java
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
添加Creating a memory leak with Java [closed] 文章的翻译,目前只是把问题翻译了一下
---
contents/creating-a-memory-leak-with-java | 2 ++
1 file changed, 2 insertions(+)
create mode 100644 contents/creating-a-memory-leak-with-java
diff --git a/contents/creating-a-memory-leak-with-java b/contents/creating-a-memory-leak-with-java
new file mode 100644
index 0000000..133c557
--- /dev/null
+++ b/contents/creating-a-memory-leak-with-java
@@ -0,0 +1,2 @@
+如何使用Java创建一个内存泄漏的程序
+问题:我在一个面试的过程中被问到如何使用Java创建一个内存泄漏的程序。毫无疑问地说,我当时哑口无言,根本不知道如何开始编码。
From e459c4ca2d964f5dca4372baedf64de1a65b3ee4 Mon Sep 17 00:00:00 2001
From: Zhao Lei
Date: 2016年3月21日 15:07:11 +0800
Subject: [PATCH 079/195] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E5=9B=9E?=
=?UTF-8?q?=E7=AD=94?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
回答第一版,可能有些地方并不准确,需要继续完善。
---
contents/creating-a-memory-leak-with-java | 28 +++++++++++++++++++++--
1 file changed, 26 insertions(+), 2 deletions(-)
diff --git a/contents/creating-a-memory-leak-with-java b/contents/creating-a-memory-leak-with-java
index 133c557..8fc0a2b 100644
--- a/contents/creating-a-memory-leak-with-java
+++ b/contents/creating-a-memory-leak-with-java
@@ -1,2 +1,26 @@
-如何使用Java创建一个内存泄漏的程序
-问题:我在一个面试的过程中被问到如何使用Java创建一个内存泄漏的程序。毫无疑问地说,我当时哑口无言,根本不知道如何开始编码。
+# 如何使用Java创建一个内存泄漏的程序
+
+## 问题:
+我在一个面试的过程中被问到如何使用Java创建一个内存泄漏的程序。毫无疑问地说,我当时哑口无言,根本不知道如何开始编码。
+
+## 解答
+在Java下有一个很好的方法来创建内存泄漏程序--通过使得对象不可访问但任然存储在内存中。
+1. 应用程序创建一个长期运行的线程A 或者 使用一个线程池来加快泄漏的速度。
+2. 线程A使用ClassLoader(用户可以自定义)加载一个类 B
+3. 在类B申请一块很大的连续内存(例如:new byte[1000000]),
+并使用一个静态成员变量中存储该空间的一个强引用,之后在一个ThreadLocal中存储类B对象的引用。
+虽然泄漏这个类的一个实例就足够了,但是也可以通过申请多个实例的方法来加快内存泄漏的速度。
+4. 线程A清理所有指向自定义类或者通过ClassLoadeer加载的引用。
+5. 重复上述步骤
+
+上述方式可以达到内存泄漏的目的,因为 ThreadLocal 存储了一个指向类B对象的引用,
+这样就可以保存一个指向该类(类B)的引用,而类B又保存了一个指向其ClassLoader的引用。
+而ClassLoader又保存了一个通过它加载的所有类的引用。
+这种方法在许多的JVM的实现中表现很糟糕,因为Classes和ClassLoader被直接存储在老年代(permgen)并且永远都不会被GC处理。
+
+这个模式的一个变形:为什么应用容器(例如Tomcat)可以像筛子一样泄漏内存,如果你频繁的重新部署那些可能使用ThreadLocals的应用。
+因为应用容器使用上述所说的线程,每次重新部署应用是,应用容器都会使用一个新的ClassLoader。
+
+具体代码可以参考:https://gist.github.com/dpryden/b2bb29ee2d146901b4ae
+
+stackoverflow原址:http://stackoverflow.com/questions/6470651/creating-a-memory-leak-with-java
From 2dc09d98aff1a22efdd69ab51cc1959fbc702720 Mon Sep 17 00:00:00 2001
From: Zhao Lei
Date: 2016年3月21日 15:15:39 +0800
Subject: [PATCH 080/195] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=96=87=E4=BB=B6?=
=?UTF-8?q?=E5=90=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
给文件名添加了.md后缀,这样能显示mardown定义的格式。
---
...-memory-leak-with-java => creating-a-memory-leak-with-java.md} | 0
1 file changed, 0 insertions(+), 0 deletions(-)
rename contents/{creating-a-memory-leak-with-java => creating-a-memory-leak-with-java.md} (100%)
diff --git a/contents/creating-a-memory-leak-with-java b/contents/creating-a-memory-leak-with-java.md
similarity index 100%
rename from contents/creating-a-memory-leak-with-java
rename to contents/creating-a-memory-leak-with-java.md
From 2741d4a0dc8cb9b4f6f80d1d280134e4a544e9fe Mon Sep 17 00:00:00 2001
From: Zhao Lei
Date: 2016年3月21日 15:17:14 +0800
Subject: [PATCH 081/195] =?UTF-8?q?=E5=86=85=E5=AE=B9=E6=A0=BC=E5=BC=8F?=
=?UTF-8?q?=E4=BF=AE=E6=94=B9?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
在每一步后面添加了空白行,改进显示效果
---
contents/creating-a-memory-leak-with-java.md | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/contents/creating-a-memory-leak-with-java.md b/contents/creating-a-memory-leak-with-java.md
index 8fc0a2b..f763f30 100644
--- a/contents/creating-a-memory-leak-with-java.md
+++ b/contents/creating-a-memory-leak-with-java.md
@@ -5,12 +5,17 @@
## 解答
在Java下有一个很好的方法来创建内存泄漏程序--通过使得对象不可访问但任然存储在内存中。
+
1. 应用程序创建一个长期运行的线程A 或者 使用一个线程池来加快泄漏的速度。
+
2. 线程A使用ClassLoader(用户可以自定义)加载一个类 B
+
3. 在类B申请一块很大的连续内存(例如:new byte[1000000]),
并使用一个静态成员变量中存储该空间的一个强引用,之后在一个ThreadLocal中存储类B对象的引用。
虽然泄漏这个类的一个实例就足够了,但是也可以通过申请多个实例的方法来加快内存泄漏的速度。
+
4. 线程A清理所有指向自定义类或者通过ClassLoadeer加载的引用。
+
5. 重复上述步骤
上述方式可以达到内存泄漏的目的,因为 ThreadLocal 存储了一个指向类B对象的引用,
From ecc7b1e42c261fc5ea197d9efa6dce2f77215c5e Mon Sep 17 00:00:00 2001
From: Zhao Lei
Date: 2016年3月21日 15:54:25 +0800
Subject: [PATCH 082/195] =?UTF-8?q?=E6=9B=B4=E6=94=B9=E4=B8=8D=E5=BD=93?=
=?UTF-8?q?=E7=9A=84=E8=AF=B4=E6=B3=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
更改了一些不正确的说法,并且添加了一些个人理解
---
contents/creating-a-memory-leak-with-java.md | 14 +++++++++++---
1 file changed, 11 insertions(+), 3 deletions(-)
diff --git a/contents/creating-a-memory-leak-with-java.md b/contents/creating-a-memory-leak-with-java.md
index f763f30..1b5644d 100644
--- a/contents/creating-a-memory-leak-with-java.md
+++ b/contents/creating-a-memory-leak-with-java.md
@@ -8,10 +8,10 @@
1. 应用程序创建一个长期运行的线程A 或者 使用一个线程池来加快泄漏的速度。
-2. 线程A使用ClassLoader(用户可以自定义)加载一个类 B
+2. 线程A使用ClassLoader(用户可以自定义)加载一个类 B。
3. 在类B申请一块很大的连续内存(例如:new byte[1000000]),
-并使用一个静态成员变量中存储该空间的一个强引用,之后在一个ThreadLocal中存储类B对象的引用。
+并使用一个静态成员变量保存该空间的一个强引用,之后在一个ThreadLocal对象中存储类B对象的引用。
虽然泄漏这个类的一个实例就足够了,但是也可以通过申请多个实例的方法来加快内存泄漏的速度。
4. 线程A清理所有指向自定义类或者通过ClassLoadeer加载的引用。
@@ -19,13 +19,21 @@
5. 重复上述步骤
上述方式可以达到内存泄漏的目的,因为 ThreadLocal 存储了一个指向类B对象的引用,
-这样就可以保存一个指向该类(类B)的引用,而类B又保存了一个指向其ClassLoader的引用。
+而该对象又保存了一个指向其类的引用,这个类又保存了一个指向其ClassLoader的引用,
而ClassLoader又保存了一个通过它加载的所有类的引用。
这种方法在许多的JVM的实现中表现很糟糕,因为Classes和ClassLoader被直接存储在老年代(permgen)并且永远都不会被GC处理。
+******************************下方为个人理解************************************
+通过一个简单的图来描述上述关系:
+ThreadLocal.obj ---> B.obj ---> B.class <--> ClassLoader.obj
+注:上图的\*.obj表示\*类的一个实例对象,B.class表示类B的Class对象
+******************************上方为个人理解************************************
+
这个模式的一个变形:为什么应用容器(例如Tomcat)可以像筛子一样泄漏内存,如果你频繁的重新部署那些可能使用ThreadLocals的应用。
因为应用容器使用上述所说的线程,每次重新部署应用是,应用容器都会使用一个新的ClassLoader。
具体代码可以参考:https://gist.github.com/dpryden/b2bb29ee2d146901b4ae
+参考:http://frankkieviet.blogspot.com/2006/10/classloader-leaks-dreaded-permgen-space.html
+
stackoverflow原址:http://stackoverflow.com/questions/6470651/creating-a-memory-leak-with-java
From 4d6508b377492e678865929ff481f6bfc5aacd12 Mon Sep 17 00:00:00 2001
From: Zhao Lei
Date: 2016年3月21日 15:55:21 +0800
Subject: [PATCH 083/195] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=A0=BC=E5=BC=8F?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
对于个人理解部分修改了下格式
---
contents/creating-a-memory-leak-with-java.md | 2 ++
1 file changed, 2 insertions(+)
diff --git a/contents/creating-a-memory-leak-with-java.md b/contents/creating-a-memory-leak-with-java.md
index 1b5644d..91cfd5b 100644
--- a/contents/creating-a-memory-leak-with-java.md
+++ b/contents/creating-a-memory-leak-with-java.md
@@ -24,9 +24,11 @@
这种方法在许多的JVM的实现中表现很糟糕,因为Classes和ClassLoader被直接存储在老年代(permgen)并且永远都不会被GC处理。
******************************下方为个人理解************************************
+
通过一个简单的图来描述上述关系:
ThreadLocal.obj ---> B.obj ---> B.class <--> ClassLoader.obj
注:上图的\*.obj表示\*类的一个实例对象,B.class表示类B的Class对象
+
******************************上方为个人理解************************************
这个模式的一个变形:为什么应用容器(例如Tomcat)可以像筛子一样泄漏内存,如果你频繁的重新部署那些可能使用ThreadLocals的应用。
From 712391ed5b8ddd644c3ec6377e45151bee33f9f4 Mon Sep 17 00:00:00 2001
From: Zhao Lei
Date: 2016年3月21日 15:56:13 +0800
Subject: [PATCH 084/195] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=A0=BC=E5=BC=8F?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
个人理解部分添加一个
标签
---
contents/creating-a-memory-leak-with-java.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/contents/creating-a-memory-leak-with-java.md b/contents/creating-a-memory-leak-with-java.md
index 91cfd5b..2ef3ab6 100644
--- a/contents/creating-a-memory-leak-with-java.md
+++ b/contents/creating-a-memory-leak-with-java.md
@@ -26,7 +26,7 @@
******************************下方为个人理解************************************
通过一个简单的图来描述上述关系:
-ThreadLocal.obj ---> B.obj ---> B.class <--> ClassLoader.obj
+ThreadLocal.obj ---> B.obj ---> B.class <--> ClassLoader.obj
注:上图的\*.obj表示\*类的一个实例对象,B.class表示类B的Class对象
******************************上方为个人理解************************************
From 5390916cba2de0a6e42965e5b0287088dacb5c70 Mon Sep 17 00:00:00 2001
From: AutumnLight
Date: 2016年3月21日 22:24:16 +0800
Subject: [PATCH 085/195] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=B8=80=E4=BA=9B?=
=?UTF-8?q?=E8=AF=AD=E5=8F=A5?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
修改一些不合适的语句
---
contents/creating-a-memory-leak-with-java.md | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/contents/creating-a-memory-leak-with-java.md b/contents/creating-a-memory-leak-with-java.md
index 2ef3ab6..267a4db 100644
--- a/contents/creating-a-memory-leak-with-java.md
+++ b/contents/creating-a-memory-leak-with-java.md
@@ -21,18 +21,18 @@
上述方式可以达到内存泄漏的目的,因为 ThreadLocal 存储了一个指向类B对象的引用,
而该对象又保存了一个指向其类的引用,这个类又保存了一个指向其ClassLoader的引用,
而ClassLoader又保存了一个通过它加载的所有类的引用。
-这种方法在许多的JVM的实现中表现很糟糕,因为Classes和ClassLoader被直接存储在老年代(permgen)并且永远都不会被GC处理。
+这种方法在许多的JVM的实现中表现更糟糕,因为Classes和ClassLoader被直接存储在老年代(permgen)并且永远都不会被GC处理。
******************************下方为个人理解************************************
-通过一个简单的图来描述上述关系:
+通过一个简单的图来描述上述关系:
ThreadLocal.obj ---> B.obj ---> B.class <--> ClassLoader.obj
注:上图的\*.obj表示\*类的一个实例对象,B.class表示类B的Class对象
******************************上方为个人理解************************************
-这个模式的一个变形:为什么应用容器(例如Tomcat)可以像筛子一样泄漏内存,如果你频繁的重新部署那些可能使用ThreadLocals的应用。
-因为应用容器使用上述所说的线程,每次重新部署应用是,应用容器都会使用一个新的ClassLoader。
+这个模式的一个变形:如果频繁的重新部署那些可能使用ThreadLocals的应用,应用容器(例如Tomcat)就会像筛子一样泄漏内存。
+因为应用容器使用上述所说的线程,每次重新部署应用时,应用容器都会使用一个新的ClassLoader。
具体代码可以参考:https://gist.github.com/dpryden/b2bb29ee2d146901b4ae
From 796f78e8080f3fc592e7a7783bfd6f67abb11e01 Mon Sep 17 00:00:00 2001
From: AutumnLight
Date: 2016年3月22日 16:54:47 +0800
Subject: [PATCH 086/195] =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=BF=AE=E6=94=B9:?=
=?UTF-8?q?=20=E4=BF=AE=E6=94=B9=E4=B8=8D=E7=AC=A6=E5=90=88=E6=A0=BC?=
=?UTF-8?q?=E5=BC=8F=E7=9A=84=E6=96=87=E4=BB=B6=E5=90=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 37 ++++++++++---------
...ast-two-out-of-three-booleans-are-true.md} | 0
...ay-t.md => create-arraylist-from-array.md} | 0
...ay-in-Java.md => declare-array-in-java.md} | 0
...=> does-finally-always-execute-in-java.md} | 0
...-java-support-default-parameter-values.md} | 0
...owing-the-progress-in-a-progressdialog.md} | 0
...esign-patterns-in-javas-core-libraries.md} | 0
...d => how-can-i-initialize-a-static-map.md} | 0
...t-if-an-array-contains-a-certain-value.md} | 0
...l-one-constructor-from-another-in-java.md} | 0
...generate-a-random-alpha-numeric-string.md} | 0
...rivate-methods-fields-or-inner-classes.md} | 0
...rialversionuid-and-why-should-i-use-it.md} | 0
...t-way-to-implement-a-singleton-in-java.md} | 0
...hat-is-reflection-and-why-is-it-useful.md} | 0
...the-simplest-way-to-print-a-java-array.md} | 0
...ng.md => why-cant-i-switch-on-a-string.md} | 0
...rred-over-string-for-passwords-in-java.md} | 0
...ed-array-faster-than-an-unsorted-array.md} | 0
20 files changed, 19 insertions(+), 18 deletions(-)
rename contents/{Check-if-at-least-two-out-of-three-booleans-are-true.md => check-if-at-least-two-out-of-three-booleans-are-true.md} (100%)
rename contents/{create-arraylist-arraylistt-from-array-t.md => create-arraylist-from-array.md} (100%)
rename contents/{Declare-array-in-Java.md => declare-array-in-java.md} (100%)
rename contents/{Does-finally-always-execute-in-Java.md => does-finally-always-execute-in-java.md} (100%)
rename contents/{Does-Java-support-default-parameter-values.md => does-java-support-default-parameter-values.md} (100%)
rename contents/{Download_a_file_with_Android_and_showing_the_progress_in_a_ProgressDialog.md => download-a-file-with-android-and-showing-the-progress-in-a-progressdialog.md} (100%)
rename contents/{Examples_of_GoF_Design_Patterns_in_Java's_core_libraries.md => examples-of-gof-design-patterns-in-javas-core-libraries.md} (100%)
rename contents/{How-can-I-Initialize-a-static-Map.md => how-can-i-initialize-a-static-map.md} (100%)
rename contents/{How-can-I-test-if-an-array-contains-a-certain-value.md => how-can-i-test-if-an-array-contains-a-certain-value.md} (100%)
rename contents/{How-do-i-call-one-constructor-from-another-in-java.md => how-do-i-call-one-constructor-from-another-in-java.md} (100%)
rename contents/{How_to_generate_a_random_alpha-numeric_string.md => how-to-generate-a-random-alpha-numeric-string.md} (100%)
rename contents/{How_to_test_a_class_that_has_private_methods,_fields_or_inner_classes.md => how-to-test-a-class-that-has-private-methods-fields-or-inner-classes.md} (100%)
rename contents/{What_is_a_serialVersionUID_and_why_should_I_use_it.md => what-is-a-serialversionuid-and-why-should-i-use-it.md} (100%)
rename contents/{What_is_an_efficient_way_to_implement_a_singleton_in_Java.md => what-is-an-efficient-way-to-implement-a-singleton-in-java.md} (100%)
rename contents/{What-is-reflection-and-why-is-it-useful.md => what-is-reflection-and-why-is-it-useful.md} (100%)
rename contents/{What's-the-simplest-way-to-print-a-Java-array.md => whats-the-simplest-way-to-print-a-java-array.md} (100%)
rename contents/{Why-can't-I-switch-on-a-String.md => why-cant-i-switch-on-a-string.md} (100%)
rename contents/{why-is-cha[]-preferred-over-String-for-passwords-in-java.md => why-is-char-preferred-over-string-for-passwords-in-java.md} (100%)
rename contents/{Why-is-processing-a-sorted-array-faster-than-an-unsorted-array.md => why-is-processing-a-sorted-array-faster-than-an-unsorted-array.md} (100%)
diff --git a/README.md b/README.md
index 35486fb..053f033 100644
--- a/README.md
+++ b/README.md
@@ -28,10 +28,10 @@ stackoverflow-Java-top-qa
* [Java += 操作符实质](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/java-operator.md)
* [将InputStream转换为String](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/read-convert-an-inputstream-to-a-string.md)
-* [将数组转换为List](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/create-arraylist-arraylistt-from-array-t.md)
+* [将数组转换为List](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/create-arraylist-from-array.md)
* [如何遍历map对象](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/iterate-through-a-hashmap.md)
* [public,protected,private,不加修饰符。有什么区别呢?](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/in-java-whats-the-difference-between-public-default-protected-and-private.md)
-* [如何测试一个数组是否包含指定的值?](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/How-can-I-test-if-an-array-contains-a-certain-value.md)
+* [如何测试一个数组是否包含指定的值?](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-can-i-test-if-an-array-contains-a-certain-value.md)
* [重写(Override)equlas和hashCode方法时应考虑的问题](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/what-issues-should-be-considered-when-overriding-equals-and-hashcode-in-java.md)
* [从一个多层嵌套循环中直接跳出](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/breaking-out-of-nested-loops-in-java.md)
* [如何将String转换为Int](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/converting-string-to-int-in-java.md)
@@ -40,33 +40,34 @@ stackoverflow-Java-top-qa
* [`Map`基于Value值排序](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-to-sort-a-mapkey-value-on-the-values-in-java.md)
* [`HashMap和Hashtable的区别](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/differences-between-hashmap-and-hashtable.md)
* [如何便捷地将两个数组合到一起](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-to-concatenate-two-arrays-in-java.md)
-* [Java 是否支持默认的参数值](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Does-Java-support-default-parameter-values.md)
+* [Java 是否支持默认的参数值](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/does-java-support-default-parameter-values.md)
* [Java 产生指定范围的随机数](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/generating-random-integers-in-a-range-with-Java.md)
* [JavaBean 到底是什么](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/what-is-a-javabean-exactly.md)
* [wait()和sleep()的区别](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/difference-between-wait-and-sleep.md)
-* [能否在一个构造器( `constructor` )中调用另一个构造器](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/How-do-i-call-one-constructor-from-another-in-java.md)
-* [ `finally` 代码块总会被执行么](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Does-finally-always-execute-in-Java.md)
+* [能否在一个构造器( `constructor` )中调用另一个构造器](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-do-i-call-one-constructor-from-another-in-java.md)
+* [ `finally` 代码块总会被执行么](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/does-finally-always-execute-in-java.md)
* [如何将String转换为enum](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/convert-a-string-to-an-enum-in-java.md)
-* [在Java中声明数组](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Declare-array-in-Java.md)
-* [反射是什么及其用途](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/What-is-reflection-and-why-is-it-useful.md)
-* [为什么不能用string类型进行switch判断](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Why-can't-I-switch-on-a-String.md)
+* [在Java中声明数组](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/declare-array-in-java.md)
+* [反射是什么及其用途](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/what-is-reflection-and-why-is-it-useful.md)
+* [为什么不能用string类型进行switch判断](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/why-cant-i-switch-on-a-string.md)
* [比较java枚举成员使用equal还是==](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/comparing-java-enum-members-or-equals.md)
* [用java怎么创建一个文件并向该文件写文本内容](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-to-create-a-file-and-write-to-a-file-in-java.md)
+* [serialVersionUID 有什么作用?该如何使用?](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/what-is-a-serialversionuid-and-why-should-i-use-it.md)
> 编程技巧
* [去掉烦人的"!=null"(判空语句](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/avoiding-null-statements-in-java.md)
* [获取完整的堆栈信息](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/get-current-stack-trace-in-java.md)
* [如何用一行代码初始化一个ArrayList](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/initialization-of-an-arraylist-in-one-line.md)
-* [初始化静态map](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/How-can-I-Initialize-a-static-Map.md)
-* [给3个布尔变量,当其中有2个或者2个以上为true才返回true](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Check-if-at-least-two-out-of-three-booleans-are-true.md)
-* [输出 Java 数组最简单的方式](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/What's-the-simplest-way-to-print-a-Java-array.md)
+* [初始化静态map](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-can-i-initialize-a-static-map.md)
+* [给3个布尔变量,当其中有2个或者2个以上为true才返回true](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/check-if-at-least-two-out-of-three-booleans-are-true.md)
+* [输出 Java 数组最简单的方式](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/whats-the-simplest-way-to-print-a-java-array.md)
* [为什么以下用随机生成的文字会得出 "hello world"?](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/why-does-this-code-using-random-strings-print-hello-world.md)
-* [什么在java中存放密码更倾向于char[]而不是String](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/why-is-cha[]-preferred-over-String-for-passwords-in-java.md)
+* [什么在java中存放密码更倾向于char[]而不是String](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/why-is-char-preferred-over-string-for-passwords-in-java.md)
* [如何避免在JSP文件中使用Java代码](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-to-avoid-java-code-in-jsp-files.md)
-* [Java 源码里的设计模式](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Examples_of_GoF_Design_Patterns_in_Java's_core_libraries.md)
-* [如何产生一个随机的字母数字串作为 session 的唯一标识符](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/How_to_generate_a_random_alpha-numeric_string.md)
-* [如何创建单例](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/What_is_an_efficient_way_to_implement_a_singleton_in_Java.md)
+* [Java 源码里的设计模式](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/examples-of-gof-design-patterns-in-javas-core-libraries.md)
+* [如何产生一个随机的字母数字串作为 session 的唯一标识符](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-to-generate-a-random-alpha-numeric-string.md)
+* [如何创建单例](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/what-is-an-efficient-way-to-implement-a-singleton-in-java.md)
* [实现Runnable接口 VS. 继承Thread类](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/implements-runnable-vs-extends-thread.md)
* [我应该用哪一个@NotNull注解](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/which-notnull-java-annotation-should-i-use.md)
* [怎样将堆栈追踪信息转换为字符串](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-can-i-convert-a-stack-trace-to-a-string.md)
@@ -81,15 +82,15 @@ stackoverflow-Java-top-qa
* [LinkedList、ArrayList各自的使用场景,如何确认应该用哪一个呢?](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/when-to-use-linkedlist-over-arraylist.md)
* [StringBuilder和StringBuffer有哪些区别呢](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/stringbuilder-and-stringbuffer.md)
-* [为什么处理排序的数组要比非排序的快](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Why-is-processing-a-sorted-array-faster-than-an-unsorted-array.md)
+* [为什么处理排序的数组要比非排序的快](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/why-is-processing-a-sorted-array-faster-than-an-unsorted-array.md)
> 测试
-* [如何测试 private 方法,变量或者内部类](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/How_to_test_a_class_that_has_private_methods,_fields_or_inner_classes.md)
+* [如何测试 private 方法,变量或者内部类](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-to-test-a-class-that-has-private-methods-fields-or-inner-classes.md)
> Android
-* [在Android里面下载文件,并在ProgressDialog显示进度](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Download_a_file_with_Android_and_showing_the_progress_in_a_ProgressDialog.md)
+* [在Android里面下载文件,并在ProgressDialog显示进度](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog.md)
* [如何获取Android设备唯一ID](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/is-there-a-unique-android-device-id.md)
### 待翻译问题链接(还剩x问题)
diff --git a/contents/Check-if-at-least-two-out-of-three-booleans-are-true.md b/contents/check-if-at-least-two-out-of-three-booleans-are-true.md
similarity index 100%
rename from contents/Check-if-at-least-two-out-of-three-booleans-are-true.md
rename to contents/check-if-at-least-two-out-of-three-booleans-are-true.md
diff --git a/contents/create-arraylist-arraylistt-from-array-t.md b/contents/create-arraylist-from-array.md
similarity index 100%
rename from contents/create-arraylist-arraylistt-from-array-t.md
rename to contents/create-arraylist-from-array.md
diff --git a/contents/Declare-array-in-Java.md b/contents/declare-array-in-java.md
similarity index 100%
rename from contents/Declare-array-in-Java.md
rename to contents/declare-array-in-java.md
diff --git a/contents/Does-finally-always-execute-in-Java.md b/contents/does-finally-always-execute-in-java.md
similarity index 100%
rename from contents/Does-finally-always-execute-in-Java.md
rename to contents/does-finally-always-execute-in-java.md
diff --git a/contents/Does-Java-support-default-parameter-values.md b/contents/does-java-support-default-parameter-values.md
similarity index 100%
rename from contents/Does-Java-support-default-parameter-values.md
rename to contents/does-java-support-default-parameter-values.md
diff --git a/contents/Download_a_file_with_Android_and_showing_the_progress_in_a_ProgressDialog.md b/contents/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog.md
similarity index 100%
rename from contents/Download_a_file_with_Android_and_showing_the_progress_in_a_ProgressDialog.md
rename to contents/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog.md
diff --git a/contents/Examples_of_GoF_Design_Patterns_in_Java's_core_libraries.md b/contents/examples-of-gof-design-patterns-in-javas-core-libraries.md
similarity index 100%
rename from contents/Examples_of_GoF_Design_Patterns_in_Java's_core_libraries.md
rename to contents/examples-of-gof-design-patterns-in-javas-core-libraries.md
diff --git a/contents/How-can-I-Initialize-a-static-Map.md b/contents/how-can-i-initialize-a-static-map.md
similarity index 100%
rename from contents/How-can-I-Initialize-a-static-Map.md
rename to contents/how-can-i-initialize-a-static-map.md
diff --git a/contents/How-can-I-test-if-an-array-contains-a-certain-value.md b/contents/how-can-i-test-if-an-array-contains-a-certain-value.md
similarity index 100%
rename from contents/How-can-I-test-if-an-array-contains-a-certain-value.md
rename to contents/how-can-i-test-if-an-array-contains-a-certain-value.md
diff --git a/contents/How-do-i-call-one-constructor-from-another-in-java.md b/contents/how-do-i-call-one-constructor-from-another-in-java.md
similarity index 100%
rename from contents/How-do-i-call-one-constructor-from-another-in-java.md
rename to contents/how-do-i-call-one-constructor-from-another-in-java.md
diff --git a/contents/How_to_generate_a_random_alpha-numeric_string.md b/contents/how-to-generate-a-random-alpha-numeric-string.md
similarity index 100%
rename from contents/How_to_generate_a_random_alpha-numeric_string.md
rename to contents/how-to-generate-a-random-alpha-numeric-string.md
diff --git a/contents/How_to_test_a_class_that_has_private_methods,_fields_or_inner_classes.md b/contents/how-to-test-a-class-that-has-private-methods-fields-or-inner-classes.md
similarity index 100%
rename from contents/How_to_test_a_class_that_has_private_methods,_fields_or_inner_classes.md
rename to contents/how-to-test-a-class-that-has-private-methods-fields-or-inner-classes.md
diff --git a/contents/What_is_a_serialVersionUID_and_why_should_I_use_it.md b/contents/what-is-a-serialversionuid-and-why-should-i-use-it.md
similarity index 100%
rename from contents/What_is_a_serialVersionUID_and_why_should_I_use_it.md
rename to contents/what-is-a-serialversionuid-and-why-should-i-use-it.md
diff --git a/contents/What_is_an_efficient_way_to_implement_a_singleton_in_Java.md b/contents/what-is-an-efficient-way-to-implement-a-singleton-in-java.md
similarity index 100%
rename from contents/What_is_an_efficient_way_to_implement_a_singleton_in_Java.md
rename to contents/what-is-an-efficient-way-to-implement-a-singleton-in-java.md
diff --git a/contents/What-is-reflection-and-why-is-it-useful.md b/contents/what-is-reflection-and-why-is-it-useful.md
similarity index 100%
rename from contents/What-is-reflection-and-why-is-it-useful.md
rename to contents/what-is-reflection-and-why-is-it-useful.md
diff --git a/contents/What's-the-simplest-way-to-print-a-Java-array.md b/contents/whats-the-simplest-way-to-print-a-java-array.md
similarity index 100%
rename from contents/What's-the-simplest-way-to-print-a-Java-array.md
rename to contents/whats-the-simplest-way-to-print-a-java-array.md
diff --git a/contents/Why-can't-I-switch-on-a-String.md b/contents/why-cant-i-switch-on-a-string.md
similarity index 100%
rename from contents/Why-can't-I-switch-on-a-String.md
rename to contents/why-cant-i-switch-on-a-string.md
diff --git a/contents/why-is-cha[]-preferred-over-String-for-passwords-in-java.md b/contents/why-is-char-preferred-over-string-for-passwords-in-java.md
similarity index 100%
rename from contents/why-is-cha[]-preferred-over-String-for-passwords-in-java.md
rename to contents/why-is-char-preferred-over-string-for-passwords-in-java.md
diff --git a/contents/Why-is-processing-a-sorted-array-faster-than-an-unsorted-array.md b/contents/why-is-processing-a-sorted-array-faster-than-an-unsorted-array.md
similarity index 100%
rename from contents/Why-is-processing-a-sorted-array-faster-than-an-unsorted-array.md
rename to contents/why-is-processing-a-sorted-array-faster-than-an-unsorted-array.md
From 8c9a864d8835cb74122776d613e36b1930cefaca Mon Sep 17 00:00:00 2001
From: AutumnLight
Date: 2016年3月22日 16:56:42 +0800
Subject: [PATCH 087/195] =?UTF-8?q?=E5=86=85=E5=AE=B9=E6=A0=BC=E5=BC=8F?=
=?UTF-8?q?=E4=BF=AE=E6=94=B9:=20=E6=B7=BB=E5=8A=A0=E4=BA=86=E9=97=AE?=
=?UTF-8?q?=E9=A2=98=E5=92=8C=E7=AD=94=E6=A1=88=E4=B8=A4=E4=B8=AA=E4=BA=8C?=
=?UTF-8?q?=E7=BA=A7=E6=A0=87=E9=A2=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../what-is-a-serialversionuid-and-why-should-i-use-it.md | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/contents/what-is-a-serialversionuid-and-why-should-i-use-it.md b/contents/what-is-a-serialversionuid-and-why-should-i-use-it.md
index 192d129..645bd54 100644
--- a/contents/what-is-a-serialversionuid-and-why-should-i-use-it.md
+++ b/contents/what-is-a-serialversionuid-and-why-should-i-use-it.md
@@ -1,7 +1,9 @@
-# serialVersionUID 有什么作用?该如何使用?
+# serialVersionUID 有什么作用?该如何使用?
+##问题
当一个对象实现 Serializable 接口时,多数 ide 会提示声明一个静态常量 serialVersionUID(版本标识),那 serialVersionUID 到底有什么作用呢?应该如何使用 serialVersionUID ?
+##回答
serialVersionUID 是实现 Serializable 接口而来的,而 Serializable 则是应用于Java 对象序列化/反序列化。对象的序列化主要有两种用途:
- 把对象序列化成字节码,保存到指定介质上(如磁盘等)
From 342ea67efc8bb63660a5deed8335c5f3e731e9b8 Mon Sep 17 00:00:00 2001
From: Chen Xie
Date: Mon, 4 Apr 2016 16:03:21 -0700
Subject: [PATCH 088/195] Create
why-is-printing-b-dramatically-slower-than-printing.md
---
...ing-b-dramatically-slower-than-printing.md | 64 +++++++++++++++++++
1 file changed, 64 insertions(+)
create mode 100644 contents/why-is-printing-b-dramatically-slower-than-printing.md
diff --git a/contents/why-is-printing-b-dramatically-slower-than-printing.md b/contents/why-is-printing-b-dramatically-slower-than-printing.md
new file mode 100644
index 0000000..8df683f
--- /dev/null
+++ b/contents/why-is-printing-b-dramatically-slower-than-printing.md
@@ -0,0 +1,64 @@
+# 为什么打印"B"会明显的比打印"#"慢
+
+## 问题
+
+我生成了两个`1000`x`1000`的矩阵:
+
+第一个矩阵:`O`和`#`。
+第二个矩阵:`O`和`B`。
+
+使用如下的代码,生成第一个矩阵需要8.52秒:
+
+ Random r = new Random();
+ for (int i = 0; i < 1000; i++) { + for (int j = 0; j < 1000; j++) { + if(r.nextInt(4) == 0) { + System.out.print("O"); + } else { + System.out.print("#"); + } + } + + System.out.println(""); + } + + +而使用这段代码,生成第二个矩阵花费了259.152秒: + + Random r = new Random(); + for (int i = 0; i < 1000; i++) { + for (int j = 0; j < 1000; j++) { + if(r.nextInt(4) == 0) { + System.out.print("O"); + } else { + System.out.print("B"); //only line changed + } + } + + System.out.println(""); + } + +如此大的运行时间差异的背后究竟是什么原因呢? + +--- + +正如评论中所建议的,只打印`System.out.print("#");`用时7.8871秒,而`System.out.print("B");`则给出`still printing...`。 + +另外有人指出这段代码对他们来说是正常的, 我使用了[Ideone.com](http://ideone.com),这两段代码的执行速度是相同的。 + +测试条件: + + - 我在Netbeans 7.2中运行测试,由控制台显示输出 + - 我使用了`System.nanoTime()`来计算时间 + + ## 解答 + + *纯粹的推测*是因为你使用的终端尝试使用[单词换行][1]而不是字符换行,并且它认为`B`是一个单词而`#`却不是。所以当它到达行尾并且寻找一个换行的地方的时候,如果是`#`就可以马上换行;而如果是`B`,它则需要花更长时间搜索,因为可能会有更多的内容才能换行(在某些终端会非常费时,比如说它会先输出退格再输出空格去覆盖被换行的那部分字符)。 + +但这都只是纯粹的推测。 + + + [1]: http://en.wikipedia.org/wiki/Word_wrap + + +stackoverflow原址:http://stackoverflow.com/questions/21947452/why-is-printing-b-dramatically-slower-than-printing From 92e3f0247c18d63e258977002cb0c8be6159d510 Mon Sep 17 00:00:00 2001 From: Chen Xie
Date: Mon, 4 Apr 2016 16:06:44 -0700
Subject: [PATCH 089/195] remove unnecessary spaces
---
.../why-is-printing-b-dramatically-slower-than-printing.md | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/contents/why-is-printing-b-dramatically-slower-than-printing.md b/contents/why-is-printing-b-dramatically-slower-than-printing.md
index 8df683f..7ff8dfd 100644
--- a/contents/why-is-printing-b-dramatically-slower-than-printing.md
+++ b/contents/why-is-printing-b-dramatically-slower-than-printing.md
@@ -51,9 +51,9 @@
- 我在Netbeans 7.2中运行测试,由控制台显示输出
- 我使用了`System.nanoTime()`来计算时间
- ## 解答
+## 解答
- *纯粹的推测*是因为你使用的终端尝试使用[单词换行][1]而不是字符换行,并且它认为`B`是一个单词而`#`却不是。所以当它到达行尾并且寻找一个换行的地方的时候,如果是`#`就可以马上换行;而如果是`B`,它则需要花更长时间搜索,因为可能会有更多的内容才能换行(在某些终端会非常费时,比如说它会先输出退格再输出空格去覆盖被换行的那部分字符)。
+*纯粹的推测*是因为你使用的终端尝试使用[单词换行][1]而不是字符换行,并且它认为`B`是一个单词而`#`却不是。所以当它到达行尾并且寻找一个换行的地方的时候,如果是`#`就可以马上换行;而如果是`B`,它则需要花更长时间搜索,因为可能会有更多的内容才能换行(在某些终端会非常费时,比如说它会先输出退格再输出空格去覆盖被换行的那部分字符)。
但这都只是纯粹的推测。
From dd3587c860b86b36f2ce4f133265827ddbcbaf7f Mon Sep 17 00:00:00 2001
From: Chen Xie
Date: Mon, 4 Apr 2016 17:51:43 -0700
Subject: [PATCH 090/195] Update
why-is-printing-b-dramatically-slower-than-printing.md
---
...ing-b-dramatically-slower-than-printing.md | 52 ++++++++++++++++++-
1 file changed, 51 insertions(+), 1 deletion(-)
diff --git a/contents/why-is-printing-b-dramatically-slower-than-printing.md b/contents/why-is-printing-b-dramatically-slower-than-printing.md
index 7ff8dfd..970dbde 100644
--- a/contents/why-is-printing-b-dramatically-slower-than-printing.md
+++ b/contents/why-is-printing-b-dramatically-slower-than-printing.md
@@ -51,7 +51,7 @@
- 我在Netbeans 7.2中运行测试,由控制台显示输出
- 我使用了`System.nanoTime()`来计算时间
-## 解答
+## 解答一
*纯粹的推测*是因为你使用的终端尝试使用[单词换行][1]而不是字符换行,并且它认为`B`是一个单词而`#`却不是。所以当它到达行尾并且寻找一个换行的地方的时候,如果是`#`就可以马上换行;而如果是`B`,它则需要花更长时间搜索,因为可能会有更多的内容才能换行(在某些终端会非常费时,比如说它会先输出退格再输出空格去覆盖被换行的那部分字符)。
@@ -61,4 +61,54 @@
[1]: http://en.wikipedia.org/wiki/Word_wrap
+##解答二
+
+我用Eclipse和Netbeans 8.0.2做了测试,他们的Java版本都是1.8;我用了`System.nanoTime()`来计时。
+
+##Eclipse:
+
+我得到了**用时相同的结果** - 大约**1.564秒**。
+
+##Netbeans:
+
+* 使用"#": **1.536秒**
+* 使用"B": **44.164秒**
+
+所以看起来像是Netbeans输出到控制台的性能问题。
+
+在做了更多研究以后我发现问题所在是Netbeans [换行][1] 的最大缓存(这并不限于`System.out.println`命令),参见以下代码:
+
+ for (int i = 0; i < 1000; i++) { + long t1 = System.nanoTime(); + System.out.print("BBB......BBB"); \\<-contain 1000 "B" + long t2 = System.nanoTime(); + System.out.println(t2-t1); + System.out.println(""); + } + +每一个循环所花费的时间都不到1毫秒,除了 **每第五个循环**会花掉大约225毫秒。像这样(单位是毫秒): + + BBB...31744 + BBB...31744 + BBB...31744 + BBB...31744 + BBB...226365807 + BBB...31744 + BBB...31744 + BBB...31744 + BBB...31744 + BBB...226365807 + . + . + . + +以此类推。 + +##总结: + +1. 使用Eclipse打印"B"完全没有问题 +1. Netbeans有换行的问题但是可以被解决(因为在Eclipse并没有这个问题)(而不用在B后面添加空格("B "))。 + + [1]: http://en.wikipedia.org/wiki/Line_wrap_and_word_wrap + stackoverflow原址:http://stackoverflow.com/questions/21947452/why-is-printing-b-dramatically-slower-than-printing From 463f6335211454da3adf08dafff11f833f17ecd9 Mon Sep 17 00:00:00 2001 From: Chen Xie
Date: Tue, 5 Apr 2016 15:48:04 -0700
Subject: [PATCH 091/195] Create
how-can-i-pad-an-integers-with-zeros-on-the-left.md
---
...i-pad-an-integers-with-zeros-on-the-left.md | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
create mode 100644 contents/how-can-i-pad-an-integers-with-zeros-on-the-left.md
diff --git a/contents/how-can-i-pad-an-integers-with-zeros-on-the-left.md b/contents/how-can-i-pad-an-integers-with-zeros-on-the-left.md
new file mode 100644
index 0000000..27809e0
--- /dev/null
+++ b/contents/how-can-i-pad-an-integers-with-zeros-on-the-left.md
@@ -0,0 +1,18 @@
+# 如何用0向左补齐一个整数?
+
+## 问题
+
+在Java中如何把一个整数转化为字符串并且用0向左补齐呢?
+
+我基本上是希望把一个不大于9999的整数用0补齐 (比如说1="0001")。
+
+## 解答
+
+ String.format("%05d", yournumber);
+
+可以将一个五位数用0补齐。
+
+
+https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html
+
+Stackoverflow链接:http://stackoverflow.com/questions/473282/how-can-i-pad-an-integers-with-zeros-on-the-left
From 79a74a6783d48f65c522ed75167ec059162534a7 Mon Sep 17 00:00:00 2001
From: jam
Date: 2016年4月17日 20:22:17 +0800
Subject: [PATCH 092/195] =?UTF-8?q?java=E7=9A=84vector=E7=B1=BB=E4=B8=BA?=
=?UTF-8?q?=E4=BB=80=E4=B9=88=E8=A2=AB=E8=AE=A4=E4=B8=BA=E8=BF=87=E6=97=B6?=
=?UTF-8?q?=E4=BA=86?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../why-is-java-vector-class-considered-obsolete-or-deprecated.md | 0
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 contents/why-is-java-vector-class-considered-obsolete-or-deprecated.md
diff --git a/contents/why-is-java-vector-class-considered-obsolete-or-deprecated.md b/contents/why-is-java-vector-class-considered-obsolete-or-deprecated.md
new file mode 100644
index 0000000..e69de29
From 9f94d699bda038e20b09f42c3fa3b347d0110307 Mon Sep 17 00:00:00 2001
From: jssyjam
Date: 2016年4月17日 20:24:12 +0800
Subject: [PATCH 093/195] Update
why-is-java-vector-class-considered-obsolete-or-deprecated.md
---
...class-considered-obsolete-or-deprecated.md | 21 +++++++++++++++++++
1 file changed, 21 insertions(+)
diff --git a/contents/why-is-java-vector-class-considered-obsolete-or-deprecated.md b/contents/why-is-java-vector-class-considered-obsolete-or-deprecated.md
index e69de29..5e23388 100644
--- a/contents/why-is-java-vector-class-considered-obsolete-or-deprecated.md
+++ b/contents/why-is-java-vector-class-considered-obsolete-or-deprecated.md
@@ -0,0 +1,21 @@
+## 为什么Java的```Vector```类被认为是过时的或者废弃的
+### 问题
+为什么java ```Vector```类被认为是一个遗留的,过时的或废弃的类?在并发操作时,使用它是无效的吗?
+
+如果我不想手动对对象实现同步,只想用一个线程安全的集合而无需创建底层数组的全新副本(如```CopyOnWriteArrayList```一样)。这种情况下,我使用```Vector```合理吗?
+
+然后就是关于栈的问题,它是Vector的一个子类,我应该用什么代替它?
+### 回答
+Vector中对每一个独立操作都实现了同步,这通常不是我们想要的做法。对单一操作实现同步通常不是线程安全的(举个例子,比如你想遍历一个Vector实例。你仍然需要申明一个锁来防止其他线程在同一时刻修改这个Vector实例。如果不添加锁的话
+
+通常会在遍历实例的这个线程中导致一个``` ConcurrentModificationException ```)同时这个操作也是十分慢的(在创建了一个锁就已经足够的前提下,为什么还需要重复的创建锁)
+
+当然,即使你不需要同步,Vector也是有锁的资源开销的。
+
+总的来说,在大多数情况下,这种同步方法是存在很大缺陷的。正如Mr Brain Henk指出,你可以通过调用```Collections.synchronizedList```来装饰一个集合 -事实上 ```Vector``` 将"可变数组"的集合实现与"同步每一个方法"结合起来的做法是另一个糟糕的设计;
+
+各个装饰方法能够更明确的指示其关注的功能实现。
+
+对于```Stack```这个类-我更乐于使用```Deque/ArrayDeque```来实现
+
+stackoverflow讨论地址: http://stackoverflow.com/questions/1386275/why-is-java-vector-class-considered-obsolete-or-deprecated
From 951f880e4cb83b7b8d32463b79fd338bddfa7f4b Mon Sep 17 00:00:00 2001
From: jam
Date: 2016年4月17日 20:29:03 +0800
Subject: [PATCH 094/195] fanyi
---
...class-considered-obsolete-or-deprecated.md | 21 +++++++++++++++++++
1 file changed, 21 insertions(+)
diff --git a/contents/why-is-java-vector-class-considered-obsolete-or-deprecated.md b/contents/why-is-java-vector-class-considered-obsolete-or-deprecated.md
index e69de29..cfbb754 100644
--- a/contents/why-is-java-vector-class-considered-obsolete-or-deprecated.md
+++ b/contents/why-is-java-vector-class-considered-obsolete-or-deprecated.md
@@ -0,0 +1,21 @@
+## 为什么Java的```Vector```类被认为是过时的或者废弃的
+### 问题
+为什么java ```Vector```类被认为是一个遗留的,过时的或废弃的类?在并发操作时,使用它是无效的吗?
+
+如果我不想手动对对象实现同步,只想用一个线程安全的集合而无需创建底层数组的全新副本(如```CopyOnWriteArrayList```一样)。这种情况下,我使用```Vector```合理吗?
+
+然后就是关于栈的问题,它是Vector的一个子类,我应该用什么代替它?
+### 回答
+Vector中对每一个独立操作都实现了同步,这通常不是我们想要的做法。对单一操作实现同步通常不是线程安全的(举个例子,比如你想遍历一个Vector实例。你仍然需要申明一个锁来防止其他线程在同一时刻修改这个Vector实例。如果不添加锁的话
+
+通常会在遍历实例的这个线程中导致一个``` ConcurrentModificationException ```)同时这个操作也是十分慢的(在创建了一个锁就已经足够的前提下,为什么还需要重复的创建锁)
+
+当然,即使你不需要同步,Vector也是有锁的资源开销的。
+
+总的来说,在大多数情况下,这种同步方法是存在很大缺陷的。正如Mr Brain Henk指出,你可以通过调用```Collections.synchronizedList```来装饰一个集合 -事实上 ```Vector``` 将"可变数组"的集合实现与"同步每一个方法"结合起来的做法是另一个糟糕的设计;
+
+各个装饰方法能够更明确的指示其关注的功能实现。
+
+对于```Stack```这个类-我更乐于使用```Deque/ArrayDeque```来实现
+
+stackoverflow讨论地址: http://stackoverflow.com/questions/1386275/why-is-java-vector-class-considered-obsolete-or-deprecated
From c2237304d294fd434a3b94e6a2de280212190a9f Mon Sep 17 00:00:00 2001
From: tangculijier <327493498@qq.com>
Date: Tue, 3 May 2016 23:36:30 +0800
Subject: [PATCH 095/195] Create onvert-a-string-to-an-enum-in-java.md
---
.../onvert-a-string-to-an-enum-in-java.md | 30 +++++++++++++++++++
1 file changed, 30 insertions(+)
create mode 100644 contents/onvert-a-string-to-an-enum-in-java.md
diff --git a/contents/onvert-a-string-to-an-enum-in-java.md b/contents/onvert-a-string-to-an-enum-in-java.md
new file mode 100644
index 0000000..0e7fd5b
--- /dev/null
+++ b/contents/onvert-a-string-to-an-enum-in-java.md
@@ -0,0 +1,30 @@
+##在java中把String转换给enum类
+-------------------------------------
+
+###问题
+假设有有个枚举类:
+```java
+public enum Blah
+{
+ A, B, C, D
+}
+```
+现在我想把这个String转成枚举类,比如说"A"应该等于Blash.A.该怎么做?
+
+------
+###回答1
+```java
+Blah A = Blah.valueOf("A");
+```
+这样传入"A"会返回Balsh枚举类.
+###回答2
+```java
+Blah A =Enum.valueOf(Blah.class, "A");
+```
+同样可以得到该枚举类
+
+**这两个方法都会传入的参数大小写敏感,这个例子如果传入"a",则会报错No enum const class Blah.a.**
+
+stackoverflow原址: http://stackoverflow.com/questions/604424/convert-a-string-to-an-enum-in-java
+
+
From 5d872de85b4ec8e05019d34cbe1d6b6a274ca4e5 Mon Sep 17 00:00:00 2001
From: ArronDon
Date: Mon, 9 May 2016 22:28:44 +0800
Subject: [PATCH 096/195] Create how-does-the-java-for-each-loop-work.md
---
.../how-does-the-java-for-each-loop-work.md | 23 +++++++++++++++++++
1 file changed, 23 insertions(+)
create mode 100644 contents/how-does-the-java-for-each-loop-work.md
diff --git a/contents/how-does-the-java-for-each-loop-work.md b/contents/how-does-the-java-for-each-loop-work.md
new file mode 100644
index 0000000..8d58d0f
--- /dev/null
+++ b/contents/how-does-the-java-for-each-loop-work.md
@@ -0,0 +1,23 @@
+## Java的foreach循环是如何工作的?
+### 问题
+````java
+List someList = new ArrayList();
+// add "monkey", "donkey", "skeleton key" to someList
+for (String item : someList) {
+ System.out.println(item);
+}
+````
+如果不用for each语法,等价的循环语句是什么样的?
+### 回答
+````java
+for(Iterator i = someList.iterator(); i.hasNext(); ) {
+ String item = i.next();
+ System.out.println(item);
+}
+````
+记住,如果需要在循环中使用i.remove;或者以某种方式获取实际的iterator,你不能使用for(:)语法,因为实际的Iterator很难被推断出来。
+正如Denis Bueno写的那样,这种代码对任何实现了Iterable接口的对象都奏效。
+此外,如果for(:)句法中右侧是一个数组而不是一个可迭代对象,那么内部代码用一个int型的计数器来防止数组越界。详见Java Language Specification:
+http://docs.oracle.com/javase/specs/jls/se8/html/jls-14.html#jls-14.14.2
+
+stackoverflow链接:http://stackoverflow.com/questions/85190/how-does-the-java-for-each-loop-work
From eae052735ee4dd49f0de606e27957bcb6cdfd034 Mon Sep 17 00:00:00 2001
From: Android-kk
Date: 2016年5月18日 10:01:15 +0800
Subject: [PATCH 097/195] Update
what-is-a-serialversionuid-and-why-should-i-use-it.md
---
contents/what-is-a-serialversionuid-and-why-should-i-use-it.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/contents/what-is-a-serialversionuid-and-why-should-i-use-it.md b/contents/what-is-a-serialversionuid-and-why-should-i-use-it.md
index 645bd54..321c58a 100644
--- a/contents/what-is-a-serialversionuid-and-why-should-i-use-it.md
+++ b/contents/what-is-a-serialversionuid-and-why-should-i-use-it.md
@@ -127,7 +127,7 @@ public void testversion1LWithExtraEmail() throws Exception {
private static final long serialVersionUID = 2L;
```
-再次进行序列化,则会报错,如下:
+再次进行反序列化,则会报错,如下:
```
java.io.InvalidClassException:Person local class incompatible: stream classdesc serialVersionUID = 1, local class serialVersionUID = 2
```
From 413b445728e3bb24abcdc16ca28aed8085e4c8dd Mon Sep 17 00:00:00 2001
From: unknown <陈恭帅>
Date: 2016年5月19日 15:04:14 +0800
Subject: [PATCH 098/195] tianjiafanyi
---
...lets-work-instantiation-shared-variables-and-multithreading.md | 0
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 contents/how-do-servlets-work-instantiation-shared-variables-and-multithreading.md
diff --git a/contents/how-do-servlets-work-instantiation-shared-variables-and-multithreading.md b/contents/how-do-servlets-work-instantiation-shared-variables-and-multithreading.md
new file mode 100644
index 0000000..e69de29
From f82979f88a8ca1c4f4fd7d4bedf77d650806de2f Mon Sep 17 00:00:00 2001
From: NothingOne
Date: 2016年5月19日 16:43:01 +0800
Subject: [PATCH 099/195] Update
what-issues-should-be-considered-when-overriding-equals-and-hashcode-in-java.md
---
...be-considered-when-overriding-equals-and-hashcode-in-java.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/contents/what-issues-should-be-considered-when-overriding-equals-and-hashcode-in-java.md b/contents/what-issues-should-be-considered-when-overriding-equals-and-hashcode-in-java.md
index 1ed4300..f09120b 100644
--- a/contents/what-issues-should-be-considered-when-overriding-equals-and-hashcode-in-java.md
+++ b/contents/what-issues-should-be-considered-when-overriding-equals-and-hashcode-in-java.md
@@ -1,4 +1,4 @@
-##重写(Override)equlas和hashCode方法时应考虑的问题
+##重写(Override)equals和hashCode方法时应考虑的问题
###理论上讲(编程语言、数学层面)
equals() 定义了对象的相等关系(自反性、对称性、传递性)(有点抽象,更详细说明,请参考[javadoc](http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#equals(java.lang.Object))) 。
From e4c99d754599845ec2d66f7b32684fce1fac02f8 Mon Sep 17 00:00:00 2001
From: lizeyang
Date: 2016年5月23日 23:35:57 +0800
Subject: [PATCH 100/195] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=B7=B2=E7=BF=BB?=
=?UTF-8?q?=E8=AF=91=E9=97=AE=E9=A2=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/README.md b/README.md
index 053f033..b8622db 100644
--- a/README.md
+++ b/README.md
@@ -1,10 +1,11 @@
+
stackoverflow-Java-top-qa
=======================
对stackoverflow上Java相关、投票数TOP100的问答进行翻译,欢迎点star,我们会持续更新!!!
为了让"翻译"更有意义,给阅读者带来更多、更有效的收获,我们会有一些加工:
例如,对问题进行分类,整合多个答案、删除冗余内容、加上自己的验证结果、心得等等
-
+
对于参与翻译的人,这也是很好的一个学习、理解过程,欢迎大家一起来翻译
-------------
@@ -72,6 +73,7 @@ stackoverflow-Java-top-qa
* [我应该用哪一个@NotNull注解](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/which-notnull-java-annotation-should-i-use.md)
* [怎样将堆栈追踪信息转换为字符串](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-can-i-convert-a-stack-trace-to-a-string.md)
* [如何处理 java.lang.outOfMemoryError PermGen space error](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/dealing-with-java-lang-outofmemoryerror-permgen-space-error.md)
+* [如何用0向左补齐一个整数](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-can-i-pad-an-integers-with-zeros-on-the-left.md)
> 网络
@@ -83,6 +85,8 @@ stackoverflow-Java-top-qa
* [LinkedList、ArrayList各自的使用场景,如何确认应该用哪一个呢?](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/when-to-use-linkedlist-over-arraylist.md)
* [StringBuilder和StringBuffer有哪些区别呢](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/stringbuilder-and-stringbuffer.md)
* [为什么处理排序的数组要比非排序的快](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/why-is-processing-a-sorted-array-faster-than-an-unsorted-array.md)
+* [如何使用Java创建一个内存泄漏的程序](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/creating-a-memory-leak-with-java.md)
+* [为什么打印"B"会明显的比打印"#"慢](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/why-is-printing-b-dramatically-slower-than-printing.md)
> 测试
@@ -96,8 +100,6 @@ stackoverflow-Java-top-qa
### 待翻译问题链接(还剩x问题)
- [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)
- [Proper use cases for Android UserManager.isUserAGoat()?](http://stackoverflow.com/questions/13375357/proper-use-cases-for-android-usermanager-isuseragoat)
-- [Creating a memory leak with Java [closed]](http://stackoverflow.com/questions/6470651/creating-a-memory-leak-with-java) 用户AutumnLight正在翻译该问题
-- [Why is printing "B" dramatically slower than printing "#"?](http://stackoverflow.com/questions/21947452/why-is-printing-b-dramatically-slower-than-printing)
- [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)
- [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)
- [Convert a String to an enum in Java](http://stackoverflow.com/questions/604424/convert-a-string-to-an-enum-in-java)
From 6173090eefcb15f0306e3fbd50c22ad5067845e3 Mon Sep 17 00:00:00 2001
From: giantray <51961070@qq.com>
Date: 2016年5月24日 00:00:10 +0800
Subject: [PATCH 101/195] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=9C=AA=E7=BF=BB?=
=?UTF-8?q?=E8=AF=91=E9=97=AE=E9=A2=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/README.md b/README.md
index b8622db..dd5f68f 100644
--- a/README.md
+++ b/README.md
@@ -54,6 +54,7 @@ stackoverflow-Java-top-qa
* [比较java枚举成员使用equal还是==](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/comparing-java-enum-members-or-equals.md)
* [用java怎么创建一个文件并向该文件写文本内容](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-to-create-a-file-and-write-to-a-file-in-java.md)
* [serialVersionUID 有什么作用?该如何使用?](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/what-is-a-serialversionuid-and-why-should-i-use-it.md)
+* [为什么Java的```Vector```类被认为是过时的或者废弃的](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/why-is-java-vector-class-considered-obsolete-or-deprecated.md)
> 编程技巧
@@ -132,7 +133,6 @@ stackoverflow-Java-top-qa
- [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)
- [Sort ArrayList of custom Objects by property](http://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property)
- [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)
-- [Why is Java Vector class considered obsolete or deprecated?](http://stackoverflow.com/questions/1386275/why-is-java-vector-class-considered-obsolete-or-deprecated)
- [Efficiency of Java "Double Brace Initialization"?](http://stackoverflow.com/questions/924285/efficiency-of-java-double-brace-initialization)
- [Why does this go into an infinite loop?](http://stackoverflow.com/questions/3831341/why-does-this-go-into-an-infinite-loop)
- [When and how should I use a ThreadLocal variable?](http://stackoverflow.com/questions/817856/when-and-how-should-i-use-a-threadlocal-variable)
From ee5bfae8ff741d99a39bfd01e99a5edd4355858d Mon Sep 17 00:00:00 2001
From: giantray <51961070@qq.com>
Date: 2016年5月24日 00:15:18 +0800
Subject: [PATCH 102/195] no message
---
contents/avoiding-null-statements-in-java.md | 11 ++++++-----
1 file changed, 6 insertions(+), 5 deletions(-)
diff --git a/contents/avoiding-null-statements-in-java.md b/contents/avoiding-null-statements-in-java.md
index baf0f60..bff9a52 100644
--- a/contents/avoiding-null-statements-in-java.md
+++ b/contents/avoiding-null-statements-in-java.md
@@ -50,7 +50,8 @@ public interface Parser {
我们来改造一下
-类定义如下,这样定义findAction方法后,确保无论用户输入什么,都不会返回null对象
+类定义如下,这样定义findAction方法后,确保无论用户输入什么,都不会返回null对象:
+```java
public class MyParser implements Parser {
private static Action DO_NOTHING = new Action() {
public void doSomething() { /* do nothing */ }
@@ -62,7 +63,7 @@ public class MyParser implements Parser {
return DO_NOTHING;
}
}}
-
+```
对比下面两份调用实例
1. 冗余: 每获取一个对象,就判一次空
@@ -90,11 +91,11 @@ ParserFactory.getParser().findAction(someInput).doSomething();
- 如果要用equal方法,请用object<不可能为空>.equal(object<可能为空>))
例如:
使用
-"bar".equals(foo)
+`"bar".equals(foo) `
而不是
-foo.equals("bar")
+`foo.equals("bar") `
- Java8或者guava lib中,提供了Optional类,这是一个元素容器,通过它来封装对象,可以减少判空。不过代码量还是不少。不爽。
- 如果你想返回null,请挺下来想一想,这个地方是否更应该抛出一个异常
stackoverflow链接:
-http://stackoverflow.com/questions/271526/avoiding-null-statements-in-java?page=2&tab=votes#tab-top
\ No newline at end of file
+http://stackoverflow.com/questions/271526/avoiding-null-statements-in-java?page=2&tab=votes#tab-top
From 7ab16f31a2d31aa9f14f7276a0caf7720cf1e917 Mon Sep 17 00:00:00 2001
From: giantray <51961070@qq.com>
Date: 2016年5月25日 08:51:14 +0800
Subject: [PATCH 103/195] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=B7=B2=E7=BF=BB?=
=?UTF-8?q?=E8=AF=91=E9=97=AE=E9=A2=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 2 +-
.../onvert-a-string-to-an-enum-in-java.md | 30 -------------------
2 files changed, 1 insertion(+), 31 deletions(-)
delete mode 100644 contents/onvert-a-string-to-an-enum-in-java.md
diff --git a/README.md b/README.md
index dd5f68f..9c8c7a9 100644
--- a/README.md
+++ b/README.md
@@ -55,6 +55,7 @@ stackoverflow-Java-top-qa
* [用java怎么创建一个文件并向该文件写文本内容](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-to-create-a-file-and-write-to-a-file-in-java.md)
* [serialVersionUID 有什么作用?该如何使用?](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/what-is-a-serialversionuid-and-why-should-i-use-it.md)
* [为什么Java的```Vector```类被认为是过时的或者废弃的](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/why-is-java-vector-class-considered-obsolete-or-deprecated.md)
+* [Java的foreach循环是如何工作的](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-does-the-java-for-each-loop-work.md)
> 编程技巧
@@ -126,7 +127,6 @@ stackoverflow-Java-top-qa
- [Setting multiple jars in java classpath](http://stackoverflow.com/questions/219585/setting-multiple-jars-in-java-classpath)
- [Iterating through a list, avoiding ConcurrentModificationException when removing in loop](http://stackoverflow.com/questions/223918/iterating-through-a-list-avoiding-concurrentmodificationexception-when-removing)
- [Is null check needed before calling instanceof](http://stackoverflow.com/questions/2950319/is-null-check-needed-before-calling-instanceof)
-- [How does the Java for each loop work?](http://stackoverflow.com/questions/85190/how-does-the-java-for-each-loop-work)
- [What is the Java equivalent for LINQ?](http://stackoverflow.com/questions/1217228/what-is-the-java-equivalent-for-linq)
- [Hibernate hbm2ddl.auto possible values and what they do?](http://stackoverflow.com/questions/438146/hibernate-hbm2ddl-auto-possible-values-and-what-they-do)
- [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)
diff --git a/contents/onvert-a-string-to-an-enum-in-java.md b/contents/onvert-a-string-to-an-enum-in-java.md
deleted file mode 100644
index 0e7fd5b..0000000
--- a/contents/onvert-a-string-to-an-enum-in-java.md
+++ /dev/null
@@ -1,30 +0,0 @@
-##在java中把String转换给enum类
--------------------------------------
-
-###问题
-假设有有个枚举类:
-```java
-public enum Blah
-{
- A, B, C, D
-}
-```
-现在我想把这个String转成枚举类,比如说"A"应该等于Blash.A.该怎么做?
-
-------
-###回答1
-```java
-Blah A = Blah.valueOf("A");
-```
-这样传入"A"会返回Balsh枚举类.
-###回答2
-```java
-Blah A =Enum.valueOf(Blah.class, "A");
-```
-同样可以得到该枚举类
-
-**这两个方法都会传入的参数大小写敏感,这个例子如果传入"a",则会报错No enum const class Blah.a.**
-
-stackoverflow原址: http://stackoverflow.com/questions/604424/convert-a-string-to-an-enum-in-java
-
-
From f0bc01868fc38caeba665b94691c69209ca8b782 Mon Sep 17 00:00:00 2001
From: giantray <51961070@qq.com>
Date: 2016年5月25日 09:00:48 +0800
Subject: [PATCH 104/195] Merge branch 'master' of
https://github.com/YuxiangQue/stackoverflow-java-top-qa into
YuxiangQue-master
# Conflicts:
# contents/how-can-i-pad-an-integers-with-zeros-on-the-left.md
---
...-pad-an-integers-with-zeros-on-the-left.md | 123 ++++++++++++++++--
...-check-needed-before-calling-instanceof.md | 47 +++++++
2 files changed, 161 insertions(+), 9 deletions(-)
create mode 100644 contents/is-null-check-needed-before-calling-instanceof.md
diff --git a/contents/how-can-i-pad-an-integers-with-zeros-on-the-left.md b/contents/how-can-i-pad-an-integers-with-zeros-on-the-left.md
index 27809e0..4c632df 100644
--- a/contents/how-can-i-pad-an-integers-with-zeros-on-the-left.md
+++ b/contents/how-can-i-pad-an-integers-with-zeros-on-the-left.md
@@ -1,18 +1,123 @@
-# 如何用0向左补齐一个整数?
+## 如何在整数左填充0
-## 问题
+### 问题
+如何在整数左填充0
+举例 1 = "0001"
-在Java中如何把一个整数转化为字符串并且用0向左补齐呢?
-我基本上是希望把一个不大于9999的整数用0补齐 (比如说1="0001")。
-
-## 解答
+### 答案一,String.format
String.format("%05d", yournumber);
-可以将一个五位数用0补齐。
+用0填充,总长度为5
+https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html
+
+### 答案二,ApacheCommonsLanguage
+如果需要在Java 1.5前使用,可以利用 Apache Commons Language 方法
+ org.apache.commons.lang.StringUtils.leftPad(String str, int size, '0')
+
+### 答案三,DecimalFormat
+ import java.text.DecimalFormat;
+ class TestingAndQualityAssuranceDepartment
+ {
+ public static void main(String [] args)
+ {
+ int x=1;
+ DecimalFormat df = new DecimalFormat("00");
+ System.out.println(df.format(x));
+ }
+ }
+
+### 答案四,自己实现
+如果效率很重要的话,相比于 String.format 函数的可以自己实现
+
+ /**
+ * @param in The integer value
+ * @param fill The number of digits to fill
+ * @return The given value left padded with the given number of digits
+ */
+ public static String lPadZero(int in, int fill){
+
+ boolean negative = false;
+ int value, len = 0;
+
+ if(in>= 0){
+ value = in;
+ } else {
+ negative = true;
+ value = - in;
+ in = - in;
+ len ++;
+ }
+
+ if(value == 0){
+ len = 1;
+ } else{
+ for(; value != 0; len ++){
+ value /= 10;
+ }
+ }
+
+ StringBuilder sb = new StringBuilder();
+
+ if(negative){
+ sb.append('-');
+ }
+
+ for(int i = fill; i> len; i--){
+ sb.append('0');
+ }
+
+ sb.append(in);
+
+ return sb.toString();
+ }
+
+ 效率对比
+
+ public static void main(String[] args) {
+ Random rdm;
+ long start;
+
+ // Using own function
+ rdm = new Random(0);
+ start = System.nanoTime();
+
+ for(int i = 10000000; i != 0; i--){
+ lPadZero(rdm.nextInt(20000) - 10000, 4);
+ }
+ System.out.println("Own function: " + ((System.nanoTime() - start) / 1000000) + "ms");
+
+ // Using String.format
+ rdm = new Random(0);
+ start = System.nanoTime();
+
+ for(int i = 10000000; i != 0; i--){
+ String.format("%04d", rdm.nextInt(20000) - 10000);
+ }
+ System.out.println("String.format: " + ((System.nanoTime() - start) / 1000000) + "ms");
+ }
+
+ 结果
+ 自己的实现:1697ms
+ String.format:38134ms
+
+### 答案,Google Guava
+Maven:
+
+
+ guava
+ com.google.guava
+ 14.0.1
+
+样例:
+
+ Strings.padStart("7", 3, '0') returns "007"
+ Strings.padStart("2020", 3, '0') returns "2020"
+注意:
+Guava 是非常有用的库,它提供了很多有用的功能,包括了Collections, Caches, Functional idioms, Concurrency, Strings, Primitives, Ranges, IO, Hashing, EventBus等
-https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html
-Stackoverflow链接:http://stackoverflow.com/questions/473282/how-can-i-pad-an-integers-with-zeros-on-the-left
+stackoverflow原址:
+http://stackoverflow.com/questions/473282/how-can-i-pad-an-integers-with-zeros-on-the-left
\ No newline at end of file
diff --git a/contents/is-null-check-needed-before-calling-instanceof.md b/contents/is-null-check-needed-before-calling-instanceof.md
new file mode 100644
index 0000000..d7a8008
--- /dev/null
+++ b/contents/is-null-check-needed-before-calling-instanceof.md
@@ -0,0 +1,47 @@
+## 在调用 instanceof 前需要进行null检查吗
+
+
+### 问题:
+
+null instanceof SomeClass 会返回 null 还是抛出 NullPointerException 异常
+
+### 答案一
+在调用 instanceof 前不要进行null检查
+null instanceof SomeClass 会返回 null
+在 Java Language Specification 中 http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.20.2
+
+```
+在运行时,如果该instanceof运算符的关系表达式(RelationExpression)不为 null,且这个引用可以被成功转型( §15.16),不抛出ClassCastException,则结果为true;
+ 否则结果为false。
+```
+
+### 答案二
+ public class IsInstanceOfTest {
+
+ public static void main(final String[] args) {
+
+ String s;
+
+ s = "";
+
+ System.out.println((s instanceof String));
+ System.out.println(String.class.isInstance(s));
+
+ s = null;
+
+ System.out.println((s instanceof String));
+ System.out.println(String.class.isInstance(s));
+ }
+ }
+打印出
+
+ true
+ true
+ false
+ false
+
+### 原文链接
+http://stackoverflow.com/questions/2950319/is-null-check-needed-before-calling-instanceof
+
+
+
\ No newline at end of file
From a4adac54f2c7dcc18880359ecfebf49d8681dd4d Mon Sep 17 00:00:00 2001
From: giantray <51961070@qq.com>
Date: 2016年5月25日 09:02:53 +0800
Subject: [PATCH 105/195] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=B7=B2=E7=BF=BB?=
=?UTF-8?q?=E8=AF=91=E9=97=AE=E9=A2=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/README.md b/README.md
index 9c8c7a9..09ff925 100644
--- a/README.md
+++ b/README.md
@@ -75,7 +75,8 @@ stackoverflow-Java-top-qa
* [我应该用哪一个@NotNull注解](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/which-notnull-java-annotation-should-i-use.md)
* [怎样将堆栈追踪信息转换为字符串](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-can-i-convert-a-stack-trace-to-a-string.md)
* [如何处理 java.lang.outOfMemoryError PermGen space error](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/dealing-with-java-lang-outofmemoryerror-permgen-space-error.md)
-* [如何用0向左补齐一个整数](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-can-i-pad-an-integers-with-zeros-on-the-left.md)
+* [如何在整数左填充0](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-can-i-pad-an-integers-with-zeros-on-the-left.md)
+* [在调用 instanceof 前需要进行null检查吗](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/is-null-check-needed-before-calling-instanceof.md)
> 网络
@@ -126,7 +127,6 @@ stackoverflow-Java-top-qa
- [Update Eclipse with Android development tools v. 23](http://stackoverflow.com/questions/24437564/update-eclipse-with-android-development-tools-v-23)
- [Setting multiple jars in java classpath](http://stackoverflow.com/questions/219585/setting-multiple-jars-in-java-classpath)
- [Iterating through a list, avoiding ConcurrentModificationException when removing in loop](http://stackoverflow.com/questions/223918/iterating-through-a-list-avoiding-concurrentmodificationexception-when-removing)
-- [Is null check needed before calling instanceof](http://stackoverflow.com/questions/2950319/is-null-check-needed-before-calling-instanceof)
- [What is the Java equivalent for LINQ?](http://stackoverflow.com/questions/1217228/what-is-the-java-equivalent-for-linq)
- [Hibernate hbm2ddl.auto possible values and what they do?](http://stackoverflow.com/questions/438146/hibernate-hbm2ddl-auto-possible-values-and-what-they-do)
- [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)
From c4ef2b4bbbb92c92b4cfbc6220d9466f364df4eb Mon Sep 17 00:00:00 2001
From: severalfly
Date: 2016年5月27日 18:08:02 +0800
Subject: [PATCH 106/195] Why is subtracting these two times (in 1927) giving a
strange result?
---
...times-(in-1927)-giving-a-strange-result.md | 80 +++++++++++++++++++
1 file changed, 80 insertions(+)
create mode 100644 contents/why-is-subtracting-these-two-times-(in-1927)-giving-a-strange-result.md
diff --git a/contents/why-is-subtracting-these-two-times-(in-1927)-giving-a-strange-result.md b/contents/why-is-subtracting-these-two-times-(in-1927)-giving-a-strange-result.md
new file mode 100644
index 0000000..d4f5d19
--- /dev/null
+++ b/contents/why-is-subtracting-these-two-times-(in-1927)-giving-a-strange-result.md
@@ -0,0 +1,80 @@
+## 为什么相减这两个时间(1927年)会得到奇怪的结果?
+
+### 问题
+如果运行下面的代码,我想要做的是解析两个相差1秒的日期字符串:
+```java
+public static void main(String[] args) throws ParseException {
+ SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+ String str3 = "1927-12-31 23:54:07";
+ String str4 = "1927-12-31 23:54:08";
+ Date sDt3 = sf.parse(str3);
+ Date sDt4 = sf.parse(str4);
+ long ld3 = sDt3.getTime() /1000;
+ long ld4 = sDt4.getTime() /1000;
+ System.out.println(ld4-ld3);
+}
+```
+其结果是
+```
+353
+```
+为什么`ld4-ld3` 结果不是`1`(因为我期望得到1秒的时间差)而是`353`?
+
+如果我把两个日期都换成1秒后:
+```java
+String str3 = "1927-12-31 23:54:08";
+String str4 = "1927-12-31 23:54:09";
+```
+这样`ld4-ld3`就为`1`。
+
+---
+java 版本
+```
+java version "1.6.0_22"
+Java(TM) SE Runtime Environment (build 1.6.0_22-b04)
+Dynamic Code Evolution Client VM (build 0.2-b02-internal, 19.0-b04-internal, mixed mode)
+```
+时区(`TimeZone.getDefault()`):
+```
+sun.util.calendar.ZoneInfo[id="Asia/Shanghai",
+offset=28800000,dstSavings=0,
+useDaylight=false,
+transitions=19,
+lastRule=null]
+
+Locale(Locale.getDefault()): zh_CN
+```
+## 回答
+这是12月31号上海时区切换的错误。
+
+[这个页面](http://www.timeanddate.com/time/change/china/shanghai?year=1927)上有关于上海1927的详细说明。因为1927年末尾的午夜,时间回滚了5分52秒,所以"1927-12-31 23:54:08" 实际上发生了两次,并且很明显java解析立即得到了可能的后面一个,而忽略了其差别。
+
+这在经常弄混且丰富的世界时区中还是一个插曲。
+
+如果重新编译[TZDB](https://github.com/nodatime/nodatime/blob/master/src/NodaTime.Demo/StackOverflowExamples.cs#L68)的2013a版本,之前的问题就不再能解释类型行为。在2013a 的版本中,结果将是358秒,切换时间由23:54:03 变为23:54:08。
+
+我能注意到这个都是因为我在[unit tests](https://github.com/nodatime/nodatime/blob/master/src/NodaTime.Demo/StackOverflowExamples.cs#L68)收集像Noda Time 这样的问题
+
+这个测试现在已经被更新,但是也表明没有历史数据是安全的。
+
+在TZDB 2014f中, 这个时间切换移动到了1900年12月31日,并且也被修改成唯一的343秒变化(如果你知道我在说啥的话,`t` 和 `t+1` 之间是343 秒)。
+
+**更新** 为了回答[ Ken Kin's question](http://stackoverflow.com/questions/6841333/why-is-subtracting-these-two-times-in-1927-giving-a-strange-result/6841479#comment22684267_6841479)的问题,看起来像是,对于任何在1900 UTC 之前的时间,java 在他们的标准时间中进行了简单的时区实现。
+```java
+import java.util.TimeZone;
+
+public class Test {
+ public static void main(String[] args) throws Exception {
+ long startOf1900Utc = -2208988800000L;
+ for (String id : TimeZone.getAvailableIDs()) {
+ TimeZone zone = TimeZone.getTimeZone(id);
+ if (zone.getRawOffset() != zone.getOffset(startOf1900Utc - 1)) {
+ System.out.println(id);
+ }
+ }
+ }
+}
+```
+上面的代码在我的windows 机器上没有任何输出,所以对于任何相对标准时间在1900年前且有偏移的时间,都会有时间过渡。TZDB 自己有数据回退到更早之前,并且不依赖任何修正过的标准时间(就是`getRawOffset`假设的有效的概念),所以其他库不再需要介绍这个切换。
+
+stackoverflow[链接](http://stackoverflow.com/questions/6841333/why-is-subtracting-these-two-times-in-1927-giving-a-strange-result)
\ No newline at end of file
From 4164f046402a4d77ceb4fbff6da51ef8965ef00b Mon Sep 17 00:00:00 2001
From: Yixiaohan <499065469@qq.com>
Date: 2016年5月31日 15:09:09 +0800
Subject: [PATCH 107/195] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=8E=92=E7=89=88?=
=?UTF-8?q?=E3=80=81=E4=BF=AE=E6=94=B9=E9=94=99=E5=88=AB=E5=AD=97?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
代码排版、修改错别字
---
contents/avoiding-null-statements-in-java.md | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/contents/avoiding-null-statements-in-java.md b/contents/avoiding-null-statements-in-java.md
index bff9a52..aab0bc7 100644
--- a/contents/avoiding-null-statements-in-java.md
+++ b/contents/avoiding-null-statements-in-java.md
@@ -75,7 +75,8 @@ if (parser == null) {
}
Action action = parser.findAction(someInput);
if (action == null) {
- // do nothing} else {
+ // do nothing}
+else {
action.doSomething();}
```
@@ -95,7 +96,7 @@ ParserFactory.getParser().findAction(someInput).doSomething();
而不是
`foo.equals("bar") `
- Java8或者guava lib中,提供了Optional类,这是一个元素容器,通过它来封装对象,可以减少判空。不过代码量还是不少。不爽。
-- 如果你想返回null,请挺下来想一想,这个地方是否更应该抛出一个异常
+- 如果你想返回null,请停下来想一想,这个地方是否更应该抛出一个异常
stackoverflow链接:
http://stackoverflow.com/questions/271526/avoiding-null-statements-in-java?page=2&tab=votes#tab-top
From 4b61f498ac933cbee79925320cf108ec740f12c0 Mon Sep 17 00:00:00 2001
From: unknown <陈恭帅>
Date: Wed, 1 Jun 2016 15:49:35 +0800
Subject: [PATCH 108/195] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=8F=90=E4=BA=A4?=
=?UTF-8?q?=E7=A9=BA=E6=96=87=E4=BB=B6=E7=9A=84=E9=97=AE=E9=A2=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...ion-shared-variables-and-multithreading.md | 52 +++++++++++++++++++
1 file changed, 52 insertions(+)
diff --git a/contents/how-do-servlets-work-instantiation-shared-variables-and-multithreading.md b/contents/how-do-servlets-work-instantiation-shared-variables-and-multithreading.md
index e69de29..148ab53 100644
--- a/contents/how-do-servlets-work-instantiation-shared-variables-and-multithreading.md
+++ b/contents/how-do-servlets-work-instantiation-shared-variables-and-multithreading.md
@@ -0,0 +1,52 @@
+##How do servlets work? Instantiation, shared variables and multithreading
+###问题:
+假设,我有一个web服务器可以支持无数的servlets,对于通过这些servlets的信息,我正在获取这些servlets的上下文环境,并设置session变量。
+现在,如果有两个或者更多的user用户发送请求到这个服务器,session变量会发生什么变化?session对于所有的user是公共的还是不同的user拥有不同的session。如果用户彼此之间的session是不同的,那么服务器怎么区分辨别不同的用户呢?
+另外一些相似的问题,如果有N个用户访问一个具体的servlets,那么这个servlets是只在第一个用户第一次访问的时候实例化,还是为每一个用户各自实例化呢?
+###答案:
+####ServletContext
+当servletcontainer(像tomcat)启动的时候,它会部署和加载所有的webapplications,当一个webapplication加载完成后,servletcontainer就会创建一个ServletContext,并且保存在服务器的内存中。这个webapp的web.xml会被解析,web.xml中的每个```, and ```或者通过注解```@WebServlet, @WebFilter and @WebListener```,都会被创建一次并且也保存在服务器的内存中。对于所有filter,```init()```方法会被直接触发,当servletcontainer关闭的时候,它会unload所有的webapplications,触发所有实例化的servlets和filters的```destroy()```方法,最后,servletcontext和所有的servlets,filter和listener实例都会被销毁。
+
+####HttpServletRequest and HttpServletResponse
+servletcontainer 是附属于webserver的,而这个webserver会持续监听一个目标端口的```HTTP request```请求,这个端口在开发中经常会被设置成8080,而在生产环境会被设置成80。当一个客户端(比如用户的浏览器)发送一个HTTP request,servletcontainer就会创建新的HttpServletRequest对象和HttpServletResponse对象。。。。
+
+在有filter的情况下,```doFilter()```方法会被触发。当代码调用```chain.doFilter(request, response)```时候,请求会经过下一个过滤器filter,如果没有了过滤器,会到达servlet。在servlets的情况下,```service()```触发,然后根据```request.getMethod()```确定执行doGet()还是```doPost()```,如果当前servlet找不到请求的方法,返回405error。
+
+request对象提供了HTTP请求所有的信息,比如request headers和request body,response对象提供了控制和发送HTTP响应的的能力,并且以你想要的方式,比如设置headers和body。当HTTP响应结束,请求和响应对象会被销毁(实际上,大多数container将会清洗到这些对象的状态然后回收这些事例以重新利用)
+####httpSession
+当客户端第一次访问webapp或者通过```request.getSession()```方法第一次获取httpSession
+,servletcontainer 将会创建一个新的HttpSession 对象,产生一个长的唯一的ID标记session(可以通过session.getId()),并且将这个session存储在server内存中。servletcontainer 同时会在HTTP response的Header中设置```Set-Cookie```cookie值,其中cookie name为JSESSIONID,cookie value为唯一的长ID值。
+
+在接下来的连续请求中,客户端浏览器都要cookie通过header带回,然后servletcontainer 会根据cookie中的JSESSIONID 值,获得server内存中的对应的httpSession。
+
+只要没超过``````设定的值,httpSession对象会一直存在,``````大小可以在web.xml中设定,默认是30分钟。所以如果连续30分钟之内客户端不再访问webapp,servletcontainer就会销毁对应的session。接下来的request请求即使cookies依旧存在,但是却不再有对应的session了。servletcontainer 会创建新的session。
+
+另外一方面,session cookie在浏览器端有默认的生命时长,就是只要浏览器一直在运行,所以当浏览器关闭,浏览器端的cookie会被销毁。
+####最后
+- 只要webapp存在,ServletContext 一定会存在。并且ServletContext 是被所有session和request共享的。
+- 只要客户端用同一个浏览器和webapp交互并且该session没有在服务端超时,HttpSession 就会一直存在。并且在同一个会话中所有请求都是共享的。
+- 只有当完整的response响应到达,HttpServletRequest 和 HttpServletResponse才不再存活,并且不被共享。
+- 只要webapp存在,servlet、filter和listener就会存在。他们被所有请求和会话共享。
+- 只要问题中的对象存在,任何设置在ServletContext, HttpServletRequest 和 HttpSession中的属性就会存在。
+
+####线程安全
+就是说,你主要关注的是线程安全性。你应该了解到,servlets和filter是被所有请求共享的。这正是Java的美妙之处,它的多线程和不同的线程可以充分利用同样的实例instance,否则对于每一个request请求都要重复创建和调用init()和destroy()开销太大。
+
+但是你也应该注意到,你不应该把任何请求或会话作用域的数据作为一个servlet或过滤器的实例变量。这样会被其他会话的请求共享,并且那是线程不安全的!下面的例子阐明的这点:
+```
+public class ExampleServlet extends HttpServlet {
+
+ private Object thisIsNOTThreadSafe;
+
+ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+ Object thisIsThreadSafe;
+
+ thisIsNOTThreadSafe = request.getParameter("foo"); // BAD!! Shared among all requests!
+ thisIsThreadSafe = request.getParameter("foo"); // OK, this is thread safe.
+ }
+}
+```
+
+stackoverflow链接:
+
+http://stackoverflow.com/questions/3106452/how-do-servlets-work-instantiation-shared-variables-and-multithreading
\ No newline at end of file
From 94d69915d0b30d39908628b110e033a011941784 Mon Sep 17 00:00:00 2001
From: ArronDon
Date: Wed, 1 Jun 2016 22:19:44 +0800
Subject: [PATCH 109/195] when-and-how-should-i-use-a-threadlocal-variable
---
...how-should-i-use-a-threadlocal-variable.md | 33 +++++++++++++++++++
1 file changed, 33 insertions(+)
create mode 100644 contents/when-and-how-should-i-use-a-threadlocal-variable.md
diff --git a/contents/when-and-how-should-i-use-a-threadlocal-variable.md b/contents/when-and-how-should-i-use-a-threadlocal-variable.md
new file mode 100644
index 0000000..17cb54c
--- /dev/null
+++ b/contents/when-and-how-should-i-use-a-threadlocal-variable.md
@@ -0,0 +1,33 @@
+## 该什么使用 ThreadLocal变量,它是如何工作的?
+### 回答1
+一种可能的(也是常见的)使用情形是你不想通过同步方式(synchronized)访问非线程安全的对象(说的就是SimpleDateFormat),而是想给每个线程一个对象实例的时候。
+例如
+````java
+public class Foo
+{
+ // SimpleDateFormat is not thread-safe, so give one to each thread
+ private static final ThreadLocal formatter = new ThreadLocal(){
+ @Override
+ protected SimpleDateFormat initialValue()
+ {
+ return new SimpleDateFormat("yyyyMMdd HHmm");
+ }
+ };
+
+ public String formatIt(Date date)
+ {
+ return formatter.get().format(date);
+ }
+}
+````
+### 回答2
+因为ThreadLocal是一个既定线程内部的数据引用,你可能在使用线程池的应用服务器上因此引起类加载时候的内存泄漏。你需要使用remove()方法很小心地清理TheadLocal中get()或者set()的变量。
+如果程序执行完毕没有清理的话,它持有的任何对类的引用将作为部署的Web应用程序的一部分仍保持在永久堆,永远无法得到回收。重新部署/取消部署也无法清理对应用程序类的引用,因为线程不是被你的应用程序所拥有的。
+每次成功部署都会创建一个永远不会被垃圾回收类的实例。
+
+最后将会遇到内存不足的异常-java.lang.java.lang.OutOfMemoryError: PermGen space -XX:MaxPermSize,在google了很多答案之后你可能只是增加了-XX:MaxPermSize,而不是修复这个bug。
+倘若你的确遇到这种问题,可以通过[Eclipse's Memory Analyzer](http://www.eclipse.org/mat/)或根据[Frank Kieviet's guide](https://blogs.oracle.com/fkieviet/entry/classloader_leaks_the_dreaded_java) 和 [followup](https://blogs.oracle.com/fkieviet/entry/how_to_fix_the_dreaded)来判断哪些线程和类保留了那些引用。
+
+更新:又发现了[Alex Vasseur's blog entry](http://avasseur.blogspot.jp/2003/11/threadlocal-and-memory-leaks.html),它帮助我查清楚了一些ThreadLocal的问题。
+
+stackoverflow链接:http://stackoverflow.com/questions/817856/when-and-how-should-i-use-a-threadlocal-variable
From 36c07fc5a2d329c267bdfaec28ec091a331ffc02 Mon Sep 17 00:00:00 2001
From: ArronDon
Date: Wed, 1 Jun 2016 22:22:24 +0800
Subject: [PATCH 110/195] Update
when-and-how-should-i-use-a-threadlocal-variable.md
---
contents/when-and-how-should-i-use-a-threadlocal-variable.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/contents/when-and-how-should-i-use-a-threadlocal-variable.md b/contents/when-and-how-should-i-use-a-threadlocal-variable.md
index 17cb54c..50d97ef 100644
--- a/contents/when-and-how-should-i-use-a-threadlocal-variable.md
+++ b/contents/when-and-how-should-i-use-a-threadlocal-variable.md
@@ -1,4 +1,4 @@
-## 该什么使用 ThreadLocal变量,它是如何工作的?
+## 该什么时候使用 ThreadLocal变量,它是如何工作的?
### 回答1
一种可能的(也是常见的)使用情形是你不想通过同步方式(synchronized)访问非线程安全的对象(说的就是SimpleDateFormat),而是想给每个线程一个对象实例的时候。
例如
From 500ee1d61a5ed2038f4c8a913e9328a0266fe75d Mon Sep 17 00:00:00 2001
From: MagicWolf
Date: 2016年6月30日 14:27:53 +0800
Subject: [PATCH 111/195] =?UTF-8?q?=E7=BF=BB=E8=AF=91Convert=20a=20String?=
=?UTF-8?q?=20to=20an=20enum=20in=20Java?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
contents/lookup-enum-by-string-value.md | 106 ++++++++++++++++++++++++
1 file changed, 106 insertions(+)
create mode 100644 contents/lookup-enum-by-string-value.md
diff --git a/contents/lookup-enum-by-string-value.md b/contents/lookup-enum-by-string-value.md
new file mode 100644
index 0000000..2bb583c
--- /dev/null
+++ b/contents/lookup-enum-by-string-value.md
@@ -0,0 +1,106 @@
+## Java 中如何将 String 转换为 enum
+
+###问题
+我有一个 enum 类
+
+``` java
+public enum Blah {
+ A, B, C, D
+}
+```
+我想要找到一个 `String` 对应的 enum 值。例如, `"A"` 将是 `Blah.A`.如何做到?
+
+我需要使用 `Enum.valueOf()` 方法吗? 如果是该如何使用?
+
+---
+
+### A1
+
+是的, `Blah.valueOf("A")` 将会给你 `Blah.A`.
+
+静态方法 `valueof()` 和 `values()` 在编译时期被插入,并不存在于源码中。但是在Javadoc中;例如,[`Dialog.ModalityType`](http://docs.oracle.com/javase/7/docs/api/java/awt/Dialog.ModalityType.html "Dialog.ModalityType")中显示了这两个方法。
+
+
+### A2
+
+另一个解答,如果文本和 `enumeration` 值不一致
+
+``` java
+public enum Blah {
+ A("text1"),
+ B("text2"),
+ C("text3"),
+ D("text4");
+
+ private String text;
+
+ Blah(String text) {
+ this.text = text;
+ }
+
+ public String getText() {
+ return this.text;
+ }
+
+ public static Blah fromString(String text) {
+ if (text != null) {
+ for (Blah b : Blah.values()) {
+ if (text.equalsIgnoreCase(b.text)) {
+ return b;
+ }
+ }
+ }
+ return null;
+ }
+}
+```
+_评论区在讨论是应该返回null还是抛出异常,个人认为视具体情况,允许转换失败就返回null,不允许就抛出异常,或许创建一个特殊的空对象是个好的选择 -译者注_
+
+### A3
+
+这是我使用的一个工具类:
+
+``` java
+/**
+ * 一个对于所有Enum类通用的方法,因为他们不能有另一个基类
+ * @param Enum type
+ * @param c enum type. All enums must be all caps.
+ * @param string case insensitive
+ * @return corresponding enum, or null
+ */
+public static > T getEnumFromString(Class c, String string) {
+ if( c != null && string != null ) {
+ try {
+ return Enum.valueOf(c, string.trim().toUpperCase());
+ } catch(IllegalArgumentException ex) {
+ }
+ }
+ return null;
+}
+```
+之后,在我的enum类中通常如此使用来减少打字:
+``` java
+public static MyEnum fromString(String name) {
+ return getEnumFromString(MyEnum.class, name);
+}
+```
+如果的enums不是全部大写,只需要修改 `Enum.valueOf` 这一行。
+很遗憾,我不能使用 `T.class` 传给 `Enum.valueOf`,因为 `T`会被擦出。
+
+_评论区对于答主的异常处理一片指责 -译者注_
+
+###A4
+如果你不想编写自己的工具类,可以使用 Google的 `guava` 库:
+``` java
+Enums.getIfPresent(Blah.class, "A")
+```
+它让你检查是否 `Blan`中存在 `A`并且不抛出异常
+
+_完整方法签名 `Optional getIfPresent(Class enumClass, String value)` , `Optional` 对象可以优雅的解决null值问题 -译者注_
+
+---
+_其他的答案都大同小异,感兴趣的可以看原帖_
+_原帖:[Convert a String to an enum in Java](http://stackoverflow.com/questions/604424/lookup-enum-by-string-value)_
+_译者:[MagicWolf](https://github.com/DaiDongLiang)_
+
+
From cc2ca4ac2927a762b8b08c0015c9c5cd1c3c21d3 Mon Sep 17 00:00:00 2001
From: gaoshihang <472024406@qq.com>
Date: 2016年7月25日 15:05:02 +0800
Subject: [PATCH 112/195] Create
why-is-subtracting-these-two-times-in-1927-giving-a-strange-result.md
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
这是我翻译的why-is-subtracting-these-two-times-in-1927-giving-a-strange-result这篇问答的中文版。
---
...o-times-in-1927-giving-a-strange-result.md | 59 +++++++++++++++++++
1 file changed, 59 insertions(+)
create mode 100644 contents/why-is-subtracting-these-two-times-in-1927-giving-a-strange-result.md
diff --git a/contents/why-is-subtracting-these-two-times-in-1927-giving-a-strange-result.md b/contents/why-is-subtracting-these-two-times-in-1927-giving-a-strange-result.md
new file mode 100644
index 0000000..d2f4449
--- /dev/null
+++ b/contents/why-is-subtracting-these-two-times-in-1927-giving-a-strange-result.md
@@ -0,0 +1,59 @@
+##为什么这两个时间(1927年)相减会得到一个奇怪的结果?
+
+###问题描述
+如果我运行如下的程序,将两个相距一秒的日期解析成字符串并比较他们。
+```
+public static void main(String[] args) throws ParseException {
+ SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+ String str3 = "1927-12-31 23:54:07";
+ String str4 = "1927-12-31 23:54:08";
+ Date sDt3 = sf.parse(str3);
+ Date sDt4 = sf.parse(str4);
+ long ld3 = sDt3.getTime() /1000;
+ long ld4 = sDt4.getTime() /1000;
+ System.out.println(ld4-ld3);
+}
+```
+
+输出结果为:
+```
+353
+```
+
+为什么`ld4-ld3`不是`1`(正如我所期望的那样),而是`353`?
+
+如果我把时间改变为之后的一秒:
+```
+String str3 = "1927-12-31 23:54:08";
+String str4 = "1927-12-31 23:54:09";
+```
+
+这时,`ld4-ld3`的结果为`1`.
+
+java版本:
+```
+java version "1.6.0_22"
+Java(TM) SE Runtime Environment (build 1.6.0_22-b04)
+Dynamic Code Evolution Client VM (build 0.2-b02-internal, 19.0-b04-internal, mixed mode)
+```
+
+时区:
+```
+sun.util.calendar.ZoneInfo[id="Asia/Shanghai",
+offset=28800000,dstSavings=0,
+useDaylight=false,
+transitions=19,
+lastRule=null]
+
+Locale(Locale.getDefault()): zh_CN
+```
+
+###问题回答
+这是因为1927年11月31日上海的时区改变了。
+观看[此页](http://www.timeanddate.com/time/change/china/shanghai?year=1927)获得更多关于上海1927年的细节。
+这个问题主要是由于在1927年12月31日的午夜,时钟回调了5分钟零52秒。
+所以"1927-12-31 23:54:08"这个时间实际上发生了两次,看上去java将这个时间解析为之后的那个瞬间。
+因此出现了这种差别。
+
+这只是美好但奇怪的世界时区中的一个插曲。
+
From bbe5225c9b0cb85f2705bd560e48587e54f312ad Mon Sep 17 00:00:00 2001
From: gaoshihang <472024406@qq.com>
Date: 2016年7月25日 15:16:25 +0800
Subject: [PATCH 113/195] Update
why-is-subtracting-these-two-times-in-1927-giving-a-strange-result.md
---
...ubtracting-these-two-times-in-1927-giving-a-strange-result.md | 1 +
1 file changed, 1 insertion(+)
diff --git a/contents/why-is-subtracting-these-two-times-in-1927-giving-a-strange-result.md b/contents/why-is-subtracting-these-two-times-in-1927-giving-a-strange-result.md
index d2f4449..d436087 100644
--- a/contents/why-is-subtracting-these-two-times-in-1927-giving-a-strange-result.md
+++ b/contents/why-is-subtracting-these-two-times-in-1927-giving-a-strange-result.md
@@ -57,3 +57,4 @@ Locale(Locale.getDefault()): zh_CN
这只是美好但奇怪的世界时区中的一个插曲。
+stackoverflow链接:[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)
From a914c65177afe26a52b76a91537abb9b67039ca1 Mon Sep 17 00:00:00 2001
From: yu
Date: Tue, 2 Aug 2016 14:34:54 +0800
Subject: [PATCH 114/195] owen1190
---
contents/lookup-enum-by-string-value.md | 54 +++++++++++++++++++++++++
1 file changed, 54 insertions(+)
create mode 100644 contents/lookup-enum-by-string-value.md
diff --git a/contents/lookup-enum-by-string-value.md b/contents/lookup-enum-by-string-value.md
new file mode 100644
index 0000000..848010d
--- /dev/null
+++ b/contents/lookup-enum-by-string-value.md
@@ -0,0 +1,54 @@
+#通过String值查找enum中常量
+## 问题
+假设有一个枚举值
+ public enum Blah
+ {
+ A,B,C,D
+ }
+想通过一个String类型,找到所需要的枚举值。
+例如"A"->Blah.A
+是使用Enum.valueOf()方法吗?该如何使用
+## 回答
+Blah.valueOf("A")会得到Blah.A
+虽然api文档确实有静态方法valueOf()和values(),但是二者在编译期时才出现,而且在没出现在源程序中。
+例如可以采用Dialog.ModalityType显示了两种方法来处理这种情况。
+备注:Blah.valueOf("A")的方法是区分大小写,且不能含有空格。
+
+如果String值与enum中不相同的查找方法:
+
+ public enum Blah
+ {
+ A("text1"),
+ B("text2"),
+ C("text3"),
+ D("text4");
+ private String text;
+ Blah(String text)
+ {
+ this.text = text;
+ }
+ public String getText()
+ {
+ return this.text;
+ }
+
+ public static Blah fromString(String text)
+ {
+ if (text != null)
+ {
+ for (Blah b : Blah.values())
+ {
+ if (text.equalsIgnoreCase(b.text))
+ {
+ return b;
+ }
+ }
+ }
+ return null;
+ }
+ }
+
+备注:throw new IllegalArgumentException("No constant with text"+text+"found")会比直接抛出null更好
+
+原文链接:
+> http://stackoverflow.com/questions/604424/lookup-enum-by-string-value#
\ No newline at end of file
From 7c7b598cb004ff85f5c93e8c8735309ea4a878cc Mon Sep 17 00:00:00 2001
From: giantray <51961070@qq.com>
Date: Wed, 3 Aug 2016 00:11:16 +0800
Subject: [PATCH 115/195] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=B7=B2=E7=BF=BB?=
=?UTF-8?q?=E8=AF=91=E9=97=AE=E9=A2=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/README.md b/README.md
index 09ff925..0295a5e 100644
--- a/README.md
+++ b/README.md
@@ -56,6 +56,7 @@ stackoverflow-Java-top-qa
* [serialVersionUID 有什么作用?该如何使用?](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/what-is-a-serialversionuid-and-why-should-i-use-it.md)
* [为什么Java的```Vector```类被认为是过时的或者废弃的](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/why-is-java-vector-class-considered-obsolete-or-deprecated.md)
* [Java的foreach循环是如何工作的](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-does-the-java-for-each-loop-work.md)
+* [为什么相减这两个时间(1927年)会得到奇怪的结果](/contens/why-is-subtracting-these-two-times-(in-1927)-giving-a-strange-result.md)
> 编程技巧
@@ -101,7 +102,6 @@ stackoverflow-Java-top-qa
* [如何获取Android设备唯一ID](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/is-there-a-unique-android-device-id.md)
### 待翻译问题链接(还剩x问题)
-- [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)
- [Proper use cases for Android UserManager.isUserAGoat()?](http://stackoverflow.com/questions/13375357/proper-use-cases-for-android-usermanager-isuseragoat)
- [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)
- [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)
From 0c8be56af7fcd1d0180215eacc2e09bcf89678fe Mon Sep 17 00:00:00 2001
From: giantray <51961070@qq.com>
Date: Wed, 3 Aug 2016 00:23:47 +0800
Subject: [PATCH 116/195] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=B7=B2=E7=BF=BB?=
=?UTF-8?q?=E8=AF=91=E9=97=AE=E9=A2=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/README.md b/README.md
index 0295a5e..f42e967 100644
--- a/README.md
+++ b/README.md
@@ -56,7 +56,10 @@ stackoverflow-Java-top-qa
* [serialVersionUID 有什么作用?该如何使用?](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/what-is-a-serialversionuid-and-why-should-i-use-it.md)
* [为什么Java的```Vector```类被认为是过时的或者废弃的](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/why-is-java-vector-class-considered-obsolete-or-deprecated.md)
* [Java的foreach循环是如何工作的](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-does-the-java-for-each-loop-work.md)
-* [为什么相减这两个时间(1927年)会得到奇怪的结果](/contens/why-is-subtracting-these-two-times-(in-1927)-giving-a-strange-result.md)
+* [为什么相减这两个时间(1927年)会得到奇怪的结果](/contents/why-is-subtracting-these-two-times-(in-1927)-giving-a-strange-result.md)
+* [Java 中如何将 String 转换为 enum](/contents/lookup-enum-by-string-value.md)
+* [该什么时候使用 ThreadLocal变量,它是如何工作的](/contents/when-and-how-should-i-use-a-threadlocal-variable.md)
+* [servlets的运行原理](/contents/how-do-servlets-work-instantiation-shared-variables-and-multithreading.md)
> 编程技巧
@@ -118,7 +121,6 @@ stackoverflow-Java-top-qa
- [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)
- [What exactly is Apache Camel?](http://stackoverflow.com/questions/8845186/what-exactly-is-apache-camel)
- [How can I permanently have line numbers in IntelliJ?](http://stackoverflow.com/questions/13751/how-can-i-permanently-have-line-numbers-in-intellij)
-- [How do servlets work? Instantiation, shared variables and multithreading](http://stackoverflow.com/questions/3106452/how-do-servlets-work-instantiation-shared-variables-and-multithreading)
- [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)
- [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)
- [How can I generate an MD5 hash?](http://stackoverflow.com/questions/415953/how-can-i-generate-an-md5-hash)
@@ -129,13 +131,11 @@ stackoverflow-Java-top-qa
- [Iterating through a list, avoiding ConcurrentModificationException when removing in loop](http://stackoverflow.com/questions/223918/iterating-through-a-list-avoiding-concurrentmodificationexception-when-removing)
- [What is the Java equivalent for LINQ?](http://stackoverflow.com/questions/1217228/what-is-the-java-equivalent-for-linq)
- [Hibernate hbm2ddl.auto possible values and what they do?](http://stackoverflow.com/questions/438146/hibernate-hbm2ddl-auto-possible-values-and-what-they-do)
-- [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)
- [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)
- [Sort ArrayList of custom Objects by property](http://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property)
- [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)
- [Efficiency of Java "Double Brace Initialization"?](http://stackoverflow.com/questions/924285/efficiency-of-java-double-brace-initialization)
- [Why does this go into an infinite loop?](http://stackoverflow.com/questions/3831341/why-does-this-go-into-an-infinite-loop)
-- [When and how should I use a ThreadLocal variable?](http://stackoverflow.com/questions/817856/when-and-how-should-i-use-a-threadlocal-variable)
- [decompiling DEX into Java sourcecode](http://stackoverflow.com/questions/1249973/decompiling-dex-into-java-sourcecode)
- [How to create a generic array in Java?](http://stackoverflow.com/questions/529085/how-to-create-a-generic-array-in-java)
- [Why does Math.round(0.49999999999999994) return 1](http://stackoverflow.com/questions/9902968/why-does-math-round0-49999999999999994-return-1)
From dd26ebd6525b18251d891ef32c4e3fd45273296b Mon Sep 17 00:00:00 2001
From: giantray <51961070@qq.com>
Date: Wed, 3 Aug 2016 00:30:20 +0800
Subject: [PATCH 117/195] Merge branch 'master' of
https://github.com/andysim3d/stackoverflow-java-top-qa into andysim3d-master
# Conflicts:
# contents/lookup-enum-by-string-value.md
---
contents/how-can-i-generate-an-md5-hash.md | 25 ++++++++++++++++++++++
contents/lookup-enum-by-string-value.md | 6 +++---
2 files changed, 28 insertions(+), 3 deletions(-)
create mode 100644 contents/how-can-i-generate-an-md5-hash.md
diff --git a/contents/how-can-i-generate-an-md5-hash.md b/contents/how-can-i-generate-an-md5-hash.md
new file mode 100644
index 0000000..b122024
--- /dev/null
+++ b/contents/how-can-i-generate-an-md5-hash.md
@@ -0,0 +1,25 @@
+##如何计算MD5值
+
+###问题
+Java中有没有方法可以计算一个String的MD5值?
+
+
+###回答
+你可以用 ```MessageDigest``` 的MD5实例来计算String的MD5值。
+
+使用 ```MessageDigest``` 和 String 时,一定要显式声明你的数据编码类型。如果你使用无参的 ```String.getBytes()``` , 它会以当前平台的默认编码来转换数据。不同平台的默认编码可能是不同的,这可能会导致你的数据不一致。
+
+``` java
+import java.security.*;
+
+..
+
+byte[] bytesOfMessage = yourString.getBytes("UTF-8");
+MessageDigest md = MessageDigest.getInstance("MD5");
+byte[] thedigest = md.digest(bytesOfMessage);
+```
+
+如果你的要计算的数据量很大,你可以循环使用 ```.update(byte[])``` 方法来加载数据。加载完毕后用 ```.digest()``` 方法来得到计算出的MD5值。
+
+stackoverflow链接
+http://stackoverflow.com/questions/415953/how-can-i-generate-an-md5-hash
diff --git a/contents/lookup-enum-by-string-value.md b/contents/lookup-enum-by-string-value.md
index 2bb583c..99f83c4 100644
--- a/contents/lookup-enum-by-string-value.md
+++ b/contents/lookup-enum-by-string-value.md
@@ -83,7 +83,7 @@ public static > T getEnumFromString(Class c, String string)
public static MyEnum fromString(String name) {
return getEnumFromString(MyEnum.class, name);
}
-```
+```
如果的enums不是全部大写,只需要修改 `Enum.valueOf` 这一行。
很遗憾,我不能使用 `T.class` 传给 `Enum.valueOf`,因为 `T`会被擦出。
@@ -100,7 +100,7 @@ _完整方法签名 `Optional getIfPresent(Class enumClass, String value)`
---
_其他的答案都大同小异,感兴趣的可以看原帖_
-_原帖:[Convert a String to an enum in Java](http://stackoverflow.com/questions/604424/lookup-enum-by-string-value)_
+stackoverflow链接
+http://stackoverflow.com/questions/604424/lookup-enum-by-string-value
_译者:[MagicWolf](https://github.com/DaiDongLiang)_
-
From 9eafdcb795657dc097db0206730162a5908aba69 Mon Sep 17 00:00:00 2001
From: giantray <51961070@qq.com>
Date: Wed, 3 Aug 2016 00:32:58 +0800
Subject: [PATCH 118/195] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=B7=B2=E7=BF=BB?=
=?UTF-8?q?=E8=AF=91=E9=97=AE=E9=A2=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/README.md b/README.md
index f42e967..b464834 100644
--- a/README.md
+++ b/README.md
@@ -60,6 +60,7 @@ stackoverflow-Java-top-qa
* [Java 中如何将 String 转换为 enum](/contents/lookup-enum-by-string-value.md)
* [该什么时候使用 ThreadLocal变量,它是如何工作的](/contents/when-and-how-should-i-use-a-threadlocal-variable.md)
* [servlets的运行原理](/contents/how-do-servlets-work-instantiation-shared-variables-and-multithreading.md)
+* [如何计算MD5值](/contents/how-can-i-generate-an-md5-hash.md)
> 编程技巧
@@ -123,7 +124,6 @@ stackoverflow-Java-top-qa
- [How can I permanently have line numbers in IntelliJ?](http://stackoverflow.com/questions/13751/how-can-i-permanently-have-line-numbers-in-intellij)
- [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)
- [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)
-- [How can I generate an MD5 hash?](http://stackoverflow.com/questions/415953/how-can-i-generate-an-md5-hash)
- [Uncatchable ChuckNorrisException](http://stackoverflow.com/questions/13883166/uncatchable-chucknorrisexception)
- [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)
- [Update Eclipse with Android development tools v. 23](http://stackoverflow.com/questions/24437564/update-eclipse-with-android-development-tools-v-23)
From b609912a2723beeea947ce0001870c1d742dd70f Mon Sep 17 00:00:00 2001
From: giantray <51961070@qq.com>
Date: Wed, 3 Aug 2016 00:36:29 +0800
Subject: [PATCH 119/195] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=B7=B2=E7=BF=BB?=
=?UTF-8?q?=E8=AF=91=E9=97=AE=E9=A2=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 2 +-
...times-(in-1927)-giving-a-strange-result.md | 80 -------------------
2 files changed, 1 insertion(+), 81 deletions(-)
delete mode 100644 contents/why-is-subtracting-these-two-times-(in-1927)-giving-a-strange-result.md
diff --git a/README.md b/README.md
index b464834..e672a6b 100644
--- a/README.md
+++ b/README.md
@@ -56,7 +56,7 @@ stackoverflow-Java-top-qa
* [serialVersionUID 有什么作用?该如何使用?](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/what-is-a-serialversionuid-and-why-should-i-use-it.md)
* [为什么Java的```Vector```类被认为是过时的或者废弃的](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/why-is-java-vector-class-considered-obsolete-or-deprecated.md)
* [Java的foreach循环是如何工作的](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-does-the-java-for-each-loop-work.md)
-* [为什么相减这两个时间(1927年)会得到奇怪的结果](/contents/why-is-subtracting-these-two-times-(in-1927)-giving-a-strange-result.md)
+* [为什么相减这两个时间(1927年)会得到奇怪的结果](/contents/why-is-subtracting-these-two-times-in-1927-giving-a-strange-result.md)
* [Java 中如何将 String 转换为 enum](/contents/lookup-enum-by-string-value.md)
* [该什么时候使用 ThreadLocal变量,它是如何工作的](/contents/when-and-how-should-i-use-a-threadlocal-variable.md)
* [servlets的运行原理](/contents/how-do-servlets-work-instantiation-shared-variables-and-multithreading.md)
diff --git a/contents/why-is-subtracting-these-two-times-(in-1927)-giving-a-strange-result.md b/contents/why-is-subtracting-these-two-times-(in-1927)-giving-a-strange-result.md
deleted file mode 100644
index d4f5d19..0000000
--- a/contents/why-is-subtracting-these-two-times-(in-1927)-giving-a-strange-result.md
+++ /dev/null
@@ -1,80 +0,0 @@
-## 为什么相减这两个时间(1927年)会得到奇怪的结果?
-
-### 问题
-如果运行下面的代码,我想要做的是解析两个相差1秒的日期字符串:
-```java
-public static void main(String[] args) throws ParseException {
- SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String str3 = "1927-12-31 23:54:07";
- String str4 = "1927-12-31 23:54:08";
- Date sDt3 = sf.parse(str3);
- Date sDt4 = sf.parse(str4);
- long ld3 = sDt3.getTime() /1000;
- long ld4 = sDt4.getTime() /1000;
- System.out.println(ld4-ld3);
-}
-```
-其结果是
-```
-353
-```
-为什么`ld4-ld3` 结果不是`1`(因为我期望得到1秒的时间差)而是`353`?
-
-如果我把两个日期都换成1秒后:
-```java
-String str3 = "1927-12-31 23:54:08";
-String str4 = "1927-12-31 23:54:09";
-```
-这样`ld4-ld3`就为`1`。
-
----
-java 版本
-```
-java version "1.6.0_22"
-Java(TM) SE Runtime Environment (build 1.6.0_22-b04)
-Dynamic Code Evolution Client VM (build 0.2-b02-internal, 19.0-b04-internal, mixed mode)
-```
-时区(`TimeZone.getDefault()`):
-```
-sun.util.calendar.ZoneInfo[id="Asia/Shanghai",
-offset=28800000,dstSavings=0,
-useDaylight=false,
-transitions=19,
-lastRule=null]
-
-Locale(Locale.getDefault()): zh_CN
-```
-## 回答
-这是12月31号上海时区切换的错误。
-
-[这个页面](http://www.timeanddate.com/time/change/china/shanghai?year=1927)上有关于上海1927的详细说明。因为1927年末尾的午夜,时间回滚了5分52秒,所以"1927-12-31 23:54:08" 实际上发生了两次,并且很明显java解析立即得到了可能的后面一个,而忽略了其差别。
-
-这在经常弄混且丰富的世界时区中还是一个插曲。
-
-如果重新编译[TZDB](https://github.com/nodatime/nodatime/blob/master/src/NodaTime.Demo/StackOverflowExamples.cs#L68)的2013a版本,之前的问题就不再能解释类型行为。在2013a 的版本中,结果将是358秒,切换时间由23:54:03 变为23:54:08。
-
-我能注意到这个都是因为我在[unit tests](https://github.com/nodatime/nodatime/blob/master/src/NodaTime.Demo/StackOverflowExamples.cs#L68)收集像Noda Time 这样的问题
-
-这个测试现在已经被更新,但是也表明没有历史数据是安全的。
-
-在TZDB 2014f中, 这个时间切换移动到了1900年12月31日,并且也被修改成唯一的343秒变化(如果你知道我在说啥的话,`t` 和 `t+1` 之间是343 秒)。
-
-**更新** 为了回答[ Ken Kin's question](http://stackoverflow.com/questions/6841333/why-is-subtracting-these-two-times-in-1927-giving-a-strange-result/6841479#comment22684267_6841479)的问题,看起来像是,对于任何在1900 UTC 之前的时间,java 在他们的标准时间中进行了简单的时区实现。
-```java
-import java.util.TimeZone;
-
-public class Test {
- public static void main(String[] args) throws Exception {
- long startOf1900Utc = -2208988800000L;
- for (String id : TimeZone.getAvailableIDs()) {
- TimeZone zone = TimeZone.getTimeZone(id);
- if (zone.getRawOffset() != zone.getOffset(startOf1900Utc - 1)) {
- System.out.println(id);
- }
- }
- }
-}
-```
-上面的代码在我的windows 机器上没有任何输出,所以对于任何相对标准时间在1900年前且有偏移的时间,都会有时间过渡。TZDB 自己有数据回退到更早之前,并且不依赖任何修正过的标准时间(就是`getRawOffset`假设的有效的概念),所以其他库不再需要介绍这个切换。
-
-stackoverflow[链接](http://stackoverflow.com/questions/6841333/why-is-subtracting-these-two-times-in-1927-giving-a-strange-result)
\ No newline at end of file
From 2d7c9a047e2d2b9de5c73309ae1c201f2ffb2483 Mon Sep 17 00:00:00 2001
From: giantray <51961070@qq.com>
Date: Wed, 3 Aug 2016 09:16:03 +0800
Subject: [PATCH 120/195] =?UTF-8?q?=E6=B7=BB=E5=8A=A0gitbook?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
SUMMARY.md | 1 +
1 file changed, 1 insertion(+)
create mode 100644 SUMMARY.md
diff --git a/SUMMARY.md b/SUMMARY.md
new file mode 100644
index 0000000..9f8edf2
--- /dev/null
+++ b/SUMMARY.md
@@ -0,0 +1 @@
+\# Summary* [前言](README.md)
\ No newline at end of file
From 465351b8be23f2cb294a5adc1ea62e803a002269 Mon Sep 17 00:00:00 2001
From: andysim3d
Date: Thu, 4 Aug 2016 14:55:05 -0400
Subject: [PATCH 121/195] translate
how-do-i-create-a-java-string-from-the-contents-of-a-file
---
...java-string-from-the-contents-of-a-file.md | 53 +++++++++++++++++++
1 file changed, 53 insertions(+)
create mode 100644 contents/how-do-i-create-a-java-string-from-the-contents-of-a-file.md
diff --git a/contents/how-do-i-create-a-java-string-from-the-contents-of-a-file.md b/contents/how-do-i-create-a-java-string-from-the-contents-of-a-file.md
new file mode 100644
index 0000000..0345f21
--- /dev/null
+++ b/contents/how-do-i-create-a-java-string-from-the-contents-of-a-file.md
@@ -0,0 +1,53 @@
+##如何从文件里读取字符串
+
+###从文件里读取所有文本:
+
+代码:
+```java
+static String readFile(String path, Charset encoding)
+ throws IOException
+{
+ byte[] encoded = Files.readAllBytes(Paths.get(path));
+ return new String(encoded, encoding);
+}
+
+```
+
+###一行一行读入文本:
+
+Java 7 提供了一个方便的方法可以直接将文件中的文本一行一行读入,存放在一个List容器里。
+```JAVA
+List lines = Files.readAllLines(Paths.get(path), encoding);
+```
+
+###内存使用率
+
+第一个方法,一次读取所有文本的方法,占用内存较多,因为它一次性保留了文件的所有原始信息,包括换行符之类的"无用"字符。
+
+第二个方法,按行读入,比起一次性全部读入,要消耗更少的内存。因为它每次只将一行的文件信息放在缓存中。然而,如果文本文件很大,这种方法依然会占用很多内存。
+
+如果你的程序需要处理很大的文本文件,在设计的时候就要考虑,分配一块固定的缓存,每次从流中读入文件的一部分放入缓存,处理,然后清空缓存,把下一部分读入缓存,直道处理完所有的数据。
+
+这里的"很大"是相对于计算机性能的。一般来说,几十个G的文件应当算是大文件。
+
+###字符编码
+
+还有一件事需要注意,就是字符编码。不同的平台有自己的默认编码,所以有时候你的程序需要指定编码,来保持平台无关/跨平台。
+
+```StandardCharsets``` 类定义了常用的编码类型,你可以用如下方法调用:
+
+```java
+String content = readFile("test.txt", StandardCharsets.UTF_8);
+```
+
+可以通过```Charset```类来获得平台默认的字符编码。
+
+```java
+String content = readFile("test.txt", Charset.defaultCharset());
+```
+
+注: 这个答案与之前Java6版本时的答案完全不同。Java 7 新增的工具类极大的优化了字符处理,文件读取等功能。Java 6 常用的内存映射方法已不适合在Java 7 以后的版本使用。
+
+
+
+
From b11f01e43ed840a49daac64aea146969c426a854 Mon Sep 17 00:00:00 2001
From: Andrew
Date: Thu, 4 Aug 2016 14:57:00 -0400
Subject: [PATCH 122/195] Update
how-do-i-create-a-java-string-from-the-contents-of-a-file.md
---
...how-do-i-create-a-java-string-from-the-contents-of-a-file.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/contents/how-do-i-create-a-java-string-from-the-contents-of-a-file.md b/contents/how-do-i-create-a-java-string-from-the-contents-of-a-file.md
index 0345f21..323fdeb 100644
--- a/contents/how-do-i-create-a-java-string-from-the-contents-of-a-file.md
+++ b/contents/how-do-i-create-a-java-string-from-the-contents-of-a-file.md
@@ -26,7 +26,7 @@ List lines = Files.readAllLines(Paths.get(path), encoding);
第二个方法,按行读入,比起一次性全部读入,要消耗更少的内存。因为它每次只将一行的文件信息放在缓存中。然而,如果文本文件很大,这种方法依然会占用很多内存。
-如果你的程序需要处理很大的文本文件,在设计的时候就要考虑,分配一块固定的缓存,每次从流中读入文件的一部分放入缓存,处理,然后清空缓存,把下一部分读入缓存,直道处理完所有的数据。
+如果你的程序需要处理很大的文本文件,在设计的时候就要考虑,分配一块固定的缓存,每次从流中读入文件的一部分放入缓存,处理,然后清空缓存,把下一部分读入缓存,直到处理完所有的数据。
这里的"很大"是相对于计算机性能的。一般来说,几十个G的文件应当算是大文件。
From 1eeda60ec3fbeaeb2548d60b4969fef376655f82 Mon Sep 17 00:00:00 2001
From: Andrew
Date: Thu, 4 Aug 2016 14:58:53 -0400
Subject: [PATCH 123/195] Update
how-do-i-create-a-java-string-from-the-contents-of-a-file.md
---
...w-do-i-create-a-java-string-from-the-contents-of-a-file.md | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/contents/how-do-i-create-a-java-string-from-the-contents-of-a-file.md b/contents/how-do-i-create-a-java-string-from-the-contents-of-a-file.md
index 323fdeb..1dc835b 100644
--- a/contents/how-do-i-create-a-java-string-from-the-contents-of-a-file.md
+++ b/contents/how-do-i-create-a-java-string-from-the-contents-of-a-file.md
@@ -48,6 +48,8 @@ String content = readFile("test.txt", Charset.defaultCharset());
注: 这个答案与之前Java6版本时的答案完全不同。Java 7 新增的工具类极大的优化了字符处理,文件读取等功能。Java 6 常用的内存映射方法已不适合在Java 7 以后的版本使用。
-
+### 原文链接
+http://stackoverflow.com/questions/326390/how-do-i-create-a-java-string-from-the-contents-of-a-file
+
From e719facf644847a17a925122a39c96a255850b09 Mon Sep 17 00:00:00 2001
From: yu
Date: Fri, 5 Aug 2016 11:04:24 +0800
Subject: [PATCH 124/195]
what-is-the-difference-between-a-soft-reference-and-a-weak-reference-in-java
---
...-reference-and-a-weak-reference-in-java.md | 38 +++++++++++++++++++
1 file changed, 38 insertions(+)
create mode 100644 contents/what-is-the-difference-between-a-soft-reference-and-a-weak-reference-in-java.md
diff --git a/contents/what-is-the-difference-between-a-soft-reference-and-a-weak-reference-in-java.md b/contents/what-is-the-difference-between-a-soft-reference-and-a-weak-reference-in-java.md
new file mode 100644
index 0000000..eb01281
--- /dev/null
+++ b/contents/what-is-the-difference-between-a-soft-reference-and-a-weak-reference-in-java.md
@@ -0,0 +1,38 @@
+# Java中软引用和弱引用的区别
+## 问题
+题目就是问题
+
+## 解答
+### 回答1
+从Ethan Nicholas的《Understanding Weak References》中
+
+弱引用:
+放置一个弱引用的作用,不是强有力强制一个对象保存在内存中。弱引用允许利用垃圾收集者的能力去决定可达性,所以你不需要自己做,你只需要创建一个软引用:
+
+ WeakReference weakWidgt = new WeakReference(widgt);
+
+然后在代码别的地方你可以使用 `weakWidget.get()` 来获取真实的 `Widgt` 对象,当然弱引用足以强大能抵制垃圾收集器,所以你也许发现(如果没有强引用指向widget)`weakWidget.get()`突然开始返回null
+
+软引用
+
+软引用就像弱引用一样,除了它不会着急将引用的对象扔出去。只有弱可达性的对象(这样的对象最强的引用只能是弱引用)将在下一次垃圾收集处理中被抛弃,但是软可达性的对象通常可以坚持一会。
+
+软引用不要求与弱引用有什么不同,但是实际中,只要内存足够,软可达的对象通常会维持下去。对于缓存来说,这是个不错的基础,就像以上图像缓存描述,虽然可以让垃圾收集者担心对象是如何可达(一个强可达性的对象从不会从缓存中移除)和她们需要消耗多少内存
+
+而且Peter Kessler备注到
+
+Sun JRE 对待软引用和弱引用是不同的。如果内存是够用的。我们应坚持用软引用引用对象。一个细节是:对于客户端和服务器,JRE的政策是不同的:客户端,JRE试图保持通过清除软引用而不是扩大堆内存来使改变小点,而服务器端,JRE通过扩大堆内存让性能更好。没有一种通用的方法。
+
+### 回答2
+弱引用对象很快被收集。如果GC发现一个对象是弱引用(只能通过弱引用可达),它会立刻清除弱引用对象。同样的,对于在程序保持关联信息的对象保持一个引用是不错的,像关于类的缓存存储的反射信息或一个对象的包装器等等。没有意义地跟随相连对象的任何事物都会被清除掉。当弱引用清除掉时,它会进入到引用队列中,同时丢弃关联的对象。你保持关于对象额外的信息,但是一旦对象引用不要了,信息也就不需要了。总之,在某些情境下,你可以创建WeakReference的子类,保持在WeakReference的子类中对象的额外信息。WeakReference的其他典型应用是与Map连接,以保持规范化的例子。
+
+在另一方面,软引用有利于外部缓存,再创造资源,因为GC会延迟清理他们。它能保证所有软引用会在内存溢出之前被清除,所以它们不会造成内存溢出。
+
+典型的使用例子是保持从一个文件内容解析形式。在你载入文件,解析和与解析过代表的根对象保持一个软引用的地方扩展系统。在你下次需要文件时,你试图通过软引用恢复。如果可以恢复,你会在其他地方载入、解析你分享的文件,如果同时GC清理掉,你也可以重新载入。这样的话,你利用空内存可以做到性能最优化,但是不要内存溢出。
+光保持一个软引用不会造成溢出。如果在另一方面你误用软引用,且弱引用被使用了(也就是说,你保持与较强引用的对象相连的信息,然后当引用对象被清除,你也丢弃信息),你可能会内存溢出,因为在进入引用队列时,也许碰巧没有及时丢弃相连的对象。
+
+所以,使用软引用还是弱引用是取决于用法的。如果你的信息构造起来较为复杂,但是尽管如此仍想从别的数据再构造信息,使用软引用。如果你对一些数据的规范化实例保持引用,或者你想对一个"不拥有的"对象保持引用(就是防止被垃圾回收),这样就使用弱引用。
+
+
+原文:
+> http://stackoverflow.com/questions/299659/what-is-the-difference-between-a-soft-reference-and-a-weak-reference-in-java
\ No newline at end of file
From dfc89235bb45818a5b4c5544bd847729441a8cb0 Mon Sep 17 00:00:00 2001
From: yu
Date: Fri, 5 Aug 2016 14:27:19 +0800
Subject: [PATCH 125/195]
what-is-the-difference-between-a-soft-reference-and-a-weak-reference-in-java
---
...-reference-and-a-weak-reference-in-java.md | 38 +++++++++++++++++++
1 file changed, 38 insertions(+)
create mode 100644 contents/what-is-the-difference-between-a-soft-reference-and-a-weak-reference-in-java.md
diff --git a/contents/what-is-the-difference-between-a-soft-reference-and-a-weak-reference-in-java.md b/contents/what-is-the-difference-between-a-soft-reference-and-a-weak-reference-in-java.md
new file mode 100644
index 0000000..eb01281
--- /dev/null
+++ b/contents/what-is-the-difference-between-a-soft-reference-and-a-weak-reference-in-java.md
@@ -0,0 +1,38 @@
+# Java中软引用和弱引用的区别
+## 问题
+题目就是问题
+
+## 解答
+### 回答1
+从Ethan Nicholas的《Understanding Weak References》中
+
+弱引用:
+放置一个弱引用的作用,不是强有力强制一个对象保存在内存中。弱引用允许利用垃圾收集者的能力去决定可达性,所以你不需要自己做,你只需要创建一个软引用:
+
+ WeakReference weakWidgt = new WeakReference(widgt);
+
+然后在代码别的地方你可以使用 `weakWidget.get()` 来获取真实的 `Widgt` 对象,当然弱引用足以强大能抵制垃圾收集器,所以你也许发现(如果没有强引用指向widget)`weakWidget.get()`突然开始返回null
+
+软引用
+
+软引用就像弱引用一样,除了它不会着急将引用的对象扔出去。只有弱可达性的对象(这样的对象最强的引用只能是弱引用)将在下一次垃圾收集处理中被抛弃,但是软可达性的对象通常可以坚持一会。
+
+软引用不要求与弱引用有什么不同,但是实际中,只要内存足够,软可达的对象通常会维持下去。对于缓存来说,这是个不错的基础,就像以上图像缓存描述,虽然可以让垃圾收集者担心对象是如何可达(一个强可达性的对象从不会从缓存中移除)和她们需要消耗多少内存
+
+而且Peter Kessler备注到
+
+Sun JRE 对待软引用和弱引用是不同的。如果内存是够用的。我们应坚持用软引用引用对象。一个细节是:对于客户端和服务器,JRE的政策是不同的:客户端,JRE试图保持通过清除软引用而不是扩大堆内存来使改变小点,而服务器端,JRE通过扩大堆内存让性能更好。没有一种通用的方法。
+
+### 回答2
+弱引用对象很快被收集。如果GC发现一个对象是弱引用(只能通过弱引用可达),它会立刻清除弱引用对象。同样的,对于在程序保持关联信息的对象保持一个引用是不错的,像关于类的缓存存储的反射信息或一个对象的包装器等等。没有意义地跟随相连对象的任何事物都会被清除掉。当弱引用清除掉时,它会进入到引用队列中,同时丢弃关联的对象。你保持关于对象额外的信息,但是一旦对象引用不要了,信息也就不需要了。总之,在某些情境下,你可以创建WeakReference的子类,保持在WeakReference的子类中对象的额外信息。WeakReference的其他典型应用是与Map连接,以保持规范化的例子。
+
+在另一方面,软引用有利于外部缓存,再创造资源,因为GC会延迟清理他们。它能保证所有软引用会在内存溢出之前被清除,所以它们不会造成内存溢出。
+
+典型的使用例子是保持从一个文件内容解析形式。在你载入文件,解析和与解析过代表的根对象保持一个软引用的地方扩展系统。在你下次需要文件时,你试图通过软引用恢复。如果可以恢复,你会在其他地方载入、解析你分享的文件,如果同时GC清理掉,你也可以重新载入。这样的话,你利用空内存可以做到性能最优化,但是不要内存溢出。
+光保持一个软引用不会造成溢出。如果在另一方面你误用软引用,且弱引用被使用了(也就是说,你保持与较强引用的对象相连的信息,然后当引用对象被清除,你也丢弃信息),你可能会内存溢出,因为在进入引用队列时,也许碰巧没有及时丢弃相连的对象。
+
+所以,使用软引用还是弱引用是取决于用法的。如果你的信息构造起来较为复杂,但是尽管如此仍想从别的数据再构造信息,使用软引用。如果你对一些数据的规范化实例保持引用,或者你想对一个"不拥有的"对象保持引用(就是防止被垃圾回收),这样就使用弱引用。
+
+
+原文:
+> http://stackoverflow.com/questions/299659/what-is-the-difference-between-a-soft-reference-and-a-weak-reference-in-java
\ No newline at end of file
From cbbb387fe8125c1890c9a7353418a93d9b0140cc Mon Sep 17 00:00:00 2001
From: "ziqiang.deng"
Date: Tue, 9 Aug 2016 18:25:01 +0800
Subject: [PATCH 126/195] what-is-the-difference-between-jsf-servlet-and-jsp
finished
---
...-difference-between-jsf-servlet-and-jsp.md | 46 +++++++++++++++++++
1 file changed, 46 insertions(+)
create mode 100644 contents/what-is-the-difference-between-jsf-servlet-and-jsp.md
diff --git a/contents/what-is-the-difference-between-jsf-servlet-and-jsp.md b/contents/what-is-the-difference-between-jsf-servlet-and-jsp.md
new file mode 100644
index 0000000..93a31c7
--- /dev/null
+++ b/contents/what-is-the-difference-between-jsf-servlet-and-jsp.md
@@ -0,0 +1,46 @@
+## JSF, Servlet 和 JSP (三种技术)有什么区别?
+
+###问题
+JSP 和 Servlet 有什么关系?JSP 是某种 Servlet 吗?JSP 和 JSF 又有什么关系?JSF 是某种基于JSP的,预构建好的 UI 吗,像
+ASP.NET-MVC 那样?
+
+
+###回答1
+
+#### JSP(Java Server Pages)
+JSP 是一种运行在服务器上的Java 视图技术,它允许你写入模版化的文本(例如客户端代码 HTML, CSS, JavaScript等)。JSP 支持标签库(taglibs),标签库由Java 代码实现,让你可以动态地控制页面输出。JSTL 便是一种比较有名的标签库。JSP 同样支持表达式语言(expression language),表达式语言可以用来访问后台数据(页面上可用的属性,request/session 对象等等), 通常与标签库结合使用。
+
+当一个 JSP 第一次被访问或者 webapp 启动时,servlet 容器会将 JSP 编译成一个继承了 HttpServlet 的类,然后在整个 webapp 生命周期内使用被编译后的类。可以在 servlet 容器的 work 目录下找到 JSP 对应的源代码。例如Tomcat 的 CATALINA.BASE/work 目录。
+当收到一个 JSP 请求时,servlet 容器会执行编译 JSP 生成的类,并将该类的输出(通常是 HTML/CSS/JS)发送到 客户端,客户端(WEB 浏览器) 会展示从服务端收到的内容。
+
+#### Servlet
+Servlet 是一种针对服务器端的 API,它用来响应客户端请求,并生成响应。比较有名的例子是 HttpServlet,它提供了响应 HTTP 请求(例如 GET POST)的方法。你可以从 web.xml 配置 HttpServlet 来监听某种 HTTP URL pattern 的请求,或者使用较新的 Java EE 6 @WebServlet 注解。
+
+当 Servlet 第一次被请求,或者 webapp 启动时,servlet 容器会创建该 Servlet 的实例,并在整个 webapp 的生命周期维持该实例在内存中。同一个实例会被复用,来响应匹配到 URL pattern 的请求。可以通过 HttpServletRequest 访问请求里的数据,通过 HttpServletResponse 控制响应。上边两个对象会是 HttpServlet 的重载方法 doGet()和 doPost() 的参数。
+
+#### JSF (JavaServer Faces)
+JSF 是一个基于组件的MVC框架,建立在 Servlet API 基础上,JSF 通过标签库提供组件,标签库又可以用于 JSP 或者其它 Java 视图技术例如 Facelets. Facelets 更适合JSF。即它提供了很厉害的模版功能例如组合组件,而JSP 基本上只提供了 `` 来支持模版,所以
+当你想用一个组件替换一组重复出现的组件时,你不得不使用原生的 Java 代码来创建自定义组件(这在 JSF 里并不那么清晰明了,而且带来很多冗余工作)。为了推进 Facelets,自从 JSF 2.0 之后,JSP 这种视图技术已经被废弃了。
+作为一种 MVC(Model-View-Controller)框架,JSF 提供了唯一的 FacesServlet 请求/响应控制器。它负责所有的 HTTP 请求/响应工作,
+例如 收集/校验/转换用户输入,将输入设置到 model 对象里,调用处理逻辑并输出响应。这样你基本上 只有一个 JSP或者 Facelets(XHTML) 页面用作视图,再加一个 Javabean 类当作 模型。 JSF 组件用来将模型和视图绑定起来(类似 ASP.NET web control 做的),然后 FacesServlet 使用 JSF 组件树来完成整个工作。
+
+### 其它答案选编
+
+参考以下链接
+
+[http://www.oracle.com/technetwork/java/faq-137059.html](http://www.oracle.com/technetwork/java/faq-137059.html)
+
+[https://jcp.org/en/introduction/faq](https://jcp.org/en/introduction/faq)
+
+JSP 是一种特殊的Servlet。
+
+JSF 是一个可以配合 JSP 使用的标签集。
+
+
+### stackoverflow原文链接:
+[http://stackoverflow.com/questions/2095397/what-is-the-difference-between-jsf-servlet-and-jsp](http://stackoverflow.com/questions/2095397/what-is-the-difference-between-jsf-servlet-and-jsp)
+
+
+
+
+
From 984c94b04a0a095b8ab320abb59150786bc75348 Mon Sep 17 00:00:00 2001
From: giantray <51961070@qq.com>
Date: 2016年8月17日 09:37:26 +0800
Subject: [PATCH 127/195] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=B7=B2=E7=BF=BB?=
=?UTF-8?q?=E8=AF=91=E9=97=AE=E9=A2=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 2 +-
.../comparing-java-enum-members-or-equals.md | 165 +++++++++---------
contents/lookup-enum-by-string-value.md | 62 +------
3 files changed, 89 insertions(+), 140 deletions(-)
diff --git a/README.md b/README.md
index e672a6b..b2e3b10 100644
--- a/README.md
+++ b/README.md
@@ -61,6 +61,7 @@ stackoverflow-Java-top-qa
* [该什么时候使用 ThreadLocal变量,它是如何工作的](/contents/when-and-how-should-i-use-a-threadlocal-variable.md)
* [servlets的运行原理](/contents/how-do-servlets-work-instantiation-shared-variables-and-multithreading.md)
* [如何计算MD5值](/contents/how-can-i-generate-an-md5-hash.md)
+* [Java中软引用和弱引用的区别](/what-is-the-difference-between-a-soft-reference-and-a-weak-reference-in-java.md)
> 编程技巧
@@ -140,7 +141,6 @@ stackoverflow-Java-top-qa
- [How to create a generic array in Java?](http://stackoverflow.com/questions/529085/how-to-create-a-generic-array-in-java)
- [Why does Math.round(0.49999999999999994) return 1](http://stackoverflow.com/questions/9902968/why-does-math-round0-49999999999999994-return-1)
- [Eclipse: Set maximum line length for auto formatting?](http://stackoverflow.com/questions/3697287/eclipse-set-maximum-line-length-for-auto-formatting)
-- [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)
- [What is the equivalent of the C++ Pair in Java?](http://stackoverflow.com/questions/156275/what-is-the-equivalent-of-the-c-pairl-r-in-java)
- [What is the difference between JSF, Servlet and JSP?](http://stackoverflow.com/questions/2095397/what-is-the-difference-between-jsf-servlet-and-jsp)
- [How do I "decompile" Java class files?](http://stackoverflow.com/questions/272535/how-do-i-decompile-java-class-files)
diff --git a/contents/comparing-java-enum-members-or-equals.md b/contents/comparing-java-enum-members-or-equals.md
index 0bd407f..867246c 100644
--- a/contents/comparing-java-enum-members-or-equals.md
+++ b/contents/comparing-java-enum-members-or-equals.md
@@ -1,80 +1,85 @@
-# 比较java中枚举成员是用"=="还是equals()
-
-## 问题
-java枚举被编译成带有私有构造器和一堆public的静态成员的类。当比较枚举中的两个成员时,经常使用.equals()方法,例如
- public useEnums(SomeEnum a)
- {
- if(a.equals(SomeEnum.SOME_ENUM_VALUE))
- {
- ...
- }
- ...
- }
-然而,偶然间使用"=="代替equals方法
- public useEnums2(SomeEnum a)
- {
- if(a == SomeEnum.SOME_ENUM_VALUE)
- {
- ...
- }
- ...
- }
-应该使用哪个呢?
-## 解答
-### 回答1
-技术上来说,都是对的,如果你看了equals的源码,它简单地遵从"==" ,然而我一般使用"==" 因为它对于空指针,比较安全
-### 回答2
-#### 能在枚举时使用用"=="吗?
-
-可以,枚举值有小型实例控制,允许你用"=="去比较实例,在文档中有说明:
-
-JLS 8.9 枚举
-一个枚举类型除了定义的那些枚举常量外没有其他实例了。
-试图明确地说明一种枚举类型是会导致编译期异常。在枚举中final clone方法确保枚举常量从不会被克隆,而且序列化机制会确保从不会因为反序列化而创造复制的实例。枚举类型的反射实例化也是被禁止的。总之,以上内容确保了除了定义的枚举常量之外,没有枚举类型实例。
-
-因为每个枚举常量只有一个实例,所以如果在比较两个参考值,至少有一个涉及到枚举常量时,允许使用"=="代替equals()。(equals()方法在枚举类中是一个final方法,在参数和返回结果时,很少调用父类的equals()方法,因此是一种恒等的比较。)
-
-这足够强力地支持Josh的建议,如果你坚持使用单例模式,最好的方法是用枚举类型强化单例属性(见Effective Java第二版中的第三条:用私有构造器或者枚举类型强化Singleton属性,或者单例模式的线程安全
->http://stackoverflow.com/questions/2912281/thread-safety-in-singleton/ )
-
-#### "=="和equals的区别
-通常情况下,==并不是可以替换equals,然而在枚举中是可以的。它们之间有两个重要的不同:
-
- "=="从不会抛出空指针异常
-
-
- enum Color { BLACK, WHITE };
-
- Color nothing = null;
-
- if (nothing == Color.BLACK); // 正常运行
-
- if (nothing.equals(Color.BLACK)); // 抛出空指
- 针异常
-
-在编译期,"=="会检查其类型的兼容性
-
- enum Color { BLACK, WHITE };
- enum Chiral { LEFT, RIGHT };
-
- if (Color.BLACK.equals(Chiral.LEFT)); // 编译正常
- if (Color.BLACK == Chiral.LEFT); // 无法编译,类型不兼容
-
-#### 在适用时,"=="可以被使用吗
-
-Bloch(effective java的作者)明确指出不可变类可以控制它们实例保证客户端"=="是可用的。枚举就被明确地证明了
-
-考虑静态工厂方法代替构造器
-它使得不可变的类可以确保不会存在两个相等的实例,即当且仅当a==b的时候才有a.equals(b)为true。如果类保证了这一点,它的客户端可以使用"=="操作符来代替equals(Object)方法,这样可以提升性能。枚举类型保证了这一点
-
-总之,在枚举中使用"=="优势:
-
-- 能运行
-- 更快
-- 在运行期更安全
-- 在编译期更安全
-
-备注:强有力的反击了那些认为foo.equals(bar)比foo==bar更有可读性的人们。
-
-原文地址:
-> http://stackoverflow.com/questions/1750435/comparing-java-enum-members-or-equals
\ No newline at end of file
+## 比较java枚举成员使用equal还是==
+
+### 问题
+我知道Java枚举会被编译成一个包含私有构造参数和一堆静态方法的类,当去比较两个枚举的时候,总是使用equals()方法,例如:
+```java
+public useEnums(SomeEnum a)
+{
+ if(a.equals(SomeEnum.SOME_ENUM_VALUE))
+ {
+ ...
+ }
+ ...
+}
+```
+除此之外,我也可以使用 == 替代equals() 方法
+```java
+public useEnums2(SomeEnum a)
+{
+ if(a == SomeEnum.SOME_ENUM_VALUE)
+ {
+ ...
+ }
+ ...
+}
+```
+我有5年以上的java编程经验,并且我想我也懂得 == 和 equals() 之间的区别,但是我仍然觉得很困惑,哪一个操作符才是我该使用的。
+
+### 答案
+
+二者皆对,如果你看过枚举的源码,你会发现在源码中,equals也仅仅非常简单的 == 。
+我使用 == ,因为无论如何,这个左值是可以为 null的
+
+
+译者补充 java.lang.Enum 中Equals 代码:
+```java
+public final boolean equals(Object other) {
+ return this==other;
+}
+```
+
+
+### 额外答案
+#### 能在枚举中使用 == 进行判断?
+答案是肯定的,因为枚举有着严格的实例化控制,所以你可以用 == 去做比较符,这个用法,在官方文档中也有明确的说明。
+
+>JLS 8.9 Enums
+>An enum type has no instances other than those defined by its enum constants.
+>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.
+>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.)
+
+#### 什么时候 == 和 equals 不一样?
+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:
+通常来说 == 不是一个 equals的一个备选方案,无论如何有2个重要的不同处需要考虑:
+
+##### == 不会抛出 NullPointerException
+```java
+enum Color { BLACK, WHITE };
+
+Color nothing = null;
+if (nothing == Color.BLACK); // runs fine
+if (nothing.equals(Color.BLACK)); // throws NullPointerException
+```
+##### == 在编译期检测类型兼容性
+```java
+enum Color { BLACK, WHITE };
+enum Chiral { LEFT, RIGHT };
+
+if (Color.BLACK.equals(Chiral.LEFT)); // compiles fine
+if (Color.BLACK == Chiral.LEFT); // DOESN'T COMPILE!!! Incompatible types!
+```
+
+#### 什么时候使用 == ?
+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.
+具体来说,那些提供恰当实例控制的不可变类能够保证 == 是可用的,枚举刚好符合这个条件。
+
+> Item 1: Consider static factory methods instead of constructors
+> [...] 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.
+
+总而言之,在枚举比较上使用 == , 因为:
+1. 能正常工作
+2. 更快
+3. 运行时是安全的
+4. 编译期也是安全的
+
+stackoverlfow链接:http://stackoverflow.com/questions/1750435/comparing-java-enum-members-or-equals
diff --git a/contents/lookup-enum-by-string-value.md b/contents/lookup-enum-by-string-value.md
index 05fd890..160aafa 100644
--- a/contents/lookup-enum-by-string-value.md
+++ b/contents/lookup-enum-by-string-value.md
@@ -1,63 +1,9 @@
-<<<<<<< HEAD -#通过String值查找enum中常量 -## 问题 -假设有一个枚举值 - public enum Blah - { - A,B,C,D - } -想通过一个String类型,找到所需要的枚举值。 -例如"A"->Blah.A
-是使用Enum.valueOf()方法吗?该如何使用
-## 回答
-Blah.valueOf("A")会得到Blah.A
-虽然api文档确实有静态方法valueOf()和values(),但是二者在编译期时才出现,而且在没出现在源程序中。
-例如可以采用Dialog.ModalityType显示了两种方法来处理这种情况。
-备注:Blah.valueOf("A")的方法是区分大小写,且不能含有空格。
-
-如果String值与enum中不相同的查找方法:
-
- public enum Blah
- {
- A("text1"),
- B("text2"),
- C("text3"),
- D("text4");
- private String text;
- Blah(String text)
- {
- this.text = text;
- }
- public String getText()
- {
- return this.text;
- }
-
- public static Blah fromString(String text)
- {
- if (text != null)
- {
- for (Blah b : Blah.values())
- {
- if (text.equalsIgnoreCase(b.text))
- {
- return b;
- }
- }
- }
- return null;
- }
- }
-
-备注:throw new IllegalArgumentException("No constant with text"+text+"found")会比直接抛出null更好
-
-原文链接:
-> http://stackoverflow.com/questions/604424/lookup-enum-by-string-value#
+Java 中如何将 String 转换为 enum
=======
-## Java 中如何将 String 转换为 enum
###问题
-我有一个 enum 类
+
+###我有一个 enum 类
``` java
public enum Blah {
@@ -159,5 +105,3 @@ _其他的答案都大同小异,感兴趣的可以看原帖_
stackoverflow链接
http://stackoverflow.com/questions/604424/lookup-enum-by-string-value
_译者:[MagicWolf](https://github.com/DaiDongLiang)_
-
->>>>>>> upstream/master
From 0edbc1809a580f004eeb5a79bfba442aba08080f Mon Sep 17 00:00:00 2001
From: giantray <51961070@qq.com>
Date: 2016年8月17日 09:42:40 +0800
Subject: [PATCH 128/195] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=B7=B2=E7=BF=BB?=
=?UTF-8?q?=E8=AF=91=E9=97=AE=E9=A2=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/README.md b/README.md
index b2e3b10..1339808 100644
--- a/README.md
+++ b/README.md
@@ -61,7 +61,7 @@ stackoverflow-Java-top-qa
* [该什么时候使用 ThreadLocal变量,它是如何工作的](/contents/when-and-how-should-i-use-a-threadlocal-variable.md)
* [servlets的运行原理](/contents/how-do-servlets-work-instantiation-shared-variables-and-multithreading.md)
* [如何计算MD5值](/contents/how-can-i-generate-an-md5-hash.md)
-* [Java中软引用和弱引用的区别](/what-is-the-difference-between-a-soft-reference-and-a-weak-reference-in-java.md)
+* [Java中软引用和弱引用的区别](/contents/what-is-the-difference-between-a-soft-reference-and-a-weak-reference-in-java.md)
> 编程技巧
@@ -83,6 +83,7 @@ stackoverflow-Java-top-qa
* [如何处理 java.lang.outOfMemoryError PermGen space error](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/dealing-with-java-lang-outofmemoryerror-permgen-space-error.md)
* [如何在整数左填充0](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-can-i-pad-an-integers-with-zeros-on-the-left.md)
* [在调用 instanceof 前需要进行null检查吗](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/is-null-check-needed-before-calling-instanceof.md)
+* [如何从文件里读取字符串](/contents/how-do-i-create-a-java-string-from-the-contents-of-a-file.md)
> 网络
@@ -119,7 +120,6 @@ stackoverflow-Java-top-qa
- [How to generate a random alpha-numeric string?](http://stackoverflow.com/questions/41107/how-to-generate-a-random-alpha-numeric-string)
- [Comparing Java enum members: == or equals()?](http://stackoverflow.com/questions/1750435/comparing-java-enum-members-or-equals)
- [Failed to load the JNI shared Library (JDK)](http://stackoverflow.com/questions/7352493/failed-to-load-the-jni-shared-library-jdk)
-- [How to create a Java String from the contents of a file?](http://stackoverflow.com/questions/326390/how-to-create-a-java-string-from-the-contents-of-a-file)
- [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)
- [What exactly is Apache Camel?](http://stackoverflow.com/questions/8845186/what-exactly-is-apache-camel)
- [How can I permanently have line numbers in IntelliJ?](http://stackoverflow.com/questions/13751/how-can-i-permanently-have-line-numbers-in-intellij)
From 679cce1ed046af0585961f8d08d597d84f7bbd06 Mon Sep 17 00:00:00 2001
From: giantray <51961070@qq.com>
Date: 2016年8月17日 09:47:16 +0800
Subject: [PATCH 129/195] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=B7=B2=E7=BF=BB?=
=?UTF-8?q?=E8=AF=91=E9=97=AE=E9=A2=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/README.md b/README.md
index 1339808..bfc5d1b 100644
--- a/README.md
+++ b/README.md
@@ -62,6 +62,7 @@ stackoverflow-Java-top-qa
* [servlets的运行原理](/contents/how-do-servlets-work-instantiation-shared-variables-and-multithreading.md)
* [如何计算MD5值](/contents/how-can-i-generate-an-md5-hash.md)
* [Java中软引用和弱引用的区别](/contents/what-is-the-difference-between-a-soft-reference-and-a-weak-reference-in-java.md)
+* [JSF, Servlet 和 JSP (三种技术)有什么区别](/contents/what-is-the-difference-between-jsf-servlet-and-jsp.md)
> 编程技巧
@@ -142,7 +143,6 @@ stackoverflow-Java-top-qa
- [Why does Math.round(0.49999999999999994) return 1](http://stackoverflow.com/questions/9902968/why-does-math-round0-49999999999999994-return-1)
- [Eclipse: Set maximum line length for auto formatting?](http://stackoverflow.com/questions/3697287/eclipse-set-maximum-line-length-for-auto-formatting)
- [What is the equivalent of the C++ Pair in Java?](http://stackoverflow.com/questions/156275/what-is-the-equivalent-of-the-c-pairl-r-in-java)
-- [What is the difference between JSF, Servlet and JSP?](http://stackoverflow.com/questions/2095397/what-is-the-difference-between-jsf-servlet-and-jsp)
- [How do I "decompile" Java class files?](http://stackoverflow.com/questions/272535/how-do-i-decompile-java-class-files)
- [Useful Eclipse Java Code Templates [closed]](http://stackoverflow.com/questions/1028858/useful-eclipse-java-code-templates)
- [How to call SOAP web service in Android](http://stackoverflow.com/questions/297586/how-to-call-soap-web-service-in-android)
From 1d7bcfa46010d7470957639bf0be72039b47160d Mon Sep 17 00:00:00 2001
From: giantray <51961070@qq.com>
Date: 2016年8月17日 10:06:34 +0800
Subject: [PATCH 130/195] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=90=90=E6=A7=BD?=
=?UTF-8?q?=E6=96=B9=E5=BC=8F?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 12 ++++++++----
1 file changed, 8 insertions(+), 4 deletions(-)
diff --git a/README.md b/README.md
index bfc5d1b..93bb18c 100644
--- a/README.md
+++ b/README.md
@@ -10,20 +10,24 @@ stackoverflow-Java-top-qa
-------------
### 如何参与翻译(欢迎加入翻译组QQ群485011036)
-如何参与:
-- 请从下文"待翻译问题链接"中寻找你感兴趣的问答进行翻译。翻译好的问答,放到contents目录下,无需更新readme.md文档。之后提一个PR,我会对你的翻译做一个审校,并更新到readme中。
+####如何参与:
+- 请从下文"待翻译问题链接"中寻找你感兴趣的问答进行翻译。翻译好的问答,放到contents目录下,无需更新readme.md文档。之后提一个PR,我负责合并PR并更新到readme中。
- 另外,为了避免多人重复新翻译一个问题,你可以提issue,说明你计划翻译的问题及时间点,我可以先更新到下面的"未翻译问题"中,说明已有人领了这个问题。当然,也不一定要提issue,一般情况下,只要及时提pr,我及时审核,出现"撞车"的概率并不高。
-一些基本的约定:
+####一些基本的约定:
- 文档的文件名,和stackoverflowhich-notnull-java-annotation-should-i-usew上的url保持一致。例如,http://stackoverflow.com/questions/8710619/java-operator 的文件名, 就是java-operator.md
- 在每篇翻译文档内容的最后,要附上stackoverflow的原文链接
-每个人可以做(但不限于):
+####每个人可以做(但不限于):
- 找未翻译的问题进行翻译
- 优化已翻译的问题
- 输出问答精简汇总版(把所有的问答集中到一个md文件,然后尽量精简,让别人可以在一天内把这100个问题的精髓都看完)
- 输出gitbook版本(现在直接在github上查看,体验不好)
+####文档优化反馈:
+请大家多多反馈,优化已翻译好的文章:可以到[吐槽区](https://github.com/giantray/stackoverflow-java-top-qa/issues/66)吐槽,也可以在已翻译文章基础上进行优化,提新的PR。文章质量的提升,需要大家一起努力!
+
+
### 目录
> 基础语法
From 8cc8fe4a14383c7de91ab7bde7897c5d9656b892 Mon Sep 17 00:00:00 2001
From: Troy Liu Date: 2016年8月17日 10:48:00 +0800
Subject: [PATCH 131/195] ADD: java-inner-class-and-static-nested-class.md
---
...ava-inner-class-and-static-nested-class.md | 44 +++++++++++++++++++
1 file changed, 44 insertions(+)
create mode 100644 contents/java-inner-class-and-static-nested-class.md
diff --git a/contents/java-inner-class-and-static-nested-class.md b/contents/java-inner-class-and-static-nested-class.md
new file mode 100644
index 0000000..e6d08c2
--- /dev/null
+++ b/contents/java-inner-class-and-static-nested-class.md
@@ -0,0 +1,44 @@
+## Java内部类和嵌套静态类
+
+### 问题
+Java 当中的内部类和静态嵌套类有什么主要区别? 在这两者中有什么设计或者实现么?
+
+### 回答
+嵌套类分为两类: 静态和非静态. 用`static`装饰的嵌套类叫做静态类, 非静态的嵌套类叫做内部类.
+
+静态嵌套类使用外围类名来访问:
+```java
+OuterClass.StaticNestedClass
+```
+例如, 实例化一个静态嵌套类的对象就要使用这种语法:
+```java
+OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();
+```
+
+内部类对象的存在需要依靠一个外部类的对象. 看看下面的类:
+```java
+class OuterClass {
+ ...
+ class InnerClass {
+ ...
+ }
+}
+```
+内部类对象只有当外部类对象存在时才有效, 并且可以直接访问他的包裹对象(外部类对象)的方法以及成员.
+
+因此, 要实例化一个内部类对象, 必须先实例化外部类对象. 然后用这种语法来创建内部类对象:
+```java
+OuterClass.InnerClass innerObject = outerObject.new InnerClass();
+```
+参考: [Java Tutorial - Nested Classes](http://download.oracle.com/javase/tutorial/java/javaOO/nested.html)
+
+提醒一下, 还有一种不用外部类对象来创建内部类对象的方法: [inner class without an enclosing ](http://stackoverflow.com/questions/20468856/is-it-true-that-every-inner-class-requires-an-enclosing-instance)
+```java
+class A {
+ int t() { return 1; }
+ static A a = new A() { int t() { return 2; } };
+}
+```
+在这里, `new A() { ... }`是一个定义在静态上下文的内部类对象, 并没有一个外围对象.
+
+stackoverflow链接: [Java inner class and static nested class](http://stackoverflow.com/questions/70324/java-inner-class-and-static-nested-class)
From 63083bfc942599b665479d5260bf93bbe68d1eee Mon Sep 17 00:00:00 2001
From: yu
Date: 2016年8月17日 15:21:06 +0800
Subject: [PATCH 132/195] owen1190
---
contents/comparing-java-enum-members-or-equals.md | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/contents/comparing-java-enum-members-or-equals.md b/contents/comparing-java-enum-members-or-equals.md
index 867246c..a83be17 100644
--- a/contents/comparing-java-enum-members-or-equals.md
+++ b/contents/comparing-java-enum-members-or-equals.md
@@ -44,10 +44,10 @@ public final boolean equals(Object other) {
答案是肯定的,因为枚举有着严格的实例化控制,所以你可以用 == 去做比较符,这个用法,在官方文档中也有明确的说明。
>JLS 8.9 Enums
->An enum type has no instances other than those defined by its enum constants.
->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.
->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.)
+一个枚举类型除了定义的那些枚举常量外没有其他实例了。
+试图明确地说明一种枚举类型是会导致编译期异常。在枚举中final clone方法确保枚举常量从不会被克隆,而且序列化机制会确保从不会因为反序列化而创造复制的实例。枚举类型的反射实例化也是被禁止的。总之,以上内容确保了除了定义的枚举常量之外,没有枚举类型实例。
+因为每个枚举常量只有一个实例,所以如果在比较两个参考值,至少有一个涉及到枚举常量时,允许使用"=="代替equals()。(equals()方法在枚举类中是一个final方法,在参数和返回结果时,很少调用父类的equals()方法,因此是一种恒等的比较。)
#### 什么时候 == 和 equals 不一样?
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:
通常来说 == 不是一个 equals的一个备选方案,无论如何有2个重要的不同处需要考虑:
@@ -73,8 +73,8 @@ if (Color.BLACK == Chiral.LEFT); // DOESN'T COMPILE!!! Incompatible types!
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.
具体来说,那些提供恰当实例控制的不可变类能够保证 == 是可用的,枚举刚好符合这个条件。
-> Item 1: Consider static factory methods instead of constructors
-> [...] 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.
+考虑静态工厂方法代替构造器
+它使得不可变的类可以确保不会存在两个相等的实例,即当且仅当a==b的时候才有a.equals(b)为true。如果类保证了这一点,它的客户端可以使用"=="操作符来代替equals(Object)方法,这样可以提升性能。枚举类型保证了这一点
总而言之,在枚举比较上使用 == , 因为:
1. 能正常工作
From 9e9aa6e687a75cc19b1505f3bc02f571229422dd Mon Sep 17 00:00:00 2001
From: AcceptedBoy <972192420@qq.com>
Date: 2016年8月17日 17:34:41 +0800
Subject: [PATCH 133/195]
add->whats-the-difference-between-component-repository-service-annotations-in.md
---
...onent-repository-service-annotations-in.md | 77 +++++++++++++++++++
1 file changed, 77 insertions(+)
create mode 100644 contents/whats-the-difference-between-component-repository-service-annotations-in.md
diff --git a/contents/whats-the-difference-between-component-repository-service-annotations-in.md b/contents/whats-the-difference-between-component-repository-service-annotations-in.md
new file mode 100644
index 0000000..be6945a
--- /dev/null
+++ b/contents/whats-the-difference-between-component-repository-service-annotations-in.md
@@ -0,0 +1,77 @@
+## @Component, @Repository, @Service的区别
+
+#### 问题
+
+在spring集成的框架中,注解在类上的`@Component`,`@Repository`,`@Service`等注解能否被互换?即这些注解的区别是什么?
+
+#### 回答1
+
+引用spring的官方文档中的一段描述:
+
+在Spring2.0之前的版本中,`@Repository`注解可以标记在任何的类上,用来表明该类是用来执行与数据库相关的操作(即dao对象)
+
+在Spring2.5版本中,引入了更多的Spring类注解:`@Component`,`@Service`,`@Controller`。`Component`是一个通用的Spring容器管理的单例bean组件。而`@Repository`, `@Service`, `@Controller`就是针对不同的使用场景所采取的特定功能化的注解组件。
+
+因此,当你的一个类被`@Component`所注解,那么就意味着同样可以用`@Repository`, `@Service`, `@Controller`来替代它,同时这些注解会具备有更多的功能,而且功能各异。
+
+最后,如果你不知道要在项目的业务层采用`@Service`还是`@Component`注解。那么,`@Service`是一个更好的选择。
+
+就如上文所说的,`@Repository`早已被支持了在你的持久层作为一个标记可以去自动处理数据库操作产生的异常(译者注:因为原生的java操作数据库所产生的异常只用了几种,但是产生的原因却有很多种,这样对于数据库操作的报错排查造成了一定的影响;而Spring拓展了原生的持久层异常,针对不同的产生原因有了更多的异常进行描述。所以,在注解了`@Repository`的类上如果数据库操作中抛出了异常,就能对其进行处理,转而抛出的是翻译后的spring专属数据库异常)。
+
+| 注解 | 含义 |
+| ------------- |:-------------:|
+| @Component | 最普通的组件,可以被注入到spring容器进行管理 |
+| @Repository | 作用于持久层 |
+| @Service | 作用于业务逻辑层 |
+| @Controller | 作用于表现层(spring-mvc的注解) |
+
+#### 回答2
+
+这几个注解几乎可以说是一样的:因为被这些注解修饰的类就会被Spring扫描到并注入到Spring的bean容器中。
+
+这里,有两个注解是不能被其他注解所互换的:
+
+* `@Controller` 注解的bean会被spring-mvc框架所使用。
+* `@Repository` 会被作为持久层操作(数据库)的bean来使用
+
+如果想使用自定义的组件注解,那么只要在你定义的新注解中加上`@Component`即可:
+
+```java
+@Component
+@Scope("prototype")
+public @interface ScheduleJob {...}
+```
+
+这样,所有被`@ScheduleJob`注解的类就都可以注入到spring容器来进行管理。我们所需要做的,就是写一些新的代码来处理这个自定义注解(译者注:可以用反射的方法),进而执行我们想要执行的工作。
+
+#### 回答3
+
+`@Component`就是跟``一样,可以托管到Spring容器进行管理。
+
+@Service, @Controller , @Repository = {@Component + 一些特定的功能}。这个就意味着这些注解在部分功能上是一样的。
+
+当然,下面三个注解被用于为我们的应用进行分层:
+
+* `@Controller`注解类进行前端请求的处理,转发,重定向。包括调用Service层的方法
+* `@Service`注解类处理业务逻辑
+* `@Repository`注解类作为DAO对象(数据访问对象,Data Access Objects),这些类可以直接对数据库进行操作
+
+有这些分层操作的话,代码之间就实现了松耦合,代码之间的调用也清晰明朗,便于项目的管理;假想一下,如果只用`@Controller`注解,那么所有的请求转发,业务处理,数据库操作代码都糅合在一个地方,那这样的代码该有多难拓展和维护。
+
+#### 总结
+
+* `@Component`, `@Service`, `@Controller`, `@Repository`是spring注解,注解后可以被spring框架所扫描并注入到spring容器来进行管理
+* `@Component`是通用注解,其他三个注解是这个注解的拓展,并且具有了特定的功能
+* `@Repository`注解在持久层中,具有将数据库操作抛出的原生异常翻译转化为spring的持久层异常的功能。
+* `@Controller`层是spring-mvc的注解,具有将请求进行转发,重定向的功能。
+* `@Service`层是业务逻辑层注解,这个注解只是标注该类处于业务逻辑层。
+* 用这些注解对应用进行分层之后,就能将请求处理,义务逻辑处理,数据库操作处理分离出来,为代码解耦,也方便了以后项目的维护和开发。
+
+#### Stackoverflow链接:
+
+[http://stackoverflow.com/questions/6827752/whats-the-difference-between-component-repository-service-annotations-in](http://stackoverflow.com/questions/6827752/whats-the-difference-between-component-repository-service-annotations-in)
+
+#### 拓展
+
+1. [Spring注解@Component、@Repository、@Service、@Controller区别](http://www.cnblogs.com/JAYIT/p/5593169.html)
+2. [Spring注解@Autowired、@Resource区别](http://www.cnblogs.com/leiOOlei/p/3713779.html)
\ No newline at end of file
From 13fd9c69a9c9e5d71716c77d545265f470618014 Mon Sep 17 00:00:00 2001
From: giantray <51961070@qq.com>
Date: 2016年8月17日 22:22:01 +0800
Subject: [PATCH 134/195] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=B7=B2=E7=BF=BB?=
=?UTF-8?q?=E8=AF=91=E9=97=AE=E9=A2=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/README.md b/README.md
index 93bb18c..6688723 100644
--- a/README.md
+++ b/README.md
@@ -67,6 +67,7 @@ stackoverflow-Java-top-qa
* [如何计算MD5值](/contents/how-can-i-generate-an-md5-hash.md)
* [Java中软引用和弱引用的区别](/contents/what-is-the-difference-between-a-soft-reference-and-a-weak-reference-in-java.md)
* [JSF, Servlet 和 JSP (三种技术)有什么区别](/contents/what-is-the-difference-between-jsf-servlet-and-jsp.md)
+* [Java内部类和嵌套静态类](/contents/java-inner-class-and-static-nested-class.md)
> 编程技巧
@@ -118,7 +119,6 @@ stackoverflow-Java-top-qa
- [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)
- [Convert a String to an enum in Java](http://stackoverflow.com/questions/604424/convert-a-string-to-an-enum-in-java)
- [Android SDK installation doesn't find JDK](http://stackoverflow.com/questions/4382178/android-sdk-installation-doesnt-find-jdk)
-- [Java inner class and static nested class](http://stackoverflow.com/questions/70324/java-inner-class-and-static-nested-class)
- ['Must Override a Superclass Method' Errors after importing a project into Eclipse](http://stackoverflow.com/questions/1678122/must-override-a-superclass-method-errors-after-importing-a-project-into-eclips)
- [Fastest way to determine if an integer's square root is an integer](http://stackoverflow.com/questions/295579/fastest-way-to-determine-if-an-integers-square-root-is-an-integer)
- [How to fix: Unsupported major.minor version 51.0 error?](http://stackoverflow.com/questions/10382929/how-to-fix-unsupported-major-minor-version-51-0-error)
From 8fffae44b2b507d42c67510980c4e8c7be3cc980 Mon Sep 17 00:00:00 2001
From: giantray <51961070@qq.com>
Date: 2016年8月17日 22:28:29 +0800
Subject: [PATCH 135/195] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=B7=B2=E7=BF=BB?=
=?UTF-8?q?=E8=AF=91=E9=97=AE=E9=A2=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/README.md b/README.md
index 6688723..defedb3 100644
--- a/README.md
+++ b/README.md
@@ -68,6 +68,7 @@ stackoverflow-Java-top-qa
* [Java中软引用和弱引用的区别](/contents/what-is-the-difference-between-a-soft-reference-and-a-weak-reference-in-java.md)
* [JSF, Servlet 和 JSP (三种技术)有什么区别](/contents/what-is-the-difference-between-jsf-servlet-and-jsp.md)
* [Java内部类和嵌套静态类](/contents/java-inner-class-and-static-nested-class.md)
+* [@Component, @Repository, @Service的区别](/contents/whats-the-difference-between-component-repository-service-annotations-in.md)
> 编程技巧
@@ -139,7 +140,6 @@ stackoverflow-Java-top-qa
- [Hibernate hbm2ddl.auto possible values and what they do?](http://stackoverflow.com/questions/438146/hibernate-hbm2ddl-auto-possible-values-and-what-they-do)
- [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)
- [Sort ArrayList of custom Objects by property](http://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property)
-- [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)
- [Efficiency of Java "Double Brace Initialization"?](http://stackoverflow.com/questions/924285/efficiency-of-java-double-brace-initialization)
- [Why does this go into an infinite loop?](http://stackoverflow.com/questions/3831341/why-does-this-go-into-an-infinite-loop)
- [decompiling DEX into Java sourcecode](http://stackoverflow.com/questions/1249973/decompiling-dex-into-java-sourcecode)
From 85619235f92d2b2d14012c92c2e0ba01bb972d8d Mon Sep 17 00:00:00 2001
From: AcceptedBoy <972192420@qq.com>
Date: 2016年8月17日 23:24:57 +0800
Subject: [PATCH 136/195]
add->iterating-through-a-collection-avoiding-concurrentmodificationexception-when-re.md
---
...concurrentmodificationexception-when-re.md | 69 +++++++++++++++++++
1 file changed, 69 insertions(+)
create mode 100644 contents/iterating-through-a-collection-avoiding-concurrentmodificationexception-when-re.md
diff --git a/contents/iterating-through-a-collection-avoiding-concurrentmodificationexception-when-re.md b/contents/iterating-through-a-collection-avoiding-concurrentmodificationexception-when-re.md
new file mode 100644
index 0000000..47195c4
--- /dev/null
+++ b/contents/iterating-through-a-collection-avoiding-concurrentmodificationexception-when-re.md
@@ -0,0 +1,69 @@
+## 遍历集合时移除元素,怎样避免ConcurrentModificationException异常抛出
+
+#### 问题:
+
+在遍历集合的过程中,不会总出现`ConcurrentModificationException`异常的抛出,但是在下面的代码块中:
+
+```java
+public static void main(String[] args) {
+ Collection l = new ArrayList();
+
+ for (int i=0; i < 10; ++i) { + l.add(new Integer(4)); + l.add(new Integer(5)); + l.add(new Integer(6)); + } + + //遍历的过程中移除部分集合元素 + for (Integer i : l) { + if (i.intValue() == 5) { + l.remove(i); + } + } + + System.out.println(l); +} +``` + +运行之后,结果显而易见,总是会抛出异常: + +```java +Exception in thread "main" java.util.ConcurrentModificationException +``` + +所以,遍历集合时移除元素,怎样避免ConcurrentModificationException异常的产生?有什么好的解决办法? + +#### 回答: + +`Iterator.remove()`是线程安全的,所以你的代码可以这样写: + +```java +List list = new ArrayList();
+
+for (Iterator iterator = list.iterator(); iterator.hasNext();) {
+ String string = iterator.next();
+ if (string.isEmpty()) {
+
+ // 从迭代器中移除集合元素,集合中相应的集合元素也会安全地被移除
+ // 在这里,如果继续调用的是list.remove(string),那么仍会抛出异常
+ iterator.remove();
+ }
+}
+```
+
+在遍历集合时修改集合的结构或内容的情况中,`Iterator.remove()`是唯一线程安全的方法。
+
+#### 问题原因:
+
+fail-fast, 快速失败机制,是java集合类的一种错误检查机制。当有多个线程同时对集合进行遍历以及内容或者结构的修改时,就有可能产生fail-fast机制。这意味着,当它们发现容器在迭代的过程中被修改时,就会抛出一个ConcurrentModificationException异常。
+
+迭代器的快速失败行为无法得到保证,它不能保证一定会出现该错误,但是快速失败操作会尽最大努力抛出ConcurrentModificationException异常,这个异常仅用于检测bug。这种迭代器并不是完备的处理机制,而只是作为并发问题的一个预警指示器。
+
+
+#### 拓展阅读:
+
+[fail-fast机制的原理解析](https://github.com/AcceptedBoy/backstage-vacation-plan/blob/master/chapter1/concurrency/fail-fast.md)
+
+#### StackOverFlow地址:
+
+[http://stackoverflow.com/questions/223918/iterating-through-a-collection-avoiding-concurrentmodificationexception-when-re](http://stackoverflow.com/questions/223918/iterating-through-a-collection-avoiding-concurrentmodificationexception-when-re)
\ No newline at end of file
From 5ebf82154ce017de5d4cfd114acb97a1ecc9d930 Mon Sep 17 00:00:00 2001
From: giantray <51961070@qq.com>
Date: 2016年8月18日 08:57:56 +0800
Subject: [PATCH 137/195] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=B7=B2=E7=BF=BB?=
=?UTF-8?q?=E8=AF=91=E9=97=AE=E9=A2=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/README.md b/README.md
index defedb3..807429a 100644
--- a/README.md
+++ b/README.md
@@ -91,6 +91,7 @@ stackoverflow-Java-top-qa
* [如何在整数左填充0](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-can-i-pad-an-integers-with-zeros-on-the-left.md)
* [在调用 instanceof 前需要进行null检查吗](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/is-null-check-needed-before-calling-instanceof.md)
* [如何从文件里读取字符串](/contents/how-do-i-create-a-java-string-from-the-contents-of-a-file.md)
+* [遍历集合时移除元素,怎样避免ConcurrentModificationException异常抛出](/contents/iterating-through-a-collection-avoiding-concurrentmodificationexception-when-reiterating-through-a-collection-avoiding-concurrentmodificationexception-when-re.md)
> 网络
@@ -135,7 +136,6 @@ stackoverflow-Java-top-qa
- [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)
- [Update Eclipse with Android development tools v. 23](http://stackoverflow.com/questions/24437564/update-eclipse-with-android-development-tools-v-23)
- [Setting multiple jars in java classpath](http://stackoverflow.com/questions/219585/setting-multiple-jars-in-java-classpath)
-- [Iterating through a list, avoiding ConcurrentModificationException when removing in loop](http://stackoverflow.com/questions/223918/iterating-through-a-list-avoiding-concurrentmodificationexception-when-removing)
- [What is the Java equivalent for LINQ?](http://stackoverflow.com/questions/1217228/what-is-the-java-equivalent-for-linq)
- [Hibernate hbm2ddl.auto possible values and what they do?](http://stackoverflow.com/questions/438146/hibernate-hbm2ddl-auto-possible-values-and-what-they-do)
- [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)
From 62805417211533ebb9a27b306853264151f84800 Mon Sep 17 00:00:00 2001
From: AcceptedBoy <972192420@qq.com>
Date: 2016年8月18日 10:57:34 +0800
Subject: [PATCH 138/195]
add->how-do-you-assert-that-a-certain-exception-is-thrown-in-junit-4-tests.md
---
...in-exception-is-thrown-in-junit-4-tests.md | 63 +++++++++++++++++++
1 file changed, 63 insertions(+)
create mode 100644 contents/how-do-you-assert-that-a-certain-exception-is-thrown-in-junit-4-tests.md
diff --git a/contents/how-do-you-assert-that-a-certain-exception-is-thrown-in-junit-4-tests.md b/contents/how-do-you-assert-that-a-certain-exception-is-thrown-in-junit-4-tests.md
new file mode 100644
index 0000000..84ca09e
--- /dev/null
+++ b/contents/how-do-you-assert-that-a-certain-exception-is-thrown-in-junit-4-tests.md
@@ -0,0 +1,63 @@
+## JUnit如何断言一个确定的异常抛出
+
+#### 问题
+
+在JUnit4单元测试中,我要怎样做才能测试出有特定的异常抛出?我能想到的就只有下面的方法:
+
+```java
+@Test
+public void testFooThrowsIndexOutOfBoundsException() {
+ boolean thrown = false;
+
+ try {
+ foo.doStuff();
+ } catch (IndexOutOfBoundsException e) {
+ thrown = true;
+ }
+
+ assertTrue(thrown);
+}
+```
+
+#### 回答1
+
+在JUnit4后支持下面的写法:
+```java
+@Test(expected=IndexOutOfBoundsException.class)
+public void testIndexOutOfBoundsException() {
+ ArrayList emptyList = new ArrayList();
+ Object o = emptyList.get(0);
+}
+```
+(译者:在`@Test`注解内提供了`expected`属性,你可以用它来指定一个`Throwble`类型,如果方法调用中抛出了这个异常,那么这条测试用例就相当于通过了)
+
+#### 回答2
+
+如果你使用的是JUnit4.7,你可以使用如下的期望异常规则来验证异常信息:
+
+```java
+public class FooTest {
+ @Rule
+ public final ExpectedException exception = ExpectedException.none();
+
+ @Test
+ public void doStuffThrowsIndexOutOfBoundsException() {
+ Foo foo = new Foo();
+
+ exception.expect(IndexOutOfBoundsException.class);
+ foo.doStuff();
+ }
+}
+```
+
+这种方式比`@Test(expected=IndexOutOfBoundsException.class)`要更好,如果是在调用`foo.doStuff()`方法之前就已经抛出异常的话,测试结果就不是我们想要的了。
+(译者:同时,`ExpectedException`还能够验证异常信息,如`exception.expectMessage("there is an exception!");`
+
+#### 拓展阅读
+
+1. [JUnit:使用ExpectedException进行异常测试](http://www.tuicool.com/articles/ANviIz)
+2. [JUnit4 用法详解](http://www.blogjava.net/jnbzwm/archive/2010/12/15/340801.html)
+
+#### StackOverflow地址:
+
+[http://stackoverflow.com/questions/156503/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)
\ No newline at end of file
From 01ca9246a07be21901ba15ab4ceb7f2bbedb5102 Mon Sep 17 00:00:00 2001
From: AcceptedBoy <972192420@qq.com>
Date: 2016年8月18日 11:39:57 +0800
Subject: [PATCH 139/195] update->junit4 && add->create_generic_array
---
...in-exception-is-thrown-in-junit-4-tests.md | 2 +-
.../how-to-create-a-generic-array-in-java.md | 76 +++++++++++++++++++
2 files changed, 77 insertions(+), 1 deletion(-)
create mode 100644 contents/how-to-create-a-generic-array-in-java.md
diff --git a/contents/how-do-you-assert-that-a-certain-exception-is-thrown-in-junit-4-tests.md b/contents/how-do-you-assert-that-a-certain-exception-is-thrown-in-junit-4-tests.md
index 84ca09e..6c5cbdf 100644
--- a/contents/how-do-you-assert-that-a-certain-exception-is-thrown-in-junit-4-tests.md
+++ b/contents/how-do-you-assert-that-a-certain-exception-is-thrown-in-junit-4-tests.md
@@ -1,4 +1,4 @@
-## JUnit如何断言一个确定的异常抛出
+## JUnit4如何断言确定异常的抛出
#### 问题
diff --git a/contents/how-to-create-a-generic-array-in-java.md b/contents/how-to-create-a-generic-array-in-java.md
new file mode 100644
index 0000000..d1d7041
--- /dev/null
+++ b/contents/how-to-create-a-generic-array-in-java.md
@@ -0,0 +1,76 @@
+## 如何创建泛型java数组
+
+#### 问题
+
+数组是不能通过泛型创建的,因为我们不能创建不可具体化的类型的数组。如下面的代码:
+
+```java
+public class GenSet {
+ private E a[];
+
+ public GenSet() {
+ a = new E[INITIAL_ARRAY_LENGTH]; //编译期就会报错:不能创建泛型数组
+ }
+}
+```
+
+#### 采纳答案
+
+* 检查:强类型。`GenSet`明确知道数组中包含的类型是什么(例如通过构造器传入`Class`,当方法中传入类型不是`E`将抛出异常)
+
+```java
+public class GenSet {
+
+ private E[] a;
+
+ public GenSet(Class c, int s) {
+ // 使用原生的反射方法,在运行时知道其数组对象类型
+ @SuppressWarnings("unchecked")
+ final E[] a = (E[]) Array.newInstance(c, s);
+ this.a = a;
+ }
+
+ E get(int i) {
+ return a[i];
+ }
+
+ //...如果传入参数不为E类型,那么强制添加进数组将会抛出异常
+ void add(E e) {...}
+}
+```
+
+* 未检查:弱类型。数组内对象不会有任何类型检查,而是作为Object类型传入。
+
+在这种情况下,你可以采取如下写法:
+
+```java
+public class GenSet {
+
+ private Object[] a;
+
+ public GenSet(int s) {
+ a = new Object[s];
+ }
+
+ E get(int i) {
+ @SuppressWarnings("unchecked")
+ final E e = (E) a[i];
+ return e;
+ }
+}
+```
+
+上述代码在编译期能够通过,但因为泛型擦除的缘故,在程序执行过程中,数组的类型有且仅有`Object`类型存在,这个时候如果我们强制转化为`E`类型的话,在运行时会有`ClassCastException`抛出。所以,要确定好泛型的上界,将上边的代码重写一下:
+
+```java
+public class GenSet { // E has an upper bound of Foo
+
+ private Foo[] a; // E 泛型在运行期会被擦除为Foo类型,所以这里使用Foo[]
+
+ public GenSet(int s) {
+ a = new Foo[s];
+ }
+
+ //...
+}
+```
\ No newline at end of file
From 6aaae682e3f0a46cbbf04ea8f7af2edef0b333b1 Mon Sep 17 00:00:00 2001
From: AcceptedBoy <972192420@qq.com>
Date: 2016年8月18日 11:42:02 +0800
Subject: [PATCH 140/195] add_stackoverflow_address->create_generic_array
---
contents/how-to-create-a-generic-array-in-java.md | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/contents/how-to-create-a-generic-array-in-java.md b/contents/how-to-create-a-generic-array-in-java.md
index d1d7041..086f0d2 100644
--- a/contents/how-to-create-a-generic-array-in-java.md
+++ b/contents/how-to-create-a-generic-array-in-java.md
@@ -73,4 +73,8 @@ public class GenSet { // E has an upper bound of Foo
//...
}
-```
\ No newline at end of file
+```
+
+#### StackOverflow地址:
+
+[http://stackoverflow.com/questions/529085/how-to-create-a-generic-array-in-java](http://stackoverflow.com/questions/529085/how-to-create-a-generic-array-in-java)
\ No newline at end of file
From 86bf2258cbff6c89ac2f05c904e885fb3e2d8d86 Mon Sep 17 00:00:00 2001
From: AcceptedBoy <972192420@qq.com>
Date: 2016年8月18日 18:57:37 +0800
Subject: [PATCH 141/195]
add->how-can-i-create-an-executable-jar-with-dependencies-using-maven.md
---
...table-jar-with-dependencies-using-maven.md | 99 +++++++++++++++++++
1 file changed, 99 insertions(+)
create mode 100644 contents/how-can-i-create-an-executable-jar-with-dependencies-using-maven.md
diff --git a/contents/how-can-i-create-an-executable-jar-with-dependencies-using-maven.md b/contents/how-can-i-create-an-executable-jar-with-dependencies-using-maven.md
new file mode 100644
index 0000000..dab0dcf
--- /dev/null
+++ b/contents/how-can-i-create-an-executable-jar-with-dependencies-using-maven.md
@@ -0,0 +1,99 @@
+## 如何使用maven把项目及其依赖打包为可运行jar包
+
+#### 问题
+
+我想把java项目打包为可运行的分布式jar包。我该怎样做,才能把项目中maven所依赖的jar包导入到我的项目jar包中?
+
+#### 回答
+
+在`pom.xml`文件中,加入如下的插件:
+
+```xml
+
+
+
+ maven-assembly-plugin
+
+
+
+
+ fully.qualified.MainClass
+
+
+
+ jar-with-dependencies
+
+
+
+
+
+```
+
+之后,运行maven命令:
+
+> mvn clean compile assembly:single
+
+`clean`,`compile`,`assembly:single`任务将会依次被执行;`compile`任务必须写在`assembly:single`之前,否则打包后的jar包内将不会有你的编译代码。
+
+(译注:执行完后,会在你的maven项目的target目录下,生成想要的jar包,而不再需要使用`mvn package`命令进行打包)
+
+通常情况下,上述maven命令执行后会自动绑定到项目的构建阶段,从而保证了以后在执行`mvn install`命令时的jar包也会被构建。
+(译注:下面是实际上完整的默认的`pom.xml`配置,只不过``可以被省略,若省略则按照下述默认的配置执行)
+
+```xml
+
+ maven-assembly-plugin
+
+
+
+ fully.qualified.MainClass
+
+
+
+ jar-with-dependencies
+
+
+
+
+ make-assembly
+ package
+
+ single
+
+
+
+
+```
+
+#### 拓展
+
+怎样去运行打包后的可运行jar包?
+
+* 对上述配置中已经指定了`main`函数所在类的jar包,打开命令行窗口,输入命令:
+
+```java
+java -jar jar包的路径/jar包的名字.jar
+```
+
+例如:
+
+```Auto
+java -jar D:\my_java_project\maven_test.jar
+```
+
+* 若在pom.xml并没有指定`main`方法所在类,那么该jar的运行应采取如下命令:
+
+```java
+java -cp jar包的路径/jar包的名字.jar main方法所在类的全限定名
+```
+
+例如:
+
+```java
+java -cp D:\my_java_project\maven_test.jar com.my.path.MainClass
+```
+
+
+#### StackOverflow地址
+
+[http://stackoverflow.com/questions/574594/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)
\ No newline at end of file
From 46310586da02ee4ce8e924b70db6d0d23bb22caa Mon Sep 17 00:00:00 2001
From: AcceptedBoy <972192420@qq.com>
Date: 2016年8月18日 19:23:17 +0800
Subject: [PATCH 142/195] add->intellij && update->difference of spring
annotation
---
...rmanently-have-line-numbers-in-intellij.md | 29 +++++++++++++++++++
...onent-repository-service-annotations-in.md | 6 ++--
2 files changed, 32 insertions(+), 3 deletions(-)
create mode 100644 contents/how-can-i-permanently-have-line-numbers-in-intellij.md
diff --git a/contents/how-can-i-permanently-have-line-numbers-in-intellij.md b/contents/how-can-i-permanently-have-line-numbers-in-intellij.md
new file mode 100644
index 0000000..8f7e071
--- /dev/null
+++ b/contents/how-can-i-permanently-have-line-numbers-in-intellij.md
@@ -0,0 +1,29 @@
+## 如何让IntelliJ编辑器永久性显示代码行数
+
+#### 问题
+
+如何让IntelliJ编辑器永久性显示代码行数
+
+#### 回答
+
+##### IntelliJ 14.0之后的版本
+
+打开软件的菜单`File`->`Settings`->`Editor`->`General`->`Appearance`,在右侧的配置`Show line numbers`打勾:
+![image1][1]
+
+##### IntelliJ 8.1.2 - 13.X的版本
+
+打开软件的菜单`File`->`Settings`->`Editor`->`Appearance`,在右侧的配置`Show line numbers`打勾:
+![images2][2]
+
+#### 拓展
+
+[IntelliJ IDEA 使用教程](http://www.phperz.com/article/15/0923/159068.html)
+
+#### StackOverflow地址
+
+http://stackoverflow.com/questions/13751/how-can-i-permanently-have-line-numbers-in-intellij
+
+
+ [1]: http://i.stack.imgur.com/9DL9q.png
+ [2]: http://i.stack.imgur.com/JVZlJ.jpg
\ No newline at end of file
diff --git a/contents/whats-the-difference-between-component-repository-service-annotations-in.md b/contents/whats-the-difference-between-component-repository-service-annotations-in.md
index be6945a..0dd4196 100644
--- a/contents/whats-the-difference-between-component-repository-service-annotations-in.md
+++ b/contents/whats-the-difference-between-component-repository-service-annotations-in.md
@@ -2,13 +2,13 @@
#### 问题
-在spring集成的框架中,注解在类上的`@Component`,`@Repository`,`@Service`等注解能否被互换?即这些注解的区别是什么?
+在spring集成的框架中,注解在类上的`@Component`,`@Repository`,`@Service`等注解能否被互换?或者说这些注解有什么区别?
#### 回答1
引用spring的官方文档中的一段描述:
-在Spring2.0之前的版本中,`@Repository`注解可以标记在任何的类上,用来表明该类是用来执行与数据库相关的操作(即dao对象)
+在Spring2.0之前的版本中,`@Repository`注解可以标记在任何的类上,用来表明该类是用来执行与数据库相关的操作(即dao对象),并支持自动处理数据库操作产生的异常
在Spring2.5版本中,引入了更多的Spring类注解:`@Component`,`@Service`,`@Controller`。`Component`是一个通用的Spring容器管理的单例bean组件。而`@Repository`, `@Service`, `@Controller`就是针对不同的使用场景所采取的特定功能化的注解组件。
@@ -16,7 +16,7 @@
最后,如果你不知道要在项目的业务层采用`@Service`还是`@Component`注解。那么,`@Service`是一个更好的选择。
-就如上文所说的,`@Repository`早已被支持了在你的持久层作为一个标记可以去自动处理数据库操作产生的异常(译者注:因为原生的java操作数据库所产生的异常只用了几种,但是产生的原因却有很多种,这样对于数据库操作的报错排查造成了一定的影响;而Spring拓展了原生的持久层异常,针对不同的产生原因有了更多的异常进行描述。所以,在注解了`@Repository`的类上如果数据库操作中抛出了异常,就能对其进行处理,转而抛出的是翻译后的spring专属数据库异常)。
+就如上文所说的,`@Repository`早已被支持了在你的持久层作为一个标记可以去自动处理数据库操作产生的异常(译者注:因为原生的java操作数据库所产生的异常只定义了几种,但是产生数据库异常的原因却有很多种,这样对于数据库操作的报错排查造成了一定的影响;而Spring拓展了原生的持久层异常,针对不同的产生原因有了更多的异常进行描述。所以,在注解了`@Repository`的类上如果数据库操作中抛出了异常,就能对其进行处理,转而抛出的是翻译后的spring专属数据库异常,方便我们对异常进行排查处理)。
| 注解 | 含义 |
| ------------- |:-------------:|
From 1bac5e9d3b32b0ddecffbc2d958f26099f32bd39 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=BF=8E=E9=87=91=E5=A5=BD?=
Date: 2016年8月22日 18:23:37 +0800
Subject: [PATCH 143/195] android-sdk-installation-doesnt-find-jdk
---
...ndroid-sdk-installation-doesnt-find-jdk.md | 30 +++++++++++++++++++
1 file changed, 30 insertions(+)
create mode 100644 contents/android-sdk-installation-doesnt-find-jdk.md
diff --git a/contents/android-sdk-installation-doesnt-find-jdk.md b/contents/android-sdk-installation-doesnt-find-jdk.md
new file mode 100644
index 0000000..7b70797
--- /dev/null
+++ b/contents/android-sdk-installation-doesnt-find-jdk.md
@@ -0,0 +1,30 @@
+##安装Android SDK的时候找不到JDK
+
+###问题
+我在我的win7 64位的系统上安装Android SDK时,jdk-6u23-windows-x64.exe已经安装上了,但是Android SDK的安装程序却因为找不到已安装的JDK无法继续下去。
+这个问题出现过吗?有没有办法解决呢?
+
+
+
+###回答1:
+当你看到这个提示(找不到jdk)的时候按Back(返回),然后再点Next(下一步)。这个时候,它将会去寻找JDK
+
+###回答2:
+实际安装:
+
+ - 系统:windows 8.1
+ - JDK文件: jdk-8u11-windows-x64.exe
+ - ADT文件:installer_r23.0.2-windows.exe
+安装64位JDK,然后尝试第一个回答中的back-next的方法。然后尝试设置JAVA_HOME 根据错误信息的提示,但是,仍旧对我没有用处,然后,尝试如下解决办法:
+
+按照它说的做,设置JAVA_HOME在你的系统环境变量中,这个路径要使用正斜杠(/)而非反斜杠(\)
+
+**注意:**
+当我把JAVA_HOME设置为C:\Program Files\Java\jdk1.6.0_31的时候还是不行,但是当我设置成C:/Program Files/Java/jdk1.6.0_31的时候就ok了。快把我逼疯了。
+
+如果还不行,就把 %JAVA_HOME%加在环境变量Path的头部。
+
+下面是我的环境变量的配置:
+ - JAVA_HOME=C:/Program Files/Java/jdk1.8.0_11
+ - JRE_HOME=C:/Program Files/Java/jre8
+ - Path=%JAVA_HOME%;C:...
From bca7ea7d4d6d405a15a83cdfb4cd2d21051020fd Mon Sep 17 00:00:00 2001
From: giantray <51961070@qq.com>
Date: 2016年8月22日 23:33:56 +0800
Subject: [PATCH 144/195] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=B7=B2=E7=BF=BB?=
=?UTF-8?q?=E8=AF=91=E9=97=AE=E9=A2=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 9 ++++-----
1 file changed, 4 insertions(+), 5 deletions(-)
diff --git a/README.md b/README.md
index 807429a..443a313 100644
--- a/README.md
+++ b/README.md
@@ -69,6 +69,7 @@ stackoverflow-Java-top-qa
* [JSF, Servlet 和 JSP (三种技术)有什么区别](/contents/what-is-the-difference-between-jsf-servlet-and-jsp.md)
* [Java内部类和嵌套静态类](/contents/java-inner-class-and-static-nested-class.md)
* [@Component, @Repository, @Service的区别](/contents/whats-the-difference-between-component-repository-service-annotations-in.md)
+* [如何创建泛型java数组](/contents/how-to-create-a-generic-array-in-java.md)
> 编程技巧
@@ -92,7 +93,8 @@ stackoverflow-Java-top-qa
* [在调用 instanceof 前需要进行null检查吗](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/is-null-check-needed-before-calling-instanceof.md)
* [如何从文件里读取字符串](/contents/how-do-i-create-a-java-string-from-the-contents-of-a-file.md)
* [遍历集合时移除元素,怎样避免ConcurrentModificationException异常抛出](/contents/iterating-through-a-collection-avoiding-concurrentmodificationexception-when-reiterating-through-a-collection-avoiding-concurrentmodificationexception-when-re.md)
-
+* [如何让IntelliJ编辑器永久性显示代码行数](/contents/how-can-i-permanently-have-line-numbers-in-intellij.md)
+* [如何使用maven把项目及其依赖打包为可运行jar包](/contents/how-can-i-create-an-executable-jar-with-dependencies-using-maven.md)
> 网络
@@ -109,6 +111,7 @@ stackoverflow-Java-top-qa
> 测试
* [如何测试 private 方法,变量或者内部类](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-to-test-a-class-that-has-private-methods-fields-or-inner-classes.md)
+* [JUnit4如何断言确定异常的抛出](/contents/how-do-you-assert-that-a-certain-exception-is-thrown-in-junit-4-tests.md)
> Android
@@ -117,7 +120,6 @@ stackoverflow-Java-top-qa
### 待翻译问题链接(还剩x问题)
- [Proper use cases for Android UserManager.isUserAGoat()?](http://stackoverflow.com/questions/13375357/proper-use-cases-for-android-usermanager-isuseragoat)
-- [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)
- [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)
- [Convert a String to an enum in Java](http://stackoverflow.com/questions/604424/convert-a-string-to-an-enum-in-java)
- [Android SDK installation doesn't find JDK](http://stackoverflow.com/questions/4382178/android-sdk-installation-doesnt-find-jdk)
@@ -127,9 +129,7 @@ stackoverflow-Java-top-qa
- [How to generate a random alpha-numeric string?](http://stackoverflow.com/questions/41107/how-to-generate-a-random-alpha-numeric-string)
- [Comparing Java enum members: == or equals()?](http://stackoverflow.com/questions/1750435/comparing-java-enum-members-or-equals)
- [Failed to load the JNI shared Library (JDK)](http://stackoverflow.com/questions/7352493/failed-to-load-the-jni-shared-library-jdk)
-- [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)
- [What exactly is Apache Camel?](http://stackoverflow.com/questions/8845186/what-exactly-is-apache-camel)
-- [How can I permanently have line numbers in IntelliJ?](http://stackoverflow.com/questions/13751/how-can-i-permanently-have-line-numbers-in-intellij)
- [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)
- [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)
- [Uncatchable ChuckNorrisException](http://stackoverflow.com/questions/13883166/uncatchable-chucknorrisexception)
@@ -143,7 +143,6 @@ stackoverflow-Java-top-qa
- [Efficiency of Java "Double Brace Initialization"?](http://stackoverflow.com/questions/924285/efficiency-of-java-double-brace-initialization)
- [Why does this go into an infinite loop?](http://stackoverflow.com/questions/3831341/why-does-this-go-into-an-infinite-loop)
- [decompiling DEX into Java sourcecode](http://stackoverflow.com/questions/1249973/decompiling-dex-into-java-sourcecode)
-- [How to create a generic array in Java?](http://stackoverflow.com/questions/529085/how-to-create-a-generic-array-in-java)
- [Why does Math.round(0.49999999999999994) return 1](http://stackoverflow.com/questions/9902968/why-does-math-round0-49999999999999994-return-1)
- [Eclipse: Set maximum line length for auto formatting?](http://stackoverflow.com/questions/3697287/eclipse-set-maximum-line-length-for-auto-formatting)
- [What is the equivalent of the C++ Pair in Java?](http://stackoverflow.com/questions/156275/what-is-the-equivalent-of-the-c-pairl-r-in-java)
From f54a51d12bc98629f1f2469f1910608657cd38c9 Mon Sep 17 00:00:00 2001
From: giantray <51961070@qq.com>
Date: 2016年8月22日 23:46:12 +0800
Subject: [PATCH 145/195] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=B7=B2=E7=BF=BB?=
=?UTF-8?q?=E8=AF=91=E9=97=AE=E9=A2=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 2 +-
contents/android-sdk-installation-doesnt-find-jdk.md | 3 +++
2 files changed, 4 insertions(+), 1 deletion(-)
diff --git a/README.md b/README.md
index 443a313..e57f95e 100644
--- a/README.md
+++ b/README.md
@@ -117,12 +117,12 @@ stackoverflow-Java-top-qa
* [在Android里面下载文件,并在ProgressDialog显示进度](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog.md)
* [如何获取Android设备唯一ID](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/is-there-a-unique-android-device-id.md)
+* [安装Android SDK的时候找不到JDK](contents/android-sdk-installation-doesnt-find-jdk.md)
### 待翻译问题链接(还剩x问题)
- [Proper use cases for Android UserManager.isUserAGoat()?](http://stackoverflow.com/questions/13375357/proper-use-cases-for-android-usermanager-isuseragoat)
- [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)
- [Convert a String to an enum in Java](http://stackoverflow.com/questions/604424/convert-a-string-to-an-enum-in-java)
-- [Android SDK installation doesn't find JDK](http://stackoverflow.com/questions/4382178/android-sdk-installation-doesnt-find-jdk)
- ['Must Override a Superclass Method' Errors after importing a project into Eclipse](http://stackoverflow.com/questions/1678122/must-override-a-superclass-method-errors-after-importing-a-project-into-eclips)
- [Fastest way to determine if an integer's square root is an integer](http://stackoverflow.com/questions/295579/fastest-way-to-determine-if-an-integers-square-root-is-an-integer)
- [How to fix: Unsupported major.minor version 51.0 error?](http://stackoverflow.com/questions/10382929/how-to-fix-unsupported-major-minor-version-51-0-error)
diff --git a/contents/android-sdk-installation-doesnt-find-jdk.md b/contents/android-sdk-installation-doesnt-find-jdk.md
index 7b70797..117b823 100644
--- a/contents/android-sdk-installation-doesnt-find-jdk.md
+++ b/contents/android-sdk-installation-doesnt-find-jdk.md
@@ -28,3 +28,6 @@
- JAVA_HOME=C:/Program Files/Java/jdk1.8.0_11
- JRE_HOME=C:/Program Files/Java/jre8
- Path=%JAVA_HOME%;C:...
+
+stackoverflow链接:
+http://stackoverflow.com/questions/4382178/android-sdk-installation-doesnt-find-jdk
\ No newline at end of file
From ca5550bd7fc8dc35aeca92c51d9d22a87792e50e Mon Sep 17 00:00:00 2001
From: giantray <51961070@qq.com>
Date: 2016年8月24日 09:00:54 +0800
Subject: [PATCH 146/195] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=B7=B2=E7=BF=BB?=
=?UTF-8?q?=E8=AF=91=E9=97=AE=E9=A2=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 1 -
1 file changed, 1 deletion(-)
diff --git a/README.md b/README.md
index e57f95e..624fcdb 100644
--- a/README.md
+++ b/README.md
@@ -127,7 +127,6 @@ stackoverflow-Java-top-qa
- [Fastest way to determine if an integer's square root is an integer](http://stackoverflow.com/questions/295579/fastest-way-to-determine-if-an-integers-square-root-is-an-integer)
- [How to fix: Unsupported major.minor version 51.0 error?](http://stackoverflow.com/questions/10382929/how-to-fix-unsupported-major-minor-version-51-0-error)
- [How to generate a random alpha-numeric string?](http://stackoverflow.com/questions/41107/how-to-generate-a-random-alpha-numeric-string)
-- [Comparing Java enum members: == or equals()?](http://stackoverflow.com/questions/1750435/comparing-java-enum-members-or-equals)
- [Failed to load the JNI shared Library (JDK)](http://stackoverflow.com/questions/7352493/failed-to-load-the-jni-shared-library-jdk)
- [What exactly is Apache Camel?](http://stackoverflow.com/questions/8845186/what-exactly-is-apache-camel)
- [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)
From af7821226f4bab3ccf8f2b5e85ddbb1f327258c2 Mon Sep 17 00:00:00 2001
From: AcceptedBoy <972192420@qq.com>
Date: 2016年8月28日 22:57:19 +0800
Subject: [PATCH 147/195]
add->efficiency-of-java-double-brace-initialization.md
---
...ncy-of-java-double-brace-initialization.md | 202 ++++++++++++++++++
1 file changed, 202 insertions(+)
create mode 100644 contents/efficiency-of-java-double-brace-initialization.md
diff --git a/contents/efficiency-of-java-double-brace-initialization.md b/contents/efficiency-of-java-double-brace-initialization.md
new file mode 100644
index 0000000..6066b1c
--- /dev/null
+++ b/contents/efficiency-of-java-double-brace-initialization.md
@@ -0,0 +1,202 @@
+## "Double Brace Initialization"的效率问题
+
+#### 问题
+
+`Double Brace Initialization`是java的隐藏特性,它有着如下诱人的语法:
+
+```java
+Set flavors = new HashSet() {{
+ add("vanilla");
+ add("strawberry");
+ add("chocolate");
+ add("butter pecan");
+}};
+```
+
+但是,这个特性听说不是很高效率,是否要限制一次性使用?
+
+#### 回答
+
+当我使用匿名内部类时出现了如下的问题:
+
+```Auto
+2009年05月27日 16:35 1,602 DemoApp21ドル.class
+2009年05月27日 16:35 1,976 DemoApp210ドル.class
+2009年05月27日 16:35 1,919 DemoApp211ドル.class
+2009年05月27日 16:35 2,404 DemoApp212ドル.class
+2009年05月27日 16:35 1,197 DemoApp213ドル.class
+
+/* snip */
+
+2009年05月27日 16:35 1,953 DemoApp230ドル.class
+2009年05月27日 16:35 1,910 DemoApp231ドル.class
+2009年05月27日 16:35 2,007 DemoApp232ドル.class
+2009年05月27日 16:35 926 DemoApp233ドル1ドル1ドル.class
+2009年05月27日 16:35 4,104 DemoApp233ドル1ドル.class
+2009年05月27日 16:35 2,849 DemoApp233ドル.class
+2009年05月27日 16:35 926 DemoApp234ドル1ドル1ドル.class
+2009年05月27日 16:35 4,234 DemoApp234ドル1ドル.class
+2009年05月27日 16:35 2,849 DemoApp234ドル.class
+
+/* snip */
+
+2009年05月27日 16:35 614 DemoApp240ドル.class
+2009年05月27日 16:35 2,344 DemoApp25ドル.class
+2009年05月27日 16:35 1,551 DemoApp26ドル.class
+2009年05月27日 16:35 1,604 DemoApp27ドル.class
+2009年05月27日 16:35 1,809 DemoApp28ドル.class
+2009年05月27日 16:35 2,022 DemoApp29ドル.class
+```
+
+这是在我的一个简单应用中所产生的类信息。在这个应用中,使用了大量的匿名内部类,这些类会被单独地编译成`class`文件。
+
+`Double Brace Initialization`是一个带有实例初始化块的匿名内部类。这就意味着每一个新的类的产生都会执行一次实例块,这样的目的通常是为了创建一个简单的对象。
+
+java虚拟机在使用类之前需要去读取其classes信息,然后执行字节码校验等流程。所以为了保存这些`class`文件,所需要的磁盘空间会增大。
+
+这个可以说是`Double Brace Initialization`的开销。所以尽量不要过分使用。
+
+---
+
+在java的介绍中,`Double Brace Initialization`有着如下的写法:
+
+```java
+List list = new ArrayList() {{
+ add("Hello");
+ add("World!");
+}};
+```
+
+看起来像是java的隐藏特性,其实它只是下面代码的一个重写:
+
+```java
+List list = new ArrayList() {
+
+ // 实例初始化块
+ {
+ add("Hello");
+ add("World!");
+ }
+};
+```
+
+所以,它只是在匿名内部类中加上了实例初始化块而已。
+
+---
+
+Joshua Bloch希望以后的集合代码能够像这样简介:
+
+```java
+List intList = [1, 2, 3, 4];
+
+Set strSet = {"Apple", "Banana", "Cactus"};
+
+Map truthMap = { "answer" : 42 };
+```
+
+但目前还没有这样的语法。
+
+---
+
+实践
+
+做一个简单的试验:创建1000个带着`"Hello"`和`"World!"`元素的`ArrayList`
+
+* 方法1:Double Brace Initialization
+
+```
+List l = new ArrayList() {{
+ add("Hello");
+ add("World!");
+}};
+```
+
+* 方法2:初始化`ArrayList`并调用`add`方法
+
+```java
+List l = new ArrayList();
+l.add("Hello");
+l.add("World!");
+```
+
+我修改了java的源文件使之能够为每种上述方法分别创建出1000个实例
+
+* 方法1的测试
+
+```java
+class Test1 {
+ public static void main(String[] s) {
+ long st = System.currentTimeMillis();
+
+ List l0 = new ArrayList() {{
+ add("Hello");
+ add("World!");
+ }};
+
+ List l1 = new ArrayList() {{
+ add("Hello");
+ add("World!");
+ }};
+
+ /* snip */
+
+ List l999 = new ArrayList() {{
+ add("Hello");
+ add("World!");
+ }};
+
+ System.out.println(System.currentTimeMillis() - st);
+ }
+```
+
+* 方法2的测试
+
+```java
+class Test2 {
+ public static void main(String[] s) {
+ long st = System.currentTimeMillis();
+
+ List l0 = new ArrayList();
+ l0.add("Hello");
+ l0.add("World!");
+
+ List l1 = new ArrayList