StackOverflowError例子 |
java |
|
public class SingleTest {
public SingleTest instance = new SingleTest();
private SingleTest(){
}
public static void main(String[] args) {
SingleTest test1 = new SingleTest().instance;
System.out.println(test1.hashCode());
}
}
|
向前初始化,JAVA代码初始化是有顺序的 |
java |
|
package com.huawei.test;
public class AHEADInit {
int j = init_j(i); //这里编译报错
int i = 10;
private int init_j(int i2) {
return i2*i2;
}
}
|
File文件的路径问题 |
java |
|
public static void main(String[] args) throws Exception{
File file = new File("");
System.out.println(System.getProperty("user.dir"));
System.out.println(System.getProperty("user.home"));
System.out.println(file.getAbsolutePath());
// testByJNI();
}
上面的代码执行路径证实了eclipse对目录src和bin目录进行了过滤,如果用eclipse执行和cmd命令行执行结果是不相同的。
1.在eclipse下面执行得到的路径D:\checkout\code
2.在cmd下面执行得到的路径是D:\checkout\code\bin
|
GCInvocateTest |
java |
|
package com.huawei.test;
public class GCInvocateTest {
@Override
protected void finalize() throws Throwable {
System.out.println("clean up glash");
super.finalize();
}
public static void main(String[] args) {
GCInvocateTest gc = new GCInvocateTest();
gc = null;
System.gc();
System.out.println("1");
System.gc();
}
}
|
关于HashMap中对key的hash测试 |
hash |
|
package com.huawei.test;
import java.util.HashMap;
public class HashTest {
public static void main(String[] args) {
getSize1();
getSize2();
}
public static void getSize1(){
HashMap<Object, String> instance = new HashMap<Object, String>();
instance.put(new String(), "xx");
instance.put(new String(), "xx");
System.out.println(instance.size());
}
public static void getSize2(){
HashMap<Object, String> instance = new HashMap<Object, String>();
instance.put(new HashObject(), "xx");
instance.put(new HashObject(), "xx");
System.out.println(instance.size());
}
private static class HashObject{
}
}
|
子类定义父类重名变量的结果 |
java |
|
父类:
package com.huawei.test;
public class Parent {
protected String name = "parent";
protected String school = "grade_school";
public void printName(){
System.out.println(name);
}
}
子类:
package com.huawei.test;
public class Child extends Parent{
private String name = "child";
private String school = "colleage_school";
public static void main(String[] args) {
Parent parent = new Child();
parent.printName();
System.out.println(parent.school);
}
@Override
public void printName(){
System.out.println(name);
}
}
打印结果________________?
|
printwriter打印 |
|
|
public static void main(String[] args){
printWriterTest();
printStreamTest();
}
public static void printWriterTest(){
PrintWriter pw = new PrintWriter(System.out);
pw.print("中国");
pw.close();
}
public static void printStreamTest(){
System.out.println("中国");
}
|