Tag: 整数

两个整数的乘法溢出导致负数

请考虑Java语言规范中的此片段。 class Test { public static void main(String[] args) { int i = 1000000; System.out.println(i * i); long l = i; System.out.println(l * l); } } 输出是 -727379968 1000000000000 为什么结果-727379968 (i*i) ? 理想情况下应该是1000000000000。 我知道Integer的范围是从-2147483648到2147483647.所以显然1000000000000不在给定的范围内。 为什么结果变成-727379968 ?

JAVA:当Integer大于128时,比较不起作用

这是我的Java程序的一部分,我已经拿出并简化了测试。 任务是比较ArrayList中的两个整数并说明它们是否相等。 以下代码适用于数字 128且代码不起作用。 任何帮助都会非常棒,谢谢。 import java.util.*; public class test { public static void main (String[] args) { Integer seat1Store = 128; Integer seat2Store = 128; Integer seat3Store = 0; Integer seat4Store = 0; Integer seat5Store = 0; ArrayList proceedArray = new ArrayList(); if (seat1Store !=0) { proceedArray.add(seat1Store); } if (seat2Store !=0) { proceedArray.add(seat2Store); } […]

为什么字节总和是整数?

我有tyo字节变量 byte a = 3; byte b = 4; 如果我求它们,sum的值是整数。 byte z = a+b //error, left side is byte, right side is integer 为什么a + b是int?

按顺序从最低到最高的java排序整数

这些数字存储在相同的整数变量中。 我如何从最低到最高的顺序排序整数? 11367 11358 11421 11530 11491 11218 11789

java上的行为不一致==

考虑以下代码: class test { public static void main(String[] args) { test inst_test = new test(); int i1 = 2000; int i2 = 2000; int i3 = 2; int i4 = 2; Integer Ithree = new Integer(2); // 1 Integer Ifour = new Integer(2); // 2 System.out.println( Ithree == Ifour ); inst_test.method( i3 , i4 ); […]

将字符串数组转换为整数数组

所以基本上用户从扫描仪输入中输入一个序列。 12, 3, 4等 它可以是任何长度的,它必须是整数。 我想将字符串输入转换为整数数组。 所以int[0]将是12 , int[1]将是3 ,等等。 任何提示和想法? 我正在考虑实现if charat(i) == ‘,’获取之前的数字并将它们解析在一起并将其应用于数组中当前可用的插槽。 但我不太清楚如何编码。

在String中提取整数部分

提取字符串的整数部分的最佳方法是什么 Hello123 你如何获得123部分。 您可以使用Java的Scanner来破解它,有更好的方法吗?

整数缓存有多大?

类Integer具有缓存,缓存Integer值。 因此,如果我使用方法valueOf或inboxing,新值将不会被实例化,而是从缓存中获取。 我知道默认缓存大小为127但由于VM设置可以扩展。 我的问题是:在这些设置中缓存大小的默认值有多大,我可以操纵这个值吗? 这个值取决于我使用的是哪个VM(32位还是64位)? 我现在正在调整遗留代码,可能需要从int转换为Integer。 澄清:遵循我在Java源代码中找到的代码 private static class IntegerCache { static final int low = -128; static final int high; static final Integer cache[]; static { // high value may be configured by property int h = 127; String integerCacheHighPropValue = sun.misc.VM.getSavedProperty(“java.lang.Integer.IntegerCache.high”); if (integerCacheHighPropValue != null) { int i = parseInt(integerCacheHighPropValue); i = […]

如何在Java中生成特定范围内的随机整数?

如何在特定范围内生成随机int值? 我尝试了以下,但那些不起作用: 尝试1: randomNum = minimum + (int)(Math.random() * maximum); // Bug: `randomNum` can be bigger than `maximum`. 尝试2: Random rn = new Random(); int n = maximum – minimum + 1; int i = rn.nextInt() % n; randomNum = minimum + i; // Bug: `randomNum` can be smaller than `minimum`.

在比较Java中的整数包装器时,为什么128 == 128 false但127 == 127为真?

class D { public static void main(String args[]) { Integer b2=128; Integer b3=128; System.out.println(b2==b3); } } 输出: false class D { public static void main(String args[]) { Integer b2=127; Integer b3=127; System.out.println(b2==b3); } } 输出: true 注意:-128到127之间的数字为真。