autoboxing

NullPointerException through auto-boxing-behavior of Java ternary operator

浪尽此生 提交于 2019-12-17 03:42:16
问题 I tripped across a really strange NullPointerException the other day caused by an unexpected type-cast in the ternary operator. Given this (useless exemplary) function: Integer getNumber() { return null; } I was expecting the following two code segments to be exactly identical after compilation: Integer number; if (condition) { number = getNumber(); } else { number = 0; } vs. Integer number = (condition) ? getNumber() : 0; . Turns out, if condition is true , the if -statement works fine,

Java: Array of primitive data types does not autobox

夙愿已清 提交于 2019-12-17 02:38:11
问题 I have a method like this: public static <T> boolean isMemberOf(T item, T[] set) { for (T t : set) { if (t.equals(item)) { return true; } } return false; } Now I try to call this method using a char for T : char ch = 'a'; char[] chars = new char[] { 'a', 'b', 'c' }; boolean member = isMemberOf(ch, chars); This doesn't work. I would expect the char and char[] to get autoboxed to Character and Character[] , but that doesn't seem to happen. Any insights? 回答1: There is no autoboxing for arrays,

Why does int num = Integer.getInteger(“123”) throw NullPointerException?

◇◆丶佛笑我妖孽 提交于 2019-12-17 02:35:22
问题 The following code throws NullPointerException : int num = Integer.getInteger("123"); Is my compiler invoking getInteger on null since it's static? That doesn't make any sense! What's happening? 回答1: The Big Picture There are two issues at play here: Integer getInteger(String) doesn't do what you think it does It returns null in this case the assignment from Integer to int causes auto-unboxing Since the Integer is null , NullPointerException is thrown To parse (String) "123" to (int) 123 ,

Why do people still use primitive types in Java?

柔情痞子 提交于 2019-12-16 20:04:26
问题 Since Java 5, we've had boxing/unboxing of primitive types so that int is wrapped to be java.lang.Integer , and so and and so forth. I see a lot of new Java projects lately (that definitely require a JRE of at least version 5, if not 6) that are using int rather than java.lang.Integer , though it's much more convenient to use the latter, as it has a few helper methods for converting to long values et al. Why do some still use primitive types in Java? Is there any tangible benefit? 回答1: In

Weird Integer boxing in Java

自古美人都是妖i 提交于 2019-12-16 19:05:34
问题 I just saw code similar to this: public class Scratch { public static void main(String[] args) { Integer a = 1000, b = 1000; System.out.println(a == b); Integer c = 100, d = 100; System.out.println(c == d); } } When ran, this block of code will print out: false true I understand why the first is false : because the two objects are separate objects, so the == compares the references. But I can't figure out, why is the second statement returning true ? Is there some strange autoboxing rule that

Weird Integer boxing in Java

旧城冷巷雨未停 提交于 2019-12-16 19:05:25
问题 I just saw code similar to this: public class Scratch { public static void main(String[] args) { Integer a = 1000, b = 1000; System.out.println(a == b); Integer c = 100, d = 100; System.out.println(c == d); } } When ran, this block of code will print out: false true I understand why the first is false : because the two objects are separate objects, so the == compares the references. But I can't figure out, why is the second statement returning true ? Is there some strange autoboxing rule that

Generics with autoboxing and unboxing of primitives

独自空忆成欢 提交于 2019-12-14 03:37:35
问题 Why autoboxing and unboxing of primitives not happens with Generics Java. public static <T extends Number> T addNumber(T a , T b) { int c = a*b; System.out.println(c); return c; } Here why * operation can't be performed and why can't return c.Any help would be appreciable. 回答1: Generics are not supposed to be used with primitive types. T indicates a type parameter which should be an object. More reference Why don't Java Generics support primitive types? Java Generics ? , E and T what is the

Java Auto Boxing and conditional operator

无人久伴 提交于 2019-12-13 16:30:30
问题 In sonar i see a major violation warning for code public Long getValue(FieldType fieldType) { Long value = paramLevelMap.get(fieldType); // ok returns Long not long return value == null ? UNSPECIFIED_PARAMETER_KEY : value; // complaints here } Where 'UNSPECIFIED_PARAMETER_KEY' is pvt static long , and 'value' is also long. Boxed value is unboxed and then immediately reboxed Its complaining on the 2nd line. I didn't quite understand it , when & how is primitive long being converted to

Correctly recognise an int[] passed as a varargs parameter

我们两清 提交于 2019-12-13 15:43:08
问题 There seems to be an edge case between the autoboxing and the varargs system for primitive arrays. Is there a clean (i.e. non-reflection) way around this? Example: public class Test { class A<T> { public void a(T... ts) { System.out.println(ts.getClass().getCanonicalName() + " of " + ts[0].getClass().getCanonicalName() + " = " + Arrays.toString(ts)); } } public void test() { // These all work fine - presumably all parameters are autoboxed. new A().a(1, 2, 3); new A().a(1.0, 2.0, 3.0); new A()

error: no suitable method found for put(String,int)

99封情书 提交于 2019-12-13 04:34:40
问题 I got errors when compiling this: TreeMap <String, Long> myMap = new TreeMap <String, Long>(); //populate the map myMap.put("preload_buffer_size", 1024); myMap.put("net_buffer_length", 1024); //etc... error: no suitable method found for put(String,int) myMap.put("preload_buffer_size", 1024); ^ method TreeMap.put(String,Long) is not applicable (actual argument int cannot be converted to Long by method invocation conversion) method AbstractMap.put(String,Long) is not applicable (actual argument