我看到以下代码,想知道编码器的意图。它是自动装箱的相关性能吗?
map.put("doesntMatter", Boolean.TRUE);
Run Code Online (Sandbox Code Playgroud)
他本可以这样做:
map.put("doesntMatter", true);
Run Code Online (Sandbox Code Playgroud)
做第一个有什么好处吗?
当数值比较运算符用于比较 Java 中的 2 个整数对象时,我试图了解以下代码的行为。
Integer i1 = new Integer(1);
Integer i2 = new Integer(1);
System.out.println(i1 == i2);
System.out.println(i1 > i2);
System.out.println(i1 >= i2);
Run Code Online (Sandbox Code Playgroud)
上面代码的输出是:
false
false
true
Run Code Online (Sandbox Code Playgroud)
我理解在第一种情况下发生的事情(对象实例的比较是这样的,这就是它给出错误的原因)。但是为什么第二个和第三个场景不同,它究竟是如何工作的呢?
布尔值会自动装箱到同一个实例吗?
我有一个小测试表明确实如此,但我不会对此下任何赌注。有保证吗?
@Test
public void autoboxBooleans() {
Multimap<Boolean, Integer> ids = HashMultimap.create();
for (int i = 0; i < 10; i++) {
Boolean rand = ThreadLocalRandom.current().nextBoolean();
ids.put(rand, System.identityHashCode(rand));
}
System.out.println(ids); // {false=[453523494], true=[2024918163]}
}
Run Code Online (Sandbox Code Playgroud)
注意:这个问题讨论的是 0-127 范围之外的整数。
我有一个 HashMap 声明为:
Map<Character, Integer> dict = new HashMap<Character, Integer>();
在代码中的某处,我从上面的映射中获取字符的整数值。我观察到,如果在获取整数值时不添加该.intValue()
方法,代码将终止,没有任何错误,如果我将其放在intValue()
** 标记的区域中,它可以正常工作。
下面是完整的代码:
class Solution {
public String minWindow(String s, String t) {
int l =0, r = 0;
Map<Character, Integer> dict = new HashMap<Character, Integer>();
for(int i = 0; i < t.length(); i++){
int count = dict.getOrDefault(t.charAt(i), 0);
dict.put(t.charAt(i), count + 1);
}
int formed = 0;
int required = dict.size();
Map<Character, Integer> window = new HashMap<Character, Integer>();
int[] ans = {-1, 0, 0};
while(r …
Run Code Online (Sandbox Code Playgroud) 例子来自Kotlin官网
val a: Int = 100
val boxedA: Int? = a
val anotherBoxedA: Int? = a
val b: Int = 100
val boxedB: Int? = b
val anotherBoxedB: Int? = b
println(a === a) // true
println(boxedA === anotherBoxedA) // true
println(boxedB === anotherBoxedB) // true
Run Code Online (Sandbox Code Playgroud)
我理解了上面的例子。但是,当我的值更改一个和b从100到1000,输出变为假的真象下面这样:
val a: Int = 1000
val boxedA: Int? = a
val anotherBoxedA: Int? = a
val b: Int = 1000
val …
Run Code Online (Sandbox Code Playgroud) 来自Java,我非常习惯于自动装箱,其中int在需要时自动包装到Integer,并且Integer可以拆箱到基元中.在iOS5中有类似的东西我可以依赖吗?
目前,我正在使用核心数据,并且需要进行大量输入才能继续输入
number.intValue
//or
number.boolValue
Run Code Online (Sandbox Code Playgroud)
有没有办法直接在方程中使用NSNumber?例如:
int x = 5+ nsNumberInstance;
Run Code Online (Sandbox Code Playgroud)
此外,每次我需要在核心数据中重新分配一个数字时,我都会创建一个这样的新对象.
managedObject.dynamicProperty = [NSNumber numberWithInt: int];
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来改变已经创建的NSNumber的值?从现在起10年后,我可以用什么样的漂亮快捷方式来保存我自己的腕管?
谢谢!
我有这种类型的Enum与TypeDef:
typedef enum {
ControlDisplayOptionNone = 0,
ControlDisplayOptionOne = 100
} ControlDisplayOption;
Run Code Online (Sandbox Code Playgroud)
而且我希望能够将它们放在这样的数组中:
- (NSArray *)displayOptions {
return @[@ControlDisplayOptionNone];
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用,即使这样也行不通:
NSNumber *test = @ControlDisplayOptionNone;
Run Code Online (Sandbox Code Playgroud)
唯一可行的选择是传统的:
return @[[NSNumber numberWithInt:ControlDisplayOptionNone]];
Run Code Online (Sandbox Code Playgroud)
有没有办法使用自动装箱?
public class TestBox {
Integer i;
int j;
public static void main (String[] args) {
TestBox t = new TestBox();
t.go();
}
public void go() {
j = i;
System.out.println(j);
System.out.println(i);
}
}
Run Code Online (Sandbox Code Playgroud)
我在线上得到一个空指针异常j=i
.
虽然如果这条线被转换为i=j
我得到输出,因为0 0
它是int的默认值.
我的问题是,什么时候i
被分配j
,不应该i
取消装箱作为一个int变量,并取默认值0而不是其原来的默认空值?
我正在编写一个程序,需要处理具有许多包装数字变量的对象,如Long,Double,Integer等.如何在不必在任何地方进行空检查的情况下安全地对它们执行数值运算?
我希望这是几乎每个Java程序员必须迟早要处理的事情,所以我很惊讶没有数百个关于该主题的博客帖子和SO问题.
我目前的解决方案是通过这样的方法过滤所有数字:
private static safelyUnbox(Integer i) {
return i == null ? 0 : i.intValue();
}
...
sumVariable += safelyUnbox(stupidObject.getNumberOfWhatever());
Run Code Online (Sandbox Code Playgroud) 我试图测量原始数据类型的执行时间及其包装类来计算相同的数字.我得到的包装类比原始数据类型花费更多时间.
在我的下面的代码中,原始t1 = 5的执行时间和包装类t2的执行时间= 31.
import java.io.*;
import java.util.*;
public class Performance
{
public static long primitive(int count)
{
long startTime = System.currentTimeMillis();
for(int i=0;i<10000;i++)
count++;
System.out.println(count);
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
return elapsedTime;
}
public static long wrapper(Integer count)
{
long startTime = System.currentTimeMillis();
for(int i=0;i<10000;i++)
count++;
System.out.println(count);
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
return elapsedTime;
}
public static void main(String args[])
{
Integer c = new Integer(0);
long t2=Performance.wrapper(c); …
Run Code Online (Sandbox Code Playgroud) autoboxing ×10
java ×7
boxing ×2
integer ×2
comparison ×1
core-data ×1
equality ×1
execution ×1
hashmap ×1
ios ×1
ios5 ×1
kotlin ×1
nsnumber ×1
nullable ×1
objective-c ×1
performance ×1
primitive ×1