java int value,java中Number类的intValue方法是在何方被实现的

本文探讨了Java中Number类的intValue方法的具体实现细节,通过DecimalFormatTest示例,揭示了parse方法如何转化为具体数值类型,并解释了其在Number抽象方法中的应用。
摘要由CSDN通过智能技术生成

java中Number类的intValue方法是在何处被实现的?

本帖最后由 wang_19800105 于 2014-04-28 09:44:42 编辑

import java.text.DecimalFormat;

import java.text.ParseException;

//此类的目得是用来验证1个数是不是在给定的范围内

public class DecimalFormatTest {

public static void main(String args[]){

boolean bl = DecimalFormatTest.isValidDate("100", 1, 100);

System.out.println(bl);

}

private static DecimalFormat numberFormat = new DecimalFormat();

public static boolean isValidDate(String numberString,int min,int max){

Integer validInteger = null;

try{

Number aNumber = numberFormat.parse(numberString);

int anInt = aNumber.intValue(); //此方法在Number中是抽象的,为什么可以使用,是何处被实现的?

if(anInt>=min && anInt<=max){

validInteger = anInt;

}

}catch(ParseException e){

//Ignore and return null;

}

return validInteger != null;

}

}

------解决方案--------------------

numberFormat.parse(numberString);的返回值应该是个具体类,查看一下?

------解决方案--------------------

parse(String source) 调用的是parse(String source, ParsePosition parsePosition) ,而该方法返回的是详细的具体类,以下是DecimalFormat.parse(String source, ParsePosition parsePosition)的源代码

public Number More ...parse(String text, ParsePosition pos) {

// special case NaN

if (text.regionMatches(pos.index, symbols.getNaN(), 0, symbols.getNaN().length())) {

pos.index = pos.index + symbols.getNaN().length();

return new Double(Double.NaN);

}

boolean[] status = new boolean[STATUS_LENGTH];

if (!subparse(text, pos, positivePrefix, negativePrefix, digitList, false, status)) {

return null;

}

// special case INFINITY

if (status[STATUS_INFINITE]) {

if (status[STATUS_POSITIVE] == (multiplier >= 0)) {

return new Double(Double.POSITIVE_INFINITY);

} else {

return new Double(Double.NEGATIVE_INFINITY);

}

}

if (multiplier == 0) {

if (digitList.isZero()) {

return new Double(Double.NaN);

} else if (status[STATUS_POSITIVE]) {

return new Double(Double.POSITIVE_INFINITY);

} else {

return new Double(Double.NEGATIVE_INFINITY);

}

}

if (isParseBigDecimal()) {

BigDecimal bigDecimalResult = digitList.getBigDecimal();

if (multiplier != 1) {

try {

bigDecimalResult = bigDecimalResult.divide(getBigDecimalMultiplier());

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值