|
| 1 | +## [1. Java += 操作符实质](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/java-operator.md) |
| 2 | +### Java += 操作符实质 |
| 3 | + |
| 4 | +### 问题 |
| 5 | +我之前以为: |
| 6 | +i += j 等同于 i = i + j; |
| 7 | +但假设有: |
| 8 | +```java |
| 9 | +int i = 5; |
| 10 | +long j = 8; |
| 11 | +``` |
| 12 | +这时 i = i + j 不能编译,但 i += j 却可以编译。这说明两者还是有差别的 |
| 13 | +这是否意味着,i += j,实际是等同于 i= (type of i) (i + j)呢? |
| 14 | + |
| 15 | +### 回答 |
| 16 | +这个问题,其实官方文档中已经解答了。 请看这里 [§15.26.2 Compound Assignment Operators](http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.26.2) |
| 17 | + |
| 18 | + |
| 19 | +再照搬下官方文档的说明 |
| 20 | + |
| 21 | + |
| 22 | +对复合赋值表达式来说,E1 op= E2 (诸如 i += j; i -= j 等等),其实是等同于 E1 = (T)((E1) op (E2)),其中,T是E1这个元素的类型。 |
| 23 | + |
| 24 | +举例来说,如下的代码 |
| 25 | +```java |
| 26 | +short x = 3; |
| 27 | +x += 4.6; |
| 28 | +``` |
| 29 | +等同于 |
| 30 | +```java |
| 31 | +short x = 3; |
| 32 | +x = (short)(x + 4.6); |
| 33 | +``` |
| 34 | + |
| 35 | + |
| 36 | +stackoverflow链接 |
| 37 | +http://stackoverflow.com/questions/8710619/java-operator |
| 38 | + |
| 39 | + |
| 40 | +## [2. 将InputStream转换为String](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/read-convert-an-inputstream-to-a-string.md) |
| 41 | +### 将InputStream转换为String |
| 42 | + |
| 43 | +### 使用Apache库 |
| 44 | +不重复造轮子。最靠谱的方法,还是用Apache commons IOUtils |
| 45 | +这样简单几行代码就搞定了 |
| 46 | +```java |
| 47 | +StringWriter writer = new StringWriter(); |
| 48 | +IOUtils.copy(inputStream, writer, encoding); |
| 49 | +String theString = writer.toString(); |
| 50 | +``` |
| 51 | +或者 |
| 52 | +String theString = IOUtils.toString(inputStream, encoding)//这个方法其实封装了上面的方法,减少了一个参数 |
| 53 | + |
| 54 | +### 使用原生库 |
| 55 | +如果不想引入Apache库,也可以这样做 |
| 56 | +```java |
| 57 | +static String convertStreamToString(java.io.InputStream is) { |
| 58 | + java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A"); |
| 59 | + return s.hasNext() ? s.next() : ""; |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +stackoverflow讨论地址 |
| 64 | +http://stackoverflow.com/questions/309424/read-convert-an-inputstream-to-a-string |
| 65 | + |
| 66 | + |
| 67 | +## [3. 将数组转换为List](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/create-arraylist-from-array.md) |
| 68 | +### 将数组转换为List |
| 69 | + |
| 70 | +### 问题 |
| 71 | +假设有数组 |
| 72 | +```java |
| 73 | +Element[] array = {new Element(1),new Element(2),new Element(3)}; |
| 74 | +``` |
| 75 | +如何将其转换为ArrayList`<Element>` arraylist = ??? |
| 76 | + |
| 77 | +### 回答1 |
| 78 | + |
| 79 | + `new ArrayList<Element>(Arrays.asList(array))` |
| 80 | + |
| 81 | +### 回答2 |
| 82 | + |
| 83 | +Arrays.asList(array)或者Arrays.asList(new Element(1),new Element(2),new Element(3)) |
| 84 | + |
| 85 | +不过,这样做有些坑要注意: |
| 86 | + |
| 87 | +1. 这样做生成的list,是定长的。也就是说,如果你对它做add或者remove,都会抛UnsupportedOperationException。 |
| 88 | +2. 如果修改数组的值,list中的对应值也会改变! |
| 89 | + |
| 90 | +**Arrays.asList() 返回的是Arrays内部静态类,而不是Java.util.ArrayList的类。这个java.util.Arrays.ArrayList有set(),get(),contains()方法,但是没有任何add() 方法,所以它是固定大小的** |
| 91 | + |
| 92 | + |
| 93 | +如果希望避免这两个坑,请改用这个方式 |
| 94 | +```java |
| 95 | +Collections.addAll(arraylist, array); |
| 96 | +``` |
| 97 | + |
| 98 | +stackoverflow原址: |
| 99 | +http://stackoverflow.com/questions/157944/how-to-create-arraylist-arraylistt-from-array-t |
| 100 | + |
| 101 | + |
| 102 | +## [4. 如何遍历map对象](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/iterate-through-a-hashmap.md) |
| 103 | +## [public,protected,private,不加修饰符。有什么区别呢?](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/in-java-whats-the-difference-between-public-default-protected-and-private.md) |
| 104 | +## [5. 如何测试一个数组是否包含指定的值?](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/how-can-i-test-if-an-array-contains-a-certain-value.md) |
| 105 | +## [6. 重写(Override)equlas和hashCode方法时应考虑的问题](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/what-issues-should-be-considered-when-overriding-equals-and-hashcode-in-java.md) |
| 106 | +## [7. 从一个多层嵌套循环中直接跳出](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/breaking-out-of-nested-loops-in-java.md) |
| 107 | +## [8. 如何将String转换为Int](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/converting-string-to-int-in-java.md) |
| 108 | +## [9. 如何分割(split)string字符串](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/how-to-split-a-string-in-java.md) |
| 109 | +## [10. 在java中如何对比(compare)string](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/how-do-i-compare-strings-in-java.md) |
| 110 | +## [11. `Map<Key,Value>`基于Value值排序](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/how-to-sort-a-mapkey-value-on-the-values-in-java.md) |
| 111 | +## [12. `HashMap和Hashtable的区别](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/differences-between-hashmap-and-hashtable.md) |
| 112 | +## [13. 如何便捷地将两个数组合到一起](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/how-to-concatenate-two-arrays-in-java.md) |
| 113 | +## [14. Java 是否支持默认的参数值](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/does-java-support-default-parameter-values.md) |
| 114 | +## [15. Java 产生指定范围的随机数](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/generating-random-integers-in-a-range-with-Java.md) |
| 115 | +## [16. JavaBean 到底是什么](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/what-is-a-javabean-exactly.md) |
| 116 | +## [17. wait()和sleep()的区别](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/difference-between-wait-and-sleep.md) |
| 117 | +## [能否在一个构造器( `constructor` )中调用另一个构造器](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/how-do-i-call-one-constructor-from-another-in-java.md) |
| 118 | +## [ `finally` 代码块总会被执行么](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/does-finally-always-execute-in-java.md) |
| 119 | +## [如何将String转换为enum](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/convert-a-string-to-an-enum-in-java.md) |
| 120 | +## [在Java中声明数组](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/declare-array-in-java.md) |
| 121 | +## [反射是什么及其用途](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/what-is-reflection-and-why-is-it-useful.md) |
| 122 | +## [为什么不能用string类型进行switch判断](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/why-cant-i-switch-on-a-string.md) |
| 123 | +## [比较java枚举成员使用equal还是==](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/comparing-java-enum-members-or-equals.md) |
| 124 | +## [用java怎么创建一个文件并向该文件写文本内容](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/how-to-create-a-file-and-write-to-a-file-in-java.md) |
| 125 | +## [serialVersionUID 有什么作用?该如何使用?](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/what-is-a-serialversionuid-and-why-should-i-use-it.md) |
| 126 | +## [为什么Java的```Vector```类被认为是过时的或者废弃的](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/why-is-java-vector-class-considered-obsolete-or-deprecated.md) |
| 127 | +## [Java的foreach循环是如何工作的](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/how-does-the-java-for-each-loop-work.md) |
| 128 | +## [为什么相减这两个时间(1927年)会得到奇怪的结果](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/why-is-subtracting-these-two-times-in-1927-giving-a-strange-result.md) |
| 129 | +## [Java 中如何将 String 转换为 enum](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/lookup-enum-by-string-value.md) |
| 130 | +## [该什么时候使用 ThreadLocal变量,它是如何工作的](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/when-and-how-should-i-use-a-threadlocal-variable.md) |
| 131 | +## [servlets的运行原理](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/how-do-servlets-work-instantiation-shared-variables-and-multithreading.md) |
| 132 | +## [如何计算MD5值](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/how-can-i-generate-an-md5-hash.md) |
| 133 | +## [Java中软引用和弱引用的区别](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/what-is-the-difference-between-a-soft-reference-and-a-weak-reference-in-java.md) |
| 134 | +## [JSF, Servlet 和 JSP (三种技术)有什么区别](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/what-is-the-difference-between-jsf-servlet-and-jsp.md) |
| 135 | +## [Java内部类和嵌套静态类](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/java-inner-class-and-static-nested-class.md) |
| 136 | +## [@Component, @Repository, @Service的区别](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/whats-the-difference-between-component-repository-service-annotations-in.md) |
| 137 | +## [如何创建泛型java数组](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/how-to-create-a-generic-array-in-java.md) |
| 138 | + |
| 139 | +# 编程技巧 |
| 140 | + |
| 141 | +## [去掉烦人的"!=null"(判空语句](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/avoiding-null-statements-in-java.md) |
| 142 | +## [获取完整的堆栈信息](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/get-current-stack-trace-in-java.md) |
| 143 | +## [如何用一行代码初始化一个ArrayList](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/initialization-of-an-arraylist-in-one-line.md) |
| 144 | +## [初始化静态map](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/how-can-i-initialize-a-static-map.md) |
| 145 | +## [给3个布尔变量,当其中有2个或者2个以上为true才返回true](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/check-if-at-least-two-out-of-three-booleans-are-true.md) |
| 146 | +## [输出 Java 数组最简单的方式](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/whats-the-simplest-way-to-print-a-java-array.md) |
| 147 | +## [为什么以下用随机生成的文字会得出 "hello world"?](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/why-does-this-code-using-random-strings-print-hello-world.md) |
| 148 | +## [什么在java中存放密码更倾向于char[]而不是String](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/why-is-char-preferred-over-string-for-passwords-in-java.md) |
| 149 | +## [如何避免在JSP文件中使用Java代码](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/how-to-avoid-java-code-in-jsp-files.md) |
| 150 | +* [Java 源码里的设计模式](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/examples-of-gof-design-patterns-in-javas-core-libraries.md) |
| 151 | +* [如何产生一个随机的字母数字串作为 session 的唯一标识符](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/how-to-generate-a-random-alpha-numeric-string.md) |
| 152 | +* [如何创建单例](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/what-is-an-efficient-way-to-implement-a-singleton-in-java.md) |
| 153 | +* [实现Runnable接口 VS. 继承Thread类](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/implements-runnable-vs-extends-thread.md) |
| 154 | +* [我应该用哪一个@NotNull注解](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/which-notnull-java-annotation-should-i-use.md) |
| 155 | +* [怎样将堆栈追踪信息转换为字符串](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/how-can-i-convert-a-stack-trace-to-a-string.md) |
| 156 | +* [如何处理 java.lang.outOfMemoryError PermGen space error](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/dealing-with-java-lang-outofmemoryerror-permgen-space-error.md) |
| 157 | +* [如何在整数左填充0](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/how-can-i-pad-an-integers-with-zeros-on-the-left.md) |
| 158 | +* [在调用 instanceof 前需要进行null检查吗](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/is-null-check-needed-before-calling-instanceof.md) |
| 159 | +* [如何从文件里读取字符串](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/how-do-i-create-a-java-string-from-the-contents-of-a-file.md) |
| 160 | +* [遍历集合时移除元素,怎样避免ConcurrentModificationException异常抛出](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/iterating-through-a-collection-avoiding-concurrentmodificationexception-when-reiterating-through-a-collection-avoiding-concurrentmodificationexception-when-re.md) |
| 161 | +* [如何让IntelliJ编辑器永久性显示代码行数](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/how-can-i-permanently-have-line-numbers-in-intellij.md) |
| 162 | +* [如何使用maven把项目及其依赖打包为可运行jar包](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/how-can-i-create-an-executable-jar-with-dependencies-using-maven.md) |
| 163 | + |
| 164 | +> 网络 |
| 165 | + |
| 166 | +* [如何使用java.net.URLConnection接收及发送HTTP请求](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/using-java-net-urlconnection-to-fire-and-handle-http-requests.md) |
| 167 | + |
| 168 | +> 性能 |
| 169 | + |
| 170 | +* [LinkedList、ArrayList各自的使用场景,如何确认应该用哪一个呢?](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/when-to-use-linkedlist-over-arraylist.md) |
| 171 | +* [StringBuilder和StringBuffer有哪些区别呢](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/stringbuilder-and-stringbuffer.md) |
| 172 | +* [为什么处理排序的数组要比非排序的快](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/why-is-processing-a-sorted-array-faster-than-an-unsorted-array.md) |
| 173 | +* [如何使用Java创建一个内存泄漏的程序](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/creating-a-memory-leak-with-java.md) |
| 174 | +* [为什么打印"B"会明显的比打印"#"慢](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/why-is-printing-b-dramatically-slower-than-printing.md) |
| 175 | + |
| 176 | +> 测试 |
| 177 | + |
| 178 | +* [如何测试 private 方法,变量或者内部类](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/how-to-test-a-class-that-has-private-methods-fields-or-inner-classes.md) |
| 179 | +* [JUnit4如何断言确定异常的抛出](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/how-do-you-assert-that-a-certain-exception-is-thrown-in-junit-4-tests.md) |
| 180 | + |
| 181 | +> Android |
| 182 | + |
| 183 | +* [在Android里面下载文件,并在ProgressDialog显示进度](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog.md) |
| 184 | +* [如何获取Android设备唯一ID](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/is-there-a-unique-android-device-id.md) |
| 185 | +* [安装Android SDK的时候找不到JDK](https://github.com/linpeiyou/stackoverflow-java-top-qa/blob/master/contents/android-sdk-installation-doesnt-find-jdk.md) |
0 commit comments