autoboxing

Booleans, conditional operators and autoboxing

孤人 提交于 2019-11-26 02:19:00
问题 Why does this throw NullPointerException public static void main(String[] args) throws Exception { Boolean b = true ? returnsNull() : false; // NPE on this line. System.out.println(b); } public static Boolean returnsNull() { return null; } while this doesn\'t public static void main(String[] args) throws Exception { Boolean b = true ? null : false; System.out.println(b); // null } ? The solution is by the way to replace false by Boolean.FALSE to avoid null being unboxed to boolean --which isn

How java auto boxing/unboxing works?

感情迁移 提交于 2019-11-26 01:59:54
问题 Since JDK 5.0, auto boxing/unboxing was introduced in java, the trick is simple and helpful, but when i started testing different conversions between wrapper classes and primitive types, i get really confused how the concept of auto boxing works in java, for example: Boxing int intValue = 0; Integer intObject = intValue; byte byteValue = 0; intObject = byteValue; // ==> Error After trying different cases ( short , long , float , double ), the only case which is accepted by the compiler is

Why do we use autoboxing and unboxing in Java?

 ̄綄美尐妖づ 提交于 2019-11-26 01:31:55
问题 Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on. If the conversion goes the other way, this is called unboxing. So why do we need it and why do we use autoboxing and unboxing in Java? 回答1: Some context is required to fully understand the main reason behind this. Primitives versus classes Primitive variables in Java

Returning null as an int permitted with ternary operator but not if statement

亡梦爱人 提交于 2019-11-26 00:25:15
问题 Let\'s look at the simple Java code in the following snippet: public class Main { private int temp() { return true ? null : 0; // No compiler error - the compiler allows a return value of null // in a method signature that returns an int. } private int same() { if (true) { return null; // The same is not possible with if, // and causes a compile-time error - incompatible types. } else { return 0; } } public static void main(String[] args) { Main m = new Main(); System.out.println(m.temp());

How to convert int[] into List<Integer> in Java?

ε祈祈猫儿з 提交于 2019-11-26 00:10:31
问题 How do I convert int[] into List<Integer> in Java? Of course, I\'m interested in any other answer than doing it in a loop, item by item. But if there\'s no other answer, I\'ll pick that one as the best to show the fact that this functionality is not part of Java. 回答1: There is no shortcut for converting from int[] to List<Integer> as Arrays.asList does not deal with boxing and will just create a List<int[]> which is not what you want. You have to make a utility method. int[] ints = {1, 2, 3};

How to properly compare two Integers in Java?

狂风中的少年 提交于 2019-11-25 21:47:46
问题 I know that if you compare a boxed primitive Integer with a constant such as: Integer a = 4; if (a < 5) a will automatically be unboxed and the comparison will work. However, what happens when you are comparing two boxed Integers and want to compare either equality or less than/greater than? Integer a = 4; Integer b = 5; if (a == b) Will above code result in checking to see if they are the same object, or will it auto-unbox in that case? What about: Integer a = 4; Integer b = 5; if (a < b) ?