Skip to content
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ 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)
* [在java中声明数组](https://github.com/chungchi300/stackoverflow-java-top-qa/blob/master/contents/declare-array-in-java.md)

> 随机

* [为什么以下用随机生成的文字会得出 “hello world”?](https://github.com/chungchi300/stackoverflow-java-top-qa/blob/master/contents/why-does-this-code-using-random-strings-print-hello-world.md)

> 编程技巧

Expand All @@ -52,25 +57,25 @@ 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唯一码?](https://github.com/chungchi300/stackoverflow-java-top-qa/blob/master/contents/is-there-a-unique-android-device-id.md)

1,2,3
### 待翻译问题链接(还剩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)
- [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)
- [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)
- ['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)
Expand Down
32 changes: 32 additions & 0 deletions contents/declare-array-in-java.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
##在java中声明数组

###问题描述:
你是如何在java中声明数组的。

你可以进行用数组声明或者数组常量(注意:当你声明或者立即影响了变量,数组常量将不能再用来分配一个数组)
对于原始类型:
```
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");
```

注意:
1.数组常量
```
//for a function return type is int[],you cannot return
{1,2,3}
//but you can return
new int[]{1, 2, 3};
```

[Stackoverflow链接:Declare array in Java?](http://stackoverflow.com/questions/1200621/declare-array-in-java#)



115 changes: 115 additions & 0 deletions contents/is-there-a-unique-android-device-id.md
Original file line number Diff line number Diff line change
@@ -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();
```

谨记:要取得以下权限
```
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
```
stackoverflow链接:
http://stackoverflow.com/questions/2785485/is-there-a-unique-android-device-id
Original file line number Diff line number Diff line change
@@ -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

而把每一个数目字加 <code>`</code> (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