标签: autoboxing

int vs int []差异 - 在泛型内部自动装箱?

看到这段代码:

import java.util.*;
public class Temp{
    public static void main(String[] args){
        List<int[]> list1 = new ArrayList<int[]>(); //WORKS!
        List<double[]> list2 = new ArrayList<double[]>(); //WORKS!
        //List<double> list3 = new ArrayList<double>(); //DOES NOT WORK
        //List<int> list4 = new ArrayList<int>(); //DOES NOT WORK
    }
}
Run Code Online (Sandbox Code Playgroud)

AFAIK,java泛型不支持原始类型,那么int[]编译是怎么回事?如何在这里进行自动装箱?

java generics autoboxing

0
推荐指数
1
解决办法
177
查看次数

Scala的行为/理解数值类型的隐式变换?

我试图理解Scala for-loop隐式盒子/拆箱"数字"类型的行为.为什么这两个首先失败但其余的失败?

1)失败:

scala> for (i:Long <- 0 to 10000000L) {}

      <console>:19: error: type mismatch;<br>
      found   : Long(10000000L)
      required: Int
              for (i:Long <- 0 to 10000000L) {}
                                  ^
Run Code Online (Sandbox Code Playgroud)

scala> for (i <- 0 to 10000000L) {}

      <console>:19: error: type mismatch;
      found   : Long(10000000L)
      required: Int
          for (i <- 0 to 10000000L) {}
                         ^
Run Code Online (Sandbox Code Playgroud)

scala> for (i:Long <- 0L to 10000000L) {}

scala> for (i <- 0L to 10000000L) {}

scala> for (i:Long <- 0 to 10000000L) {}

      <console>:19: error: type …
Run Code Online (Sandbox Code Playgroud)

autoboxing numerical types scala numerical-methods

0
推荐指数
1
解决办法
313
查看次数

关于Java内存管理

可能重复:
整数包装器对象仅在值127内共享相同的实例?

我对Java中的内存管理有疑问.

当我尝试以下代码时:

Integer a = 1;
Integer b = 1;
System.out.println(a==b); // this gives "true" 
Run Code Online (Sandbox Code Playgroud)

然而,

Integer a = 256;
Integer b = 256;
System.out.println(a==b); //this gives "false"
Run Code Online (Sandbox Code Playgroud)

为什么?

非常感谢.

java memory autoboxing

0
推荐指数
1
解决办法
133
查看次数

我该怎么自动装这个班?

我有以下课程:

public class IntegerKey extends Number implements Comparable<IntegerKey> {

    private Integer m_key;

    public IntegerKey(Integer key) {
        m_key = key;
    }

    public IntegerKey(int key) {
        m_key = key;
    }

}
Run Code Online (Sandbox Code Playgroud)

我想使用这个课程如下:

假设我有以下泛型:

Map<IntegerKey, MyCache> map = new HashMap<IntegerKey, MyCache>();

map.put(5, new MyCache());
Run Code Online (Sandbox Code Playgroud)

这不编译,为什么?我不想这样做:

map.put(new IntegerKey(5), new MyCache());
Run Code Online (Sandbox Code Playgroud)

谢谢.

java autoboxing boxing

0
推荐指数
1
解决办法
335
查看次数

将int转换为String计为Autoboxing?

AFAIK当Java自动将原始类型转换为包装类对象而不是其被称为自动装箱时,因为原语被装入包装类.

也是

int test = 3;
String str = String.valueOf(test);
Run Code Online (Sandbox Code Playgroud)

算作自动装箱?

有理由问?

我最近遇到了这个问题.我认为是java 5中引入的自动装箱的原因(Java 4中没有).

java autoboxing string-interning

0
推荐指数
1
解决办法
1313
查看次数

使用Java中的泛型函数进行Integer/int自动装箱查询

好的,所以我正在尝试用Java编写一个通用的排序包(我正在学习算法课程,我认为这是一种很好的实践,无论是在算法意义上还是在Java中,因为我对语言都很新).无论如何,我认为最好的方法是创建一个Sort具有多种排序方法的类,例如insertionSort,mergeSort等等.如果您觉得有更好的方法,请告诉我作为评论,因为我始终对编写更清晰,更高效的代码的建议.

至于问题:

我有骨干结构,想先尝试编码insertionSort方法.但是,我试图传递一个整数数组但是遇到了一些问题.首先,使用泛型我理解你必须使用<Integer>而不是<int>,但出于某种原因,如果我创建一个数组,int[] arr = new int[]{..}并将其传递给泛型它不起作用.如何在不使用的情况下解决此问题Integer[] arr = new Integer[]{..}?我的印象是,编译器将我的盒子intInteger自动?

我的代码:

public class Sort<T> {
    public T[] insertionSort(T[] array) {
        // do some stuff
        return array;
    }

    public static void main(String[] args) {
        int[] orig = new int[]{1,35,3,2,4634,2,46,7,33,56};
        Sort<Integer> sorter = new Sort<Integer>();
        sorter.insertionSort(orig);
    }
}
Run Code Online (Sandbox Code Playgroud)

java generics int autoboxing integer

0
推荐指数
1
解决办法
95
查看次数

自动装箱不适用于布尔值

