标签: unboxing

使用增强型for循环时是否正在转换数组?

Java 5或更高版本是否对数组应用了某种形式的"装箱"?这个问题浮现在脑海中,因为下面的代码通过一个数组,好像它是一个Iterable.

for( String : args ){
// Do stuff
}
Run Code Online (Sandbox Code Playgroud)

java arrays unboxing

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

在性能,隐式(自动)拆箱或显式拆箱方面哪个更好?

把它放在代码中 - 性能更好(如果有差异的话)?

鉴于这种:

public class Customer
{
    ....

    public Boolean isVIP(){...}
    ...
}
Run Code Online (Sandbox Code Playgroud)

哪个更快?

public void handleCustomer(Customer customer)
{
    if (customer.isVIP())  // Auto Unboxing
    {
        handleNow(customer);
    }
    else
    {  
        sayHandlingNowButQueueForTomorrow(customer);
    }
}
Run Code Online (Sandbox Code Playgroud)

或这个:

public void handleCustomer(Customer customer)
{
    if (customer.isVIP().booleanValue()) // Explicit unboxing
    {
        handleNow(customer);
    }
    else
    {  
        sayHandlingNowButQueueForTomorrow(customer);
    }
}
Run Code Online (Sandbox Code Playgroud)

java performance unboxing

1
推荐指数
2
解决办法
1407
查看次数

1
推荐指数
2
解决办法
597
查看次数

关于第二个陈述,以下哪一项是正确的?(拆箱和自动装箱)

我已经浏览了整个互联网,试图解决这个问题.任何人都可以正确回答并解释原因吗?非常感谢!

请查看以下代码.

Integer myNumber;
myNumber = 5;
Run Code Online (Sandbox Code Playgroud)

关于第二个陈述,以下哪一项是正确的?

  1. 该语句执行拆箱

  2. 该语句执行自动包装.

  3. 该声明执行自动装箱.

  4. 它会导致错误,因为您无法将基元类型分配给包装类对象.

java autoboxing unboxing

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

取消装箱后无法访问类的成员

我正在学习高级C#.在下面的代码中,我正在尝试进行事件处理

我在取消装箱后访问类发件人的成员时遇到错误//编译器不允许我使用这些名称

//    Console.WriteLine("Sender is {0} and message is {1}",obj.name,obj.messgae);
Run Code Online (Sandbox Code Playgroud)

为什么会这样?这就是我们所说的拳击和拆箱,如果我不会感到困惑.

在我到目前为止所做的所有示例中,都有事件类继承EventArgs.该课程需要什么.虽然在这里我没有使用过这个课程,但我直接使用了Eventargs(只是制作了它).

      namespace ConsoleApplication5
    {
        class eventclass : EventArgs
        {
            string var;
        }
        delegate void GenerateEvent(object source, EventArgs e);
        class Sender
        {
            public void MyMethod()
            {
                Console.WriteLine("My method Called");

            }
            private string name;
            public string Name
            {
                get
                {
                    return name;
                }
                set
                {
                    name = value;
                }
            }

            private string message;
            public string Message
            {
                get
                {
                    return message;
                }
                set
                {
                    message = value;
                }
            }
            public event …
Run Code Online (Sandbox Code Playgroud)

c# events unboxing object

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

我应该在断言中装箱还是取消装箱?

假设我有一个方法foo:

public Integer foo () { return 1; }
Run Code Online (Sandbox Code Playgroud)

以上哪个选项的价格较低?拳击还是拆箱?

assert(1, (int) foo()));
Run Code Online (Sandbox Code Playgroud)

assert((Integer)3, foo());
Run Code Online (Sandbox Code Playgroud)

java unboxing

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

为什么在这个java代码中没有应用拆箱?

public class P {
  String m(int i) {
    return "P.m(int)";
  }

  String m(Object o) {
    return "P.m(Object)";
  }
}

public class Test {
  public static void main(String[] args) {
    P p = new P();
    System.out.println(p.m(Integer.valueOf(42)));
  }
}
Run Code Online (Sandbox Code Playgroud)

我不明白为什么这个程序打印"Pm(Object)"而不是"Pm(int)".

java unboxing

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

拆箱 cameraCharacteristics 可能会产生 Nullpointer Exception 和 FindBugs

我无法处理导致FindBugs抛出错误的以下警告。

错误

我正在使用camera2 api。如您所见,我已经在检查 null 并另外捕获可能的 NullPointer 异常。CameraCharacteristics 类的 .get 方法使用 Nullable 进行注释,因此出现此错误。我不知道如何防止这种情况发生。检查 null 似乎不能完成这项工作。

同时,我将 SuppressFBWarnings Annotation 添加到我的项目中。但即使我抑制这样的警告:

@SuppressFBWarnings(value = "NP_NULL_ON_SOME_PATH")
private void setUpCamera(int width, int height) {
    try {
        for (String cameraId : cameraManager.getCameraIdList()) {
            CameraCharacteristics cameraCharacteristics = cameraManager.getCameraCharacteristics(cameraId);

            if (cameraCharacteristics != null && cameraCharacteristics.get(CameraCharacteristics.LENS_FACING) != null) {
                int lensFaceingCameraCharacteristics = cameraCharacteristics.get(CameraCharacteristics.LENS_FACING);

                if (cameraFacing == lensFaceingCameraCharacteristics) {
                    StreamConfigurationMap streamConfigurationMap = cameraCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
                    previewSize = getPreviewSize(streamConfigurationMap.getOutputSizes(SurfaceTexture.class), width, height);
                    this.cameraId = cameraId;
                }
            }
        }
    } catch (CameraAccessException | NullPointerException …
Run Code Online (Sandbox Code Playgroud)

java android unboxing annotations findbugs

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

Boxing/unboxing, changing the copy of the refence of the boxed value does not refected to the boxed value

So I read through the documentation of Microsoft here.

Consider the following code:

int i = 0;
object o = i;
object p = o;

o = 1;
p = 2;

Console.WriteLine($"o:{o}, p:{p}");
//output o:1, p:2
Run Code Online (Sandbox Code Playgroud)

My understanding is that boxing happen on object o = i;, now o is a refence to the value in heap. Then p is assigned to be same as o.

Why isn't the change of p refected to o? Aren't …

c# boxing unboxing types reference

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

取消一个数字加倍

是否有某种方法可以将未知数字转换成双倍?例如

    public static double Foo(object obj)
    {
        if (!obj.GetType().IsValueType)
            throw new ArgumentException("Argument should be a number", "obj");
        return (double) obj;
    }

    private static void Main(string[] args)
    {
        double dbl = 10;
        decimal dec = 10;
        int i = 10;
        short s = 10;
        Foo(dbl);
        Foo(dec);
        Foo(i);
        Foo(s);
    }
Run Code Online (Sandbox Code Playgroud)

但是当尝试将unbox打包为不正确的类型时,此代码会抛出异常.

.net c# boxing unboxing

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