autoboxing

Does autoboxing call valueOf()?

ⅰ亾dé卋堺 提交于 2019-11-28 07:55:20
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 -128 and 127 inclusive (§3.10.1), or the boolean literal true or false (§3.10.3), or a character

Java autoboxing rules

江枫思渺然 提交于 2019-11-28 07:41:36
I am a java novice and so confused by the following example. Is it okay to think that "==" sign will compare the values between Integers and "autoboxed" Integers from int, and compare reference address between Integers? What about doubles and 0/0? import edu.princeton.cs.introcs.*; public class Autoboxing { public static void cmp(Integer first, Integer second) { if (first < second) StdOut.printf("%d < %d\n", first, second); else if (first == second) StdOut.printf("%d == %d\n", first, second); else if (first > second) StdOut.printf("%d > %d\n", first, second); else StdOut.printf("%d and %d are

The expression of type x is boxed into X?

流过昼夜 提交于 2019-11-28 07:40:24
问题 I'm a little confused by a warning that my Eclipse IDE is currently writing next to every expression where types are autoboxed or autounboxed: The expression of type x is boxed into X The expression of type X is unboxed into x Is this a warning I should react on? I thought autoboxing was a Java language feature - but now I seem to get warnings everytime this feature is used. 回答1: I don't think Eclipse does this by default (mine does not), but you can turn it on or off using Preferences > Java

Java question about autoboxing and object equality / identity [duplicate]

ε祈祈猫儿з 提交于 2019-11-28 07:34:51
This question already has an answer here: Weird Integer boxing in Java 10 answers public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here int a1 = 1000, a2 = 1000; System.out.println(a1==a2);//=>true Integer b1 = 1000, b2 = 1000; System.out.println(b1 == b2);//=>false Integer c1 = 100, c2 = 100; System.out.println(c1 == c2);//=>true } } Why is b1 == b2 false and c1 == c2 true? Read this . Java uses a pool for Integer s in the range from -128 to 127. That means if you create an Integer with Integer i = 42;

why does auto-boxing and unboxing of integers does not work with Arrays.asList in Java? [duplicate]

两盒软妹~` 提交于 2019-11-28 04:31:43
问题 This question already has an answer here: Arrays.asList() not working as it should? 10 answers The following is throws compile error : int[] arrs = {1,2,4,3,5,6}; List<Integer> arry = Arrays.asList(arrs); but this works: for (Integer i : arrs){ //do something } Auto-boxing works in many contexts, I just gave one example of for-loop above. but it fails in the List-view that I make in Arrays.asList() . Why does this fail and why is such as a design implementation chosen? 回答1: To make things

autoboxing of numeric literals : wrapper initialization vs passing method arguments inconsistency

别等时光非礼了梦想. 提交于 2019-11-28 03:36:58
问题 Please consider 2 cases: //1 Short s = 10; //obviously compiles //2 takeShort(10); //error - int is not applicable //where: static void takeShort(Short s) {} I assume that case 1 is changed by compiler to : short _temp_s = 10; Short s = Short.valueOf(_temp_s); Could you please explain what compiler is trying to do in case 2, so it does not compile ? If it is not trying to apply autoboxing as it does in case 1, then why ? EDIT Reference to JSL in johnchen902 answer explains compiler's

Does Java autobox when assigning an int to an Object?

纵饮孤独 提交于 2019-11-28 01:25:38
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. 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 irrelevant here, really. It could be a method call returning int , for example.) Because the java language

Autoboxing: So I can write: Integer i = 0; instead of: Integer i = new Integer(0);

那年仲夏 提交于 2019-11-27 23:49:34
Autoboxing seems to come down to the fact that I can write: Integer i = 0; instead of: Integer i = new Integer(0); So, the compiler can automatically convert a primitive to an Object. Is that the idea? Why is this important? You kind of simplified it a little too much. Autoboxing also comes into play when using collections. As explained in sun's java docs: Collections can only hold object references, so you have to box primitive values into the appropriate wrapper class. ... When you take the object out of the collection, you get the Integer that you put in; if you need an int, you must unbox

Boxed Primitives and Equivalence

老子叫甜甜 提交于 2019-11-27 23:35:11
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? Peter Štibraný Boxed values between -128 to 127 are cached. Boxing uses Integer.valueOf method, which uses the cache. Values outside the

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

こ雲淡風輕ζ 提交于 2019-11-27 23:12:01
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 null-checking or exception handling. same goes for doing math operations on an Integer. for a long time,