当前位置: 代码迷 >> J2SE >> scjp 错误有关问题+println null有关问题
  详细解决方案

scjp 错误有关问题+println null有关问题

热度:538   发布时间:2016-04-24 13:09:17.0
scjp 异常问题+println null问题!
public class wrenwren
{
static int a = 1;
public static void main(String[] args)
{
System.out.println("aa:" + aa());
System.out.println("aaaa"+a);
System.out.println(null); //这句话为什么编译不了呢?? }
public static int aa()
{

int b = 10;
try
{
System.out.println("abc");
return a;
}
finally
{
a = 2;
System.out.println("a: " + a);
}
}
}

结果:abc
a: 2
aa:1
aaaa2


1。结果为什么是aa:1?
2。上面红字部分System.out.println(null);为什么编译部过去??

------解决方案--------------------
你去查帮助会发现System.out的println有下面几种
void println() 
Terminate the current line by writing the line separator string. 
 void println(boolean x) 
Print a boolean and then terminate the line. 
 void println(char x) 
Print a character and then terminate the line. 
 void println(char[] x) 
Print an array of characters and then terminate the line. 
 void println(double x) 
Print a double and then terminate the line. 
 void println(float x) 
Print a float and then terminate the line. 
 void println(int x) 
Print an integer and then terminate the line. 
 void println(long x) 
Print a long and then terminate the line. 
 void println(Object x) 
Print an Object and then terminate the line. 
 void println(String x) 
Print a String and then terminate the line. 
但是你传个null过去是什么意思??让编译器怎么识别?null是上面的参数中哪个类的子类?哪个都不是嘛!

至于为什么aa:1 那是因为在try的时候已经把a返回出来了,后面的操作肯定对它的输出没影响咯
------解决方案--------------------
对那个null不能执行没兴趣

aa:1 楼上的解释应该不够彻底 那为什么aaaa2呢

return语句返回的是调用方法的结果 但是方法返回前会继续执行finally子句

return a; 返回的并不是a 而是a的值 相当于return 1; 调用aa()的结果是a的值 并不是aa()=a 然后finally里改变了a的值 但是这个与aa()的返回值无关 
如果
finally 

a = 2; 
System.out.println("a: " + a); 
return a+1; //这样调用aa() 的结果就是3了 但是a还是2 分清aa()的返回值和a的值你就明白了


------解决方案--------------------
不知道你想输出null是什么意思 加个引号可以当字符串输出
------解决方案--------------------
null即不是对象也不是基本数据类型,就不会有地址空间,能输出什么呢???
  相关解决方案