几个Java基础题

1.java中线程能不能重复start

  t1.start();
        System.out.println("ssss");
        t1.start();

答:第一个start能运行,然后打出ssss,第二个运行时错误。编译不会错。

2.java中接口能不能多继承?

答:可以多继承。但是只能继承别的接口。不能直接或间接的继承自己。

Interface3 Extends Interface0, Interface1, interface……

不允许类多重继承的主要原因是,如果A同时继承B和C,而b和c同时有一个D方法,A如何决定该继承那一个呢?
但接口不存在这样的问题,接口全都是抽象方法继承谁都无所谓,所以接口可以继承多个接口

3.抽象类能不能实现接口?

答:当然可以。

4.Java 中对象的equals,==,hashCode(),还有HashMap.put(object,xxxx)之间的关系。

String a="abc";String b="abc"; a==b为true

String a=new String("abc"); a==b 为false

对于非基础数据类型来讲,== 比较的是地址,“abc”是常量字符串,a="abc" ,b="abc"其实a,b指向都是同一个常量字符串地址。

new String 是在堆区分配内存,a=new String("abc"),a指向堆区的 new String地址。

 1 package com.abc.zj;
 2 
 3 public class Test {
 4     String a;
 5     public Test(String a){
 6         this.a=a;
 7     }
 8     public int hashCode(){
 9         
10         return super.hashCode();
11         //return 1;
12     }
13     public boolean equals(Object object){
14         if(a==((Test)object).a){
15             return true;
16         }else{
17             return false;
18         }
19     }
20 }
 1      public static void main(String argv[]) throws ArithmeticException{
 2          Test testa = new Test("abc");
 3          Test testb = new Test("abc");
 4          if(testa==testb){
 5              System.out.println("testa==testb");
 6          }
 7          if(testa.equals(testb)){
 8              System.out.println("testa equals testb");
 9          }
10          if(testa.a==(testb.a)){
11              System.out.println("testa.a == testb.a");
12          }
13          if(testa.a.equals(testb.a)){
14              System.out.println("testa.a equals testb.a");
15          }
16          
17          Map<Test,String> map = new HashMap<Test,String>();
18          map.put(testa,    "value1");
19          map.put(testb,"value2");
20          for(Test t:map.keySet()){
21              System.out.println(map.get(t));
22          }
23      }

运行结果:

testa equals testb
testa.a == testb.a
testa.a equals testb.a
value1
value2

说明:最后 value1 和 value2 先后顺序可能有变化,每次运行都可能不一样。map遍历随机,没顺序。

	public int hashCode(){
		
		//return super.hashCode();
		return 1;
	}

 这样以后,运行结果变为:

testa equals testb
testa.a == testb.a
testa.a equals testb.a
value2

==================

	public int hashCode(){
		
		//return super.hashCode();
		return 1;
	}
	public boolean equals(Object object){
//		if(a==((Test)object).a){
//			return true;
//		}else{
//			return false;
//		}
		return false;
	}

 这样 运行结果变为:

testa.a == testb.a
testa.a equals testb.a
value2
value1

结论:hashCode 一样 且 equals 返回true,hashMap才认为两个key相同。

5.Integer ,int ,==,装箱 拆箱

小于128的Integer之间的比较。

         Integer i=128;
         Integer b=128;
         if(i==b){
             System.out.println("i=b");
         }
         if(i.equals(b)){
             System.out.println("i equals b");
         }        

输出 i equals b

         Integer i=127;
         Integer b=127;
         if(i==b){
             System.out.println("i=b");
         }
         if(i.equals(b)){
             System.out.println("i equals b");
         }

输出

i=b
i equals b

原因: -128 到 127 之间 用缓存。看Integer源码

public static Integer valueOf(int i) {
        assert IntegerCache.high >= 127;
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }

网上其他的人解释:看看Integer 的源代码就知道了,其实就是Integer 把-128-127之间的每个值都建立了一个对应的Integer 对象,类似一个缓存。由于Integer 是不可变类,因此这些缓存的Integer 对象可以安全的重复使用。Integer i = XXX ,就是Integer i = Interger.valueOf(XXX), 首先判断XXX 是否在-128-127 之间,如果是直接return 已经存在的对象,否则就只能new 一个了。

new Integer 就不一样了。例如:

         Integer i=new Integer(127);
         Integer b=127;
         if(i==b){
             System.out.println("i=b");
         }
         if(i.equals(b)){
             System.out.println("i equals b");
         }                

这样结果是:i equals b

6.子类重写父类的方法的访问修饰符,不能低于父类的方法访问权限

对于接口来讲,实现接口的类中,对于实现的接口中的方法,都必须是public。因为接口中的方法都是public。

接口中的方法,只有public 和 abstract可以修饰,并且默认就是他俩修饰。(static不行)。

接口中的常量,默认都是public final static 修饰。

原文地址:https://www.cnblogs.com/aji2014/p/6687746.html