四字词语接龙大全:JAVA中对象类型的能赋给基本类型的吗?

来源:百度文库 编辑:查人人中国名人网 时间:2024/05/03 01:27:43
例如 int i= Integer.parseInt("ab3");
这个改怎么解释才可以呢?
谁明白,详细说明,我给加分!!!
越详细+越明白的,我给加的越多!!!

一点浅见,仅供参考:
如果你写入以下语句

try {
int i = Integer.parseInt("ab3");
}
catch(NumberFormatException nfe){
System.out.println("Not a number");
}
finally {
int i = 0;
// do something must be done and depend on the value of i here
}

你会得到一个NumberFormat Exception,然后做finally部分.
通过Integer.parseInt("...")是可以将一个对象类型(string)转换为基本类型的,但是必须和那个基本类型匹配.

另,类似这样的问题应该都可以从sun api中获取答案,如果你想搞这行要学会使用各种资源.

parseInt
public static int parseInt(String s) throws NumberFormatException

将字符串参数作为带符号十进制整数来分析。除过第一个字符为 ASCII 字符中减号 '-' 表示的负数,字符串中的字符都必须是十进制数。

参数:
s - 串。
返回值:
十进制参数表示的整数。
抛出: NumberFormatException
若该串不包含一个可分析的整数。

这是jdkdoc中的回答,我想String类的对象s必须为数字,也就是ab3是不成立的;
我简单写了个程序,发现工具要求你重构parseInt方法,就是将String后面重写string对象,而return中必须返回数值,代码如下:
import java.lang.*;

public class Integer {
public static void main(String args[]){
int i= Integer.parseInt("ab3");
System.out.println(i);
}

private static int parseInt(String string) {
// TODO Auto-generated method stub
return 0;
}
}

return 后的参数不能写成String型,而只能是Int型。