day_04 刷题日记

总算感觉读书刷题有点用了哈哈哈哈哈,开心。剑指offer杀我!雪菜带我飞!

1.String s = new String(“xyz”);创建了几个StringObject?

​ 话说这个题挺坑的,因为我刚看了书,一个在堆中一个在常量池中,不过在常量池中新建的前提是,常量池中没有我们要新建的这个对象,目前我们不知道常量池中有没有xyz,所以可能是一个也可能是两个。

2.以下JAVA程序代码的输出是 C

public static void main(String args[]) {
    System.out.println(17^5);
}

12
22
20
1419857

​ 我选的D,看成多少次方了。分别转二进制,然后进行异或运算。

3.代码输出结果?A

public class Base
{
    private String baseName = "base";
    public Base()
    {
        callName();
    }
 
    public void callName()
    {
        System. out. println(baseName);
    }
 
    static class Sub extends Base
    {
        private String baseName = "sub";
        public void callName()
        {
            System. out. println (baseName) ;
        }
    }
    public static void main(String[] args)
    {
        Base b = new Sub();
    }
}


null
sub
base

​ 在创造派生类的过程中首先创建基类对象,然后才能创建派生类。创建基类即默认调用Base()方法,在方法中调用callName()方法,由于派生类中存在此方法,则被调用的callName()方法是派生类中的方法,此时派生类还未构造,所以变量baseName的值为null。

4.what is the result of the following code?

enum AccountType
{
    SAVING, FIXED, CURRENT;
    private AccountType()
    {
        System.out.println(“It is a account type”);
    }
}
class EnumOne
{
    public static void main(String[]args)
    {
        System.out.println(AccountType.FIXED);
    }
}

Compiles fine and output is prints”It is a account type”once followed by”FIXED”
Compiles fine and output is prints”It is a account type”twice followed by”FIXED”
Compiles fine and output is prints”It is a account type”thrice followed by”FIXED”
Compiles fine and output is prints”It is a account type”four times followed by”FIXED”
Compilation fails

​ 枚举类型实际上是class,有构造方法,而且还只能是private不能是public。枚举类型的每个值都是对象,SAVINT实际上是public static final AccountType SAVING=new AccountType();,所以说三个枚举量,打印三次It is a account type。选C。

5.以下哪几种方式可用来实现线程间通知和唤醒:( )

Object.wait/notify/notifyAll
ReentrantLock.wait/notify/notifyAll
Condition.await/signal/signalAll
Thread.wait/notify/notifyAll

答案是AC,我选的BD没谁了。wait()、notify()和notifyAll()是Object类中的方法.Condition是在java1.5中才出现的,它用来替代传统的Object的wait()、notify()实现线程间的协作,相比使用Object的wait()、 notify(),使用Condition1的await()、signal()这种方式实现线程间协作更加安全和高效。

B错: ReentrantLock使用lock和unlock

D错: Thread的wait/notify/notifyAll继承自Object.没有自己实现.

6.关于类方法、实例方法和this的一些问题。

img

7.运行结果?

public class Example{
    String str=new String("tarena");
    char[]ch={'a','b','c'};
    public static void main(String args[]){
        Example ex=new Example();
        ex.change(ex.str,ex.ch);
        System.out.print(ex.str+" and ");
        System.out.print(ex.ch);
    }
    public void change(String str,char ch[]){
   //引用类型变量,传递的是地址,属于引用传递。
        str="test ok";
        ch[0]='g';
    }
}


tarena and abc
tarena and gbc
test ok and abc
test ok and gbc

选B,首先这个String是个不可变的类,调用change方法之后其实是没用的,但是如果换成可变的StringBuilder,输出就变了。结合之前在疯狂java讲义上看到的,强调一下Java没有引用传递,只有值传递。运行change方法的时候,str和ch会各自复制一份作为change的形参传入,main方法中的ch实际上是一个引用,保存了char数组对象的地址值,当把ch的值赋给change()的ch形参之后,就是让change()方法的ch形参也保存了这个地址值,也就是会引用到堆内存的char数组对象。ch是个引用变量,操作的还是堆内存中的char数组对象,不管是main方法里还是change()方法里,实际上都是操作一个对象。所以会看到ch[0]的值改变是有效的。

其实如果是把String类型换成int啥的,结果还是只有char数组会被改变。

public class Test01 {

	//String str=new String("tarena");
	StringBuilder str=new StringBuilder("tarena");
	char[]ch={'a','b','c'};
	public static void main(String args[]){
		Test01 ex=new Test01();
		ex.change(ex.str,ex.ch);
		System.out.print(ex.str+" and ");
		System.out.print(ex.ch);
	}
	public void change(StringBuilder str,char ch[]){
		str=str.append(" test ok");
		ch[0]='g';
	}

}

打印结果:tarena test ok and gbc

8.线程安全(Thread-safe)的集合对象:

  • Vector 线程安全:
  • HashTable 线程安全:
  • StringBuffer 线程安全:

非线程安全的集合对象:

  • ArrayList :
  • LinkedList:
  • HashMap:
  • HashSet:
  • TreeMap:
  • TreeSet:
  • StringBulider:

9.即使垃圾回收器工作,finalize方法也不一定得到执行,这是由于程序中的其他线程的优先级远远高于执行finalize()函数线程的优先级。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值