autoboxing

Widening and Boxing Java primitives

风格不统一 提交于 2019-12-18 08:09:11
问题 Widening and Boxing Java primitives. I know it is not possible to widen a wrapper class from one to another as they are not from the same inheritence tree. Why though is it not possible to widen a primitive to another primitive type and autobox the widened primitive? Given that a byte argument can be passed to a method that expects an int, why cant the byte in the following example be widened to an int and then boxed to an Integer? class ScjpTest{ static void goInteger(Integer x){ System.out

java: boolean instanceOf Boolean?

强颜欢笑 提交于 2019-12-18 04:38:11
问题 I'm a bit confused: I have a function, that takes an Object as argument. But the compiler does not complain if I just pass a primitive and even recognizes a boolean primitive as Boolean Object. Why is that so? public String test(Object value) { if (! (value instanceof Boolean) ) return "invalid"; if (((Boolean) value).booleanValue() == true ) return "yes"; if (((Boolean) value).booleanValue() == false ) return "no"; return "dunno"; } String result = test(true); // will result in "yes" 回答1:

Method overloading with primitives and their wrappers

你离开我真会死。 提交于 2019-12-18 03:14:20
问题 I am trying to formulate the rules that are being used in the scenarios below. Please explain why I am getting 2 different outputs. Scenario 1 output: I am an object. class Test { public static void main (String[] args) { Test t = new Test(); byte b_var = 10; t.do_the_test(b_var); } public void do_the_test(Character c) { System.out.println("I am a character."); } public void do_the_test(Integer i) { System.out.println("I am an integer."); } public void do_the_test(Object obj) { System.out

Wrapper classes - why integer literals fail for Long but work for anything smaller

Deadly 提交于 2019-12-17 19:38:59
问题 Just trying to understand auto-boxing, which I do apart from one thing: Short s = 250; Long l = 250; The assignment to Long l fails. This, I expect, is because you cannot widen then box (i.e. it tries to widen the int value 250 to a long and then box it which it cannot do). However, the assignment to Short s works. What is going on to make this fine? My assumption was it is still doing boxing and some kind of conversion. But if it's a case of it knowing 250 fits into a short , why does it not

Performance impact of autoboxing

家住魔仙堡 提交于 2019-12-17 18:53:13
问题 Usually the compiler generates code to perform boxing and unboxing. But what does the compiler, if the boxed values are not needed? Is the (Oracle standard) compiler smart enough to optimize it away? Take a look at this method: public static void requireInRange(int index, Object[] array) { if(index < 0 || index >= array.length) throw new IndexOutOfBoundsException(); } The only relevant information is the array.length , so it would be useless to box each value of an array for example. Like in

Does Java autobox when assigning an int to an Object?

╄→гoц情女王★ 提交于 2019-12-17 16:50:09
问题 Is this autoboxing? Object ob = 8; Will the above code first wrap the int literal 8 in an Integer and then assign its reference to variable ob? Because the java language specification has nothing on this case. 回答1: Will the above code first wrap the int literal 8 in an Integer and then assign its reference to variable ob? Yes. (Or rather, it will box the int value into an Integer object, and then assign the reference to the variable ob . The fact that the integer value is a literal is

Boxed Primitives and Equivalence

大兔子大兔子 提交于 2019-12-17 16:33:46
问题 So I was asked this question today. Integer a = 3; Integer b = 2; Integer c = 5; Integer d = a + b; System.out.println(c == d); What will this program print out? It returns true. I answered it will always print out false because of how I understood auto (and auto un) boxing. I was under the impression that assigning Integer a = 3 will create a new Integer(3) so that an == will evaluate the reference rather then the primitive value. Can anyone explain this? 回答1: Boxed values between -128 to

Why does autoboxing in Java allow me to have 3 possible values for a boolean?

倾然丶 夕夏残阳落幕 提交于 2019-12-17 16:28:51
问题 Reference: http://java.sun.com/j2se/1.5.0/docs/guide/language/autoboxing.html "If your program tries to autounbox null, it will throw a NullPointerException." javac will give you a compile-time error if you try to assign null to a boolean. makes sense. assigning null to a Boolean is a-ok though. also makes sense, i guess. but let's think about the fact that you'll get a NPE when trying to autounbox null. what this means is that you can't safely perform boolean operations on Booleans without

Is it guaranteed that new Integer(i) == i in Java?

 ̄綄美尐妖づ 提交于 2019-12-17 06:13:24
问题 Consider the following snippet: int i = 99999999; byte b = 99; short s = 9999; Integer ii = Integer.valueOf(9); // should be within cache System.out.println(new Integer(i) == i); // "true" System.out.println(new Integer(b) == b); // "true" System.out.println(new Integer(s) == s); // "true" System.out.println(new Integer(ii) == ii); // "false" It's obvious why the last line will ALWAYS prints "false" : we're using == reference identity comparison, and a new object will NEVER be == to an

NullPointerException through auto-boxing-behavior of Java ternary operator

半世苍凉 提交于 2019-12-17 03:42:26
问题 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,