autoboxing

Autoboxing versus manual boxing in Java

情到浓时终转凉″ 提交于 2019-11-27 19:47:05
Why is the second piece of code faster? Map<Integer, Double> map = new HashMap<Integer, Double>(); for (int i = 0; i < 50000; i++) { for (double j = 0.0; j < 10000; j++) { map.put(i, j); } } Map<Integer, Double> map=new HashMap<Integer, Double>(); for (int i = 0; i < 50000; i++) { for (double j = 0.0; j < 10000; j++) { map.put(new Integer(i), new Double(j)); } } Autoboxing uses Integer.valueOf , which internally caches Integer objects for small integers (by default -128 to 127, but the max value can be configured with the "java.lang.Integer.IntegerCache.high" property - see the source code of

Why are primitive types such as Int erased to Object in Scala?

邮差的信 提交于 2019-11-27 17:36:27
问题 In Scala, { x: Option[Int] => x } .getClass .getMethod("apply", classOf[Option[_]]) .getGenericParameterTypes returns Array(scala.Option<java.lang.Object>) . I'd initially been expecting to see instead Array(scala.Option<scala.Int>) , but I see that scala.Int is a value class (extends AnyVal ) 'whose instances are not represented as objects by the underlying host system'. I still don't understand the erasure to Object , though. Couldn't it be the much more useful java.lang.Integer ? 回答1:

Which is better: letting Java do autoboxing or using valueOf()

房东的猫 提交于 2019-11-27 15:49:08
问题 I am just wondering is there any difference in letting java autobox say an integer: Integer myInteger = 3; // This will call Integer.valueOf() or having your code as Integer myInteger = Integer.valueOf(3); Is there any micro optimization on this? I know the second one is more explicit, but it is also more unnecessary typing, is there any difference besides this?. 回答1: They are equal anyway internally, so use the first variant. Chances are good, that future compiler optimizations may make the

Java convert Arraylist<Float> to float[]

走远了吗. 提交于 2019-11-27 15:28:29
问题 How I can do that? I have an arraylist, with float elements. (Arraylist <Float>) (float[]) Floats_arraylist.toArray() it is not working. cannot cast from Object[] to float[] 回答1: Loop over it yourself. List<Float> floatList = getItSomehow(); float[] floatArray = new float[floatList.size()]; int i = 0; for (Float f : floatList) { floatArray[i++] = (f != null ? f : Float.NaN); // Or whatever default you want. } The nullcheck is mandatory to avoid NullPointerException because a Float (an object)

NullPointerException with autoboxing in ternary expression

时间秒杀一切 提交于 2019-11-27 13:12:04
Run the following Java code: boolean b = false; Double d1 = 0d; Double d2 = null; Double d = b ? d1.doubleValue() : d2; Why is there a NullPointerException? The return type of the conditional expression b ? d1.doubleValue : d2 is double . A conditional expression must have a single return type. Following the rules for binary numeric promotion, d2 is autounboxed to a double , which causes a NullPointerException when d2 == null . From the language spec, section §15.25: Otherwise, if the second and third operands have types that are convertible (§5.1.8) to numeric types, then there are several

Why doesn't autoboxing overrule varargs when using method overloading in Java 7?

我怕爱的太早我们不能终老 提交于 2019-11-27 08:38:37
We have a class LogManager in our Java project which looks like this: public class LogManager { public void log(Level logLevel, Object... args) { // do something } public void log(Level logLevel, int value, Object... args) { // do something else } } When compiling the project with OpenJDK 6 under Debian everyting works fine. When using OpenJDK 7 the build (done with ant) produces the following errors and the build fails: [javac] /…/LogManager.java:123: error: reference to log is ambiguous, both method log(Level,Object...) in LogManager and method log(Level,int,Object...) in LogManager match

Java allows to assign byte to java.lang.Short but not to java.lang.Integer

北城余情 提交于 2019-11-27 06:52:11
问题 final byte b = 12; Short s = b; Integer i = b; Program compiles fine for Short but for Integer compilation fails with "incompatible types" message. I am having difficult time trying to understand this behavior. I could not find anything for this specific scenario.. 回答1: I attempted to duplicate this with a wider group of assignment contexts: final byte b = 12; Byte b2 = b; Character c = b; // Only an error if b isn't final char c2 = b; // Only an error if b isn't final Short s = b; // Only an

What does it mean to say a type is “boxed”?

妖精的绣舞 提交于 2019-11-27 06:33:28
问题 I have heard of types being referred to as "boxed" in some languages. In Java, I have heard of "autoboxing". What is this? Is it having wrapper classes for a type? How would my code change if I'm working with boxed or unboxed types? 回答1: Some data types are considered "primitive", meaning they are not treated like an object and don't have the properties of an object. On most platforms, integers and characters are examples of types that are primitive but can be boxed. Boxing means wrapping

Does autoboxing call valueOf()?

别说谁变了你拦得住时间么 提交于 2019-11-27 05:46:07
问题 I'm trying to determine whether the following statements are guaranteed to be true: ((Boolean)true) == Boolean.TRUE ((Boolean)true) == Boolean.valueOf(true) ((Integer)1) == Integer.valueOf(1) I've always assumed that autoboxing was equivalent to calling valueOf() on the corresponding type. Every discussion that I've seen on the topic seems to support my assumption. But all I could find in the JLS was the following (§5.1.7): If the value p being boxed is an integer literal of type int between

Java autoboxing and ternary operator madness

若如初见. 提交于 2019-11-27 04:48:48
Just spent a frustrating couple of hours debugging this code: LinkedHashMap<String, Integer> rsrqs = new LinkedHashMap<String, Integer>(); Integer boxedPci = 52; Integer boxedRsrq = boxedPci != null ? rsrqs.get(boxedPci.toString()) : -1; The above produces a NullPointerException. The below code doesn't: LinkedHashMap<String, Integer> rsrqs = new LinkedHashMap<String, Integer>(); Integer boxedPci = 52; Integer boxedRsrq = boxedPci != null ? rsrqs.get(boxedPci.toString()) : Integer.valueOf(-1); The only difference is wrapping the -1 with Integer.valueOf(). I'm sure I'm going to smack my forehead