我有一个简单的类下面,当编译autoboxes正确的整数但是,我没有为我的布尔做它坚持我应该将参数更改为布尔值.我正在使用jdk 1.8否则编译器会抱怨Integer转换.我看不出我做错了什么?所有包装类都可以开箱即用自动包装,所以我想?

public class MsgLog<Boolean,String> {

    private boolean sentOk ;
    private Integer id ;
    private int id2 ;
    public boolean isSentOk() {
        return sentOk;
    }

    public String getTheMsg() {
        return theMsg;
    }

    private String theMsg ;

    private MsgLog(Boolean sentOkp, String theMsg)
    {

        this.sentOk = sentOkp ; // compile error - autoboxing not working

        this.theMsg = theMsg ;

        this.id = 2; // autoboxing working
        this.id2 = (new Integer(7)) ; // autoboxing working the other way around as well

    }

}
Run Code Online (Sandbox Code Playgroud)

Autoboxing是一个双向过程不是这样吗? …

java autoboxing boolean

0
推荐指数
1
解决办法
434
查看次数

自动装箱和原始类型以匹配方法签名

在版本中1.5,Java引入了概念auto-boxing.

public interface SomeInterface {
    public void test(Integer val);
}

public class Main implements SomeInterface {

    /*The method signature gets different and compiler is asking to override 
    un-implemented methods*/
    public void test(int t) {

    }
}
Run Code Online (Sandbox Code Playgroud)

那么为什么我为了覆盖未实现的方法而得到编译时错误,为什么上面的测试方法的参数不auto-boxed匹配父测试方法签名?

java autoboxing

0
推荐指数
1
解决办法
1283
查看次数

为什么Integer.parseInt(String str)在Java中返回int而不是Integer?

我问这个问题,因为我有一种情况,我有以下两种方法:

public T get(Serializable id) and
public T get(int id) 
Run Code Online (Sandbox Code Playgroud)

我必须在大多数情况下使用第一种方法,而第二种方法已在我们的系统中弃用.

我的输入是String所以每次我需要调用时 get(Serializable id),我必须传递Integer而不是int,否则它将使用不推荐使用的第二种方法.即我打电话Integer.parseInt(str),然后我不得不专栏它整数(即(Integer)Integer.parseInt(str)),这似乎是多余的.

有没有更好的方法来实现这一点,为什么Integer.parseInt(str)return int而不是Integer的实现?

java autoboxing serialization unboxing integer

0
推荐指数
1
解决办法
568
查看次数

自动装箱,然后在Java中加宽

以下程序打印Object为输出,当我删除重载方法包含Objectas参数时,以下编译时错误是:

LangPackage类型中的方法m1(Long)不适用于参数(int)

 public class A {
    public static void main(String args[])
    {
      int x = 0;
       m1(x);

    }
    static void m1(Long l) {
            System.out.println("long");

        }

        static void m1(Object l) {
            System.out.println("Object");

        }
}
Run Code Online (Sandbox Code Playgroud)

我的查询是为什么auto-boxing后面加宽是允许Objects不为Long类型

java autoboxing

0
推荐指数
1
解决办法
383
查看次数

Java,整数不是唯一的,但它们如何在集合中进行比较?

我用这种直截了当的方法:

    Collection<Integer> aCollection = Arrays.asList(1,2,3,4,5,6);
    Integer a = new Integer(5);

    if( aCollection.contains(a) )
        System.out.println("aCollection contains 5");
Run Code Online (Sandbox Code Playgroud)

结果是"aCollection contains 5".整数不是整个系统中的唯一对象,但在这种情况下是"5" Collection<Integer>new Integer(5)事实上引用同一个对象?我可以安全地假设任何集合在调用时都会以这种方式运行.contains()吗?

我不确定,因为通过==结果比较引用和值时:

3 == new Integer(3)
new Integer(3) != new Integer(3)
new Integer(3) == 3
Run Code Online (Sandbox Code Playgroud)

java collections autoboxing

-1
推荐指数
1
解决办法
95
查看次数

==运算符返回false,如果比较具有相同值的Double类型

当我比较具有相同值的两个双基元类型时,为什么等于方法是错误的?但整数不是

public class EqualMethod {
    public static void main(String[] args) {        
        Double value1 = 6.2;
        Double value2 = 6.2;        
        System.out.println(value1 == value2);

        Integer number1 = 2;
        Integer number2 = 2;
        System.out.println(number1 == number2);  
    } 
}
Run Code Online (Sandbox Code Playgroud)

java autoboxing equals

-1
推荐指数
1
解决办法
3186
查看次数

Java autoboxing性能比较

    // Hideously slow program! Can you spot the object creation?
    Long sum = 0L;
    for (long i = 0; i < Integer.MAX_VALUE; i++) {
        sum += i;
    }
    end = System.currentTimeMillis();
    System.out.println("Long sum took: " + (end - start) + " milliseconds");

    long sum2 = 0L;
    for (long i = 0; i < Integer.MAX_VALUE; i++) {
        sum2 += i;
    }
    end = System.currentTimeMillis();
    System.out.println("long sum took: " + (end - start) + " milliseconds");
Run Code Online (Sandbox Code Playgroud)

嗨,我正在阅读有效的Java,并且Item 6:Avoid creating unnecessary objects …

java performance autoboxing

-1
推荐指数
1
解决办法
1606
查看次数