错题整理 + 错题相关的知识要点整理(一)

题目1代码:

public class Test01 {
    public static void main(String[] args) {
        int i = 0;
        for (foo('A') ; foo('B') && i < 2; foo('C')){
            foo('D');
            i++;
        }
    }

    static boolean foo(char c){
        System.out.println(c);
        return true;
    }
}

程序结果:
ABDCBDCB

原理:
for(条件1;条件2;条件3){代码块}的运行顺序:
条件1 -> 条件2 -> 代码块 -> 条件3 -> 条件2 -> 代码块 -> 条件3 -> 条件2 -> ……
可参考for(int i = 0;i < 10; i++){代码块}的运行顺序 

题目2代码:

public class Test {
    static String x="1";
    static int y=1;
    public static void main(String args[]) {
        static int z=2;
        System.out.println(x+y+z);
    }
}

结果:
编译报错

原因:static修饰的变量称为静态变量,静态变量属于整个类,而局部变量属于方法,只在该方法内有效,所以static不能修饰局部变量

题目3代码:

public class Test02 {

    public static String sRet = "";

    public static void func(int i) {
        try {
            if (i % 2 == 0) {
                throw new Exception();
            }
        } catch (Exception e) {
            sRet += "0";
            return;
        } finally {
            sRet += "1";
        }
        sRet += "2";
    }

    public static void main(String[] args) {
        func(1);
        func(2);
        System.out.println(sRet);
    }

}

结果:
1201

运行到try - catch中的catch部分代码时,将不会运行方法中除了try-catch-finally以外的代码块
  1. HashMap中解决hash冲突的方法是链地址法

  2. Java程序内存泄露的最直接表现是程序抛内存控制的Exception

    java是自动管理内存的,通常情况下程序运行到稳定状态,内存大小也达到一个 基本稳定的值,但是内存泄露导致Gc不能回收泄露的垃圾,内存不断变大,最终超出内存界限,抛出OutOfMemoryExpection

题目6代码:

public class Test2
{
    public void add(Byte b)
    {
        b = b++;
    }
    public void test()
    {
        Byte a = 127;
        Byte b = 127;
        add(++a);
        System.out.print(a + " ");
        add(b);
        System.out.print(b + "");
    }
}


题目7代码:

package NowCoder;
class Test {
	public static void hello() {
	    System.out.println("hello");
	}
}
public class MyApplication {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Test test=null;
		test.hello();
	}
}

结果:编译能通过并且能正确运行
hello

原因:
因为Test类的hello方法是静态的,所以是属于类的,当实例化该类的时候,静态会被优先加载而且只加载一次,所以不受实例化new Test();影响,只要是使用到了Test类,都会加载静态hello方法!
另外,在其他类的静态方法中也是可以调用公开的静态方法,此题hello方法是使用public修饰的所以在MyApplication中调用hello也是可以的。
总结:即使Test test=null;这里也会加载静态方法,所以test数据中包含Test类的初始化数据。(静态的,构造的,成员属性)

题目8代码:

public class Student {
    String name;

    public Student(String name) {
        this.name = name;
    }

    public static void main(String[] args) {
        Student s1 = new Student("Tom");
        Student s2 = new Student(new String("Tom"));
        System.out.println(s1 == s2);
        System.out.println(s1.equals(s2));
    }

    @Override
    public boolean equals(Object o) {
        if (o instanceof Student){
            Student student = (Student) o;
            return student.name.equals(this.name);
            //return o.name.equals(this.name);
            //采用注释的写法则会编译报错
        }else {
            return false;
        }
    }
}
结果:
false
true
原因:s1与s2对象的地址值不一致,因此用 == 比较地址值则为false,重写equlas去比较s1与s2的name属性,传输的是Object需要对Object进行向下造型成Student类才能有name属性,因此注释的编写方法编译错误
  1. 定义有StringBuffer s1=new StringBuffer(10);s1.append(“1234”)则s1.length()和s1.capacity()分别是4和10

    原因:StirngBuffer中的length()方法是返回字符串长度,如果字符串长度没有初始化的长度大,就返回字符串的长度,capacity则返回初始化的长度;如果append后的字符串长度超过初始化长度,capacity返回增长后的长度
    StringBuffer s = new StringBuffer(x); x为初始化容量长度
    s.append(“Y”); "Y"表示长度为y的字符串
    length始终返回当前长度即y;
    对于s.capacity():
    1.当y<x时,值为x
    以下情况,容器容量需要扩展
    2.当x<y<2x+2时,值为 2x+2
    3.当y>2*x+2时,值为y

    StringBuffer和StringBuilder的默认大小为16
    ArrayList和LinkedList的默认大小10

题目10代码:

public class A implements B{
public static void main(String args[]){
    int i;
    A a1=new  A();
    i =a1.k;
    System.out.println("i="+i);
    }
}
interface B{
    int k=10}
答案:i = 10
原因:在接口里面的变量默认都是public static final 的,它们是公共的,静态的,最终的常量.相当于全局常量,可以直接省略修饰符。
实现类可以直接访问接口中的变量
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值