草庐IT

long-integer

全部标签

java - 为什么 Float.parseFloat() 同时抛出 NumberFormatException 和 NullPointerException 而 Integer.parseInt() 只抛出 NumberFormatException?

在检查我的错误处理代码时偶然发现了这个。当您调用Integer.parseInt(null)时,Java抛出NumberFormatException而Float.parseFloat(null)抛出NullPointerException。另请参阅Float的文档和Integer这种差异是否有特定的技术或设计原因,或者它只是一个历史怪癖? 最佳答案 这绝对是一件历史文物。多年来,这种不一致和相关文档问题已作为JavaBug多次提出。例如:>https://bugs.openjdk.java.net/browse/JDK-64639

Java 允许将字节分配给 java.lang.Short 但不允许分配给 java.lang.Integer

finalbyteb=12;Shorts=b;Integeri=b;程序对Short编译良好,但对Integer编译失败并显示“不兼容类型”消息。我很难理解这种行为。对于这个特定场景,我找不到任何东西。 最佳答案 我试图用更广泛的分配上下文组来复制它:finalbyteb=12;Byteb2=b;Characterc=b;//Onlyanerrorifbisn'tfinalcharc2=b;//Onlyanerrorifbisn'tfinalShorts=b;//Onlyanerrorifbisn'tfinalshorts2=b;I

java通过反射访问Integer构造函数

我有这个代码。为什么它不起作用?(工作意味着它显示3)我该如何修复它?publicclassMain{publicstaticVcopy(Vvar){try{return(V)var.getClass().getConstructor(var.getClass()).newInstance(var);}catch(Exceptione){System.out.println("Copyfaield"+e.getMessage()+"");e.printStackTrace();}returnnull;}publicstaticvoidmain(String[]args){Integer

java - 为什么从 int 到 Long 的隐式转换是不可能的?

我可以隐式地将int转换为long,并将long转换为Long。为什么不能将int隐式转换为Long?为什么Java不能在示例的最后一行进行隐式转换?inti=10;//OKlongprimitiveLong=i;//OKLongboxedLong=primitiveLong;//OKboxedLong=i;//Typemismatch:cannotconvertfrominttoLong 最佳答案 Long和Integer是对象。装箱/拆箱仅适用于基元。执行LongboxedLong=i就像LongboxedLong=newInt

java - 为什么传递给 List 参数的 List<Integer> 上的 add(String) 不抛出异常?

为什么可以插入String进入List在下面的代码中?我有一个将数字插入整数列表的类:publicclassMain{publicstaticvoidmain(String[]args){Listlist=newArrayList();list.add(2);list.add(3);list.add(4);Inserterinserter=newInserter();inserter.insertValue(list);System.out.print(list);}}然后我有一个单独的类,它插入一个String进入List,带有数字字符串值"42":publicclassInsert

java - 为什么 Gson 将 Integer 解析为 Double?

一个复杂的json字符串,我想将其转换为map,我有一个问题。请看这个简单的测试:publicclassTest{@SuppressWarnings("serial")publicstaticvoidmain(String[]args){MaphashMap=newHashMap();hashMap.put("data","{\"rowNum\":0,\"colNum\":2,\"text\":\"math\"}");MapdataMap=JsonUtil.getGson().fromJson(hashMap.get("data").toString(),newTypeToken>()

java - 如何将 Float(包装类)转换为 Integer 包装类?

如何在java中将float转换为整数?Floatvalue=30.0F如何将上述值转换​​为整数?请帮帮我好吗? 最佳答案 使用Float.intValue():Integeri=value.intValue();请注意,这会导致自动装箱,但由于您打算创建一个Integer,因此这不会对性能产生任何影响。另请注意,您应该注意舍入:intValue()和int向零舍入。要四舍五入到最接近的整数,使用Math.round(),向下舍入使用Math.floor(),向上舍入使用Math.ceil()。如果您需要一些其他类型的舍入,您需要

java - 比较 java 中的 Long 值

这个问题在这里已经有了答案:关闭9年前。PossibleDuplicate:Integer==intallowedinjava下面两个语句有什么区别Longl1=2L;if(l1==2)System.out.println("EQUAL");if(l1.longValue()==2)System.out.println("EQUAL");他们都给出相同的结果“EQUAL”。但我怀疑Long是对象。怎么相等?

java - 将字符串添加到列表 <Integer>

以下代码的结果:publicclassListIntegerDemo1{publicstaticvoidaddToList(Listlist){list.add("0067");list.add("bb");}publicstaticvoidmain(String[]args){Listlist=newArrayList();addToList(list);System.out.println(list.get(0));}}是“0067”。当我将一个字符串添加到一个整数列表时,我会预料到一个RuntimeException或类似的异常。为什么没有问题? 最佳

java - 将 Java 的 Integer 转换为 Scala 的 Int

我有一段返回java.lang.Integer的Java代码,它可以是null:someClass.getMyInteger但是当我在Scala类中使用它时,出现了这个错误:Causedby:java.lang.NullPointerExceptionatscala.Predef$.Integer2int(Predef.scala:357)即Scala隐式尝试将Java的Integer转换为Scala的Int(使用隐式Integer2int方法),但由于在这种情况下Integer为null它失败并出现异常。如何解决这个问题? 最佳答案