希望同学们看后,写输出的答案和解释原因(越详细越好)
Q1:
package com.victor.test;
public class TestMain {
public static void main(String[] args) {
String value = "Hello";
System.out.println("Hello" + "World" == "HelloWorld");
join(value);
System.out.println(value);
}
public static void join(String value) {
value+="World";
System.out.println(value=="HelloWorld");
}
}
Q2:
package com.victor.test;
public class TestMain2 {
public static void main(String[] args) {
System.out.println(getResult("a"));
}
public static String getResult(String str) {
String result = "defalut";
switch (str) {
case "a":
result = "a";
case "b":
result = "b";
default:
result = "c";
}
return result;
}
}
Q3:
package com.victor.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class TestMain3 {
public static void main(String[] args) {
List<String> aList = new ArrayList<String>();
aList.add(null);
Set aset = new HashSet();
aset.add(null);
Map<String,String> aMap = new HashMap<String,String>();
aMap.put(null,null);
Hashtable<Object, Object> attributeTypes = new Hashtable<Object, Object>();
attributeTypes.put("String",null);
attributeTypes.put(null,null);
}
}
Q4:
package com.victor.test;
public class TestMain4 {
public static void main(String[] args) {
System.out.println(getValue());
}
public static int getValue(){
int i=1;
try {
throw new Exception();
} catch (Exception e) {
return ++i;
}finally{
++i;
System.out.println("finally : " + i);
}
}
}
javascript:
Q1:
var foo = function() {
speak();
jumpp;
var jump = function jumpp(){
console.log('how high');
}
return;
function speak(){
console.log('howdy');
}
};
foo();
Q2:
console.log("" == 0);
console.log(0 == '0');
console.log(false == 'false');
console.log(false == '0');
console.log(false == null);
console.log(null == undefined);
暂时先发这些,希望知道原理的童鞋写出心得供大家学习,谢谢了。
------解决思路----------------------
最主要的原因还是因为value是在堆中,在编译期是无法确定的,所以必须新创建一个对象。如果用value.intern()。就和第二个结果一样了。