scjp 异常问题+println null问题!

wren4255 2008-04-02 04:26:46
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);为什么编译部过去??
...全文
57 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
qinjinxtu 2008-04-03
  • 打赏
  • 举报
回复
null即不是对象也不是基本数据类型,就不会有地址空间,能输出什么呢???
  • 打赏
  • 举报
回复
不知道你想输出null是什么意思 加个引号可以当字符串输出
wren4255 2008-04-03
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 classnameextendsjava 的回复:]
对那个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了 …
[/Quote]

首先谢谢你的回答,谢谢!!
可是第二个问题,我想输出null(是常量吧),怎么输出呢??
  • 打赏
  • 举报
回复
对那个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的值你就明白了
}
rypgood 2008-04-02
  • 打赏
  • 举报
回复
你去查帮助会发现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返回出来了,后面的操作肯定对它的输出没影响咯

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