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/creating-a-memory-leak-with-java.md b/contents/creating-a-memory-leak-with-java.md new file mode 100644 index 0000000..267a4db --- /dev/null +++ b/contents/creating-a-memory-leak-with-java.md @@ -0,0 +1,41 @@ +# 如何使用Java创建一个内存泄漏的程序 + +## 问题: +我在一个面试的过程中被问到如何使用Java创建一个内存泄漏的程序。毫无疑问地说,我当时哑口无言,根本不知道如何开始编码。 + +## 解答 +在Java下有一个很好的方法来创建内存泄漏程序--通过使得对象不可访问但任然存储在内存中。 + +1. 应用程序创建一个长期运行的线程A 或者 使用一个线程池来加快泄漏的速度。 + +2. 线程A使用ClassLoader(用户可以自定义)加载一个类 B。 + +3. 在类B申请一块很大的连续内存(例如:new byte[1000000]), +并使用一个静态成员变量保存该空间的一个强引用,之后在一个ThreadLocal对象中存储类B对象的引用。 +虽然泄漏这个类的一个实例就足够了,但是也可以通过申请多个实例的方法来加快内存泄漏的速度。 + +4. 线程A清理所有指向自定义类或者通过ClassLoadeer加载的引用。 + +5. 重复上述步骤 + +上述方式可以达到内存泄漏的目的,因为 ThreadLocal 存储了一个指向类B对象的引用, +而该对象又保存了一个指向其类的引用,这个类又保存了一个指向其ClassLoader的引用, +而ClassLoader又保存了一个通过它加载的所有类的引用。 +这种方法在许多的JVM的实现中表现更糟糕,因为Classes和ClassLoader被直接存储在老年代(permgen)并且永远都不会被GC处理。 + +******************************下方为个人理解************************************ + +通过一个简单的图来描述上述关系:
+ThreadLocal.obj ---> B.obj ---> B.class <--> ClassLoader.obj
+注:上图的\*.obj表示\*类的一个实例对象,B.class表示类B的Class对象 + +******************************上方为个人理解************************************ + +这个模式的一个变形:如果频繁的重新部署那些可能使用ThreadLocals的应用,应用容器(例如Tomcat)就会像筛子一样泄漏内存。 +因为应用容器使用上述所说的线程,每次重新部署应用时,应用容器都会使用一个新的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 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-create-a-java-string-from-the-contents-of-a-file.md b/contents/how-to-create-a-java-string-from-the-contents-of-a-file.md new file mode 100644 index 0000000..afaa3d4 --- /dev/null +++ b/contents/how-to-create-a-java-string-from-the-contents-of-a-file.md @@ -0,0 +1,59 @@ +## 怎样将文件的内容生成java string +### 问题: +我现在使用如下的语句有一段时间了。它看起来是最广泛使用的,最起码在我浏览过的网站来说。 +有谁有一个更好/不同的方法来将文件中的内容转换为java中的string吗? + +````java +private String readFile(String file) throws IOException { + BufferedReader reader = new BufferedReader(new FileReader(file)); + String line = null; + StringBuilder stringBuilder = new StringBuilder(); + String ls = System.getProperty("line.separator"); + + try { + while((line = reader.readLine())!= null) { + stringBuilder.append(line); + stringBuilder.append(ls); + } + + return stringBuilder.toString(); + } finally { + reader.close(); + } +} + +```` + +### 最佳答案: +#### 从文件中读取全部内容 +下面是java7中利用实用的Method实现的一种紧凑、健壮的写法。 +````java +static String readFile(String path, Charset encoding) + throws IOException +{ + byte[] encoded = Files.readAllBytes(Paths.get(path)); + return new String(encoded, encoding); +} +```` +#### 从文件中读取文件行 +java7增加了一种更方便的从文件中[`读取文件行的方法`](http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#readAllLines%28java.nio.file.Path,%20java.nio.charset.Charset%29),用 List表示。 +这种方式是“有损耗的”,因为行分隔符在每行最后被剥夺了。 +````java +List lines = Files.readAllLines(Paths.get(path), encoding); +```` +#### 内存分析 +第一种保留行分隔符的方法能在短时间内需求文件规模几倍的内存,因为短时间内源文件内容(字节数组)和编码的字符(每个都是16位的即使在文件中按8位编译)都曾经在内存中存留。处理那些相对于有限的内存很小的文件是使用这种方法最安全的方式。 +第二种读取文件行的方法通常来说更节省内存,因为输入字节的缓冲不必包含整个文件。但是,对于相比内存很大的文件来说,这种方法也同样不合适。 +对于读取大型文件来说,你需要设计一个不同的系统。这个系统从流中读取一大块文本,处理,然后移动到下一块,重新使用同样的大小固定的内存模块。这里,“大型”取决于电脑的规格。现在,定义“大型”的门槛可能从许多G的RAM开始。 +#### 字符编码 +原来的解答中遗漏了关于字符编码的说明。只有一些特殊情况下,平台的默认设定是你需要的,但是这种情况很少,你应该能够根据自己的需求调整。 +StanderdCharsets类定义了一些编码中需要的常量。 +````java +String content = readFile("test.txt", StandardCharsets.UTF_8); +```` +平台的默认设定中也可从charset类中得到。 +````java +String content = readFile("test.txt", Charset.defaultCharset()); +```` + +StackOverFlow 链接:http://stackoverflow.com/questions/326390/how-to-create-a-java-string-from-the-contents-of-a-file 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 98% 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 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 对象序列化/反序列化。对象的序列化主要有两种用途: - 把对象序列化成字节码,保存到指定介质上(如磁盘等) 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