java题库总结6
无论是static还是非static的全局变量,如果不加限制随意访问的话易出现同步问题。
无论是static还是非static的局部变量,每个线程都是私有的,其他线程不会对其进行干扰。
2.volatile关键字
保证了变量的可见性
被volatile关键字修饰的变量,如果值发生了变更,其他线程立马可见
设备的硬件寄存器(如:状态寄存器)
一个中断服务子程序中会访问到的非自动变量(Non-automatic variables)
多线程应用中被几个任务共享的变量
3.以下程序段执行后将有()个字节被写入到文件afile.txt中。
try { FileOutputStream fos = new FileOutputStream("afile.txt"); DataOutputStream dos = new DataOutputStream(fos); dos.writeInt(3); dos.writeChar(1); dos.close(); fos.close();} catch (IOException e) {}//6
byte 1 short 2 int 4 long 8 char 2 float 4 double 8 boolean 1
4.
class Test { public static void main(String[] args) { System.out.println(new B().getValue()); } static class A { protected int value; public A (int v) { setValue(v); } public void setValue(int value) { this.value= value; } public int getValue() { try { value ++; return value; } finally { this.setValue(value); System.out.println(value); } } } static class B extends A { public B () { super(5); setValue(getValue()- 3); } public void setValue(int value) { super.setValue(2 * value); } }}//22 34 17
5.
public static void main(String[] args) throws Exception {
int num=0;
int count=0;
for(int i=0;i<=100;i++) {
num=num+i;
count=count++;
}
System.out.println(num*count);//0
}
考察作用域和i++的知识点
i=i++;和i++;的区别
count=count++就是先把局部变量表中count的值0放入操作数栈中,然后直接对局部变量表中的count加1,
然后再把操作数栈中的0出栈赋值给局部变量表中的count,最终局部变量表中的count值仍为0
浙公网安备 33010602011771号