autoboxing

String Concatenation and Autoboxing in Java

痞子三分冷 提交于 2021-01-27 05:25:48
问题 When you concatenate a String with a primitive such as int, does it autobox the value first. ex. String string = "Four" + 4; How does it convert the value to a string in Java ? 回答1: To see what the Java compiler produces it is always useful to use javap -c to show the actual bytecode produced: For example the following Java code: String s1 = "Four" + 4; int i = 4; String s2 = "Four" + i; would produce the following bytecode: 0: ldc #2; //String Four4 2: astore_1 3: iconst_4 4: istore_2 5: new

String Concatenation and Autoboxing in Java

浪子不回头ぞ 提交于 2021-01-27 05:24:36
问题 When you concatenate a String with a primitive such as int, does it autobox the value first. ex. String string = "Four" + 4; How does it convert the value to a string in Java ? 回答1: To see what the Java compiler produces it is always useful to use javap -c to show the actual bytecode produced: For example the following Java code: String s1 = "Four" + 4; int i = 4; String s2 = "Four" + i; would produce the following bytecode: 0: ldc #2; //String Four4 2: astore_1 3: iconst_4 4: istore_2 5: new

Why does autoboxing not use valueOf() when invoking via reflection?

扶醉桌前 提交于 2020-06-07 13:48:13
问题 To my understanding following code should print "true" , but when I run it it prints "false" . public class Test { public static boolean testTrue() { return true; } public static void main(String[] args) throws Exception { Object trueResult = Test.class.getMethod("testTrue").invoke(null); System.out.println(trueResult == Boolean.TRUE); } } According to JLS §5.1.7. Boxing Conversion: If the value p being boxed is true , false , a byte , or a char in the range \u0000 to \u007f , or an int or

Why are so few things @specialized in Scala's standard library?

不羁岁月 提交于 2020-05-09 18:33:09
问题 I've searched for the use of @specialized in the source code of the standard library of Scala 2.8.1. It looks like only a handful of traits and classes use this annotation: Function0 , Function1 , Function2 , Tuple1 , Tuple2 , Product1 , Product2 , AbstractFunction0 , AbstractFunction1 , AbstractFunction2 . None of the collection classes are @specialized . Why not? Would this generate too many classes? This means that using collection classes with primitive types is very inefficient, because

Why no autoboxing when removing primitive type from a List in Java?

拜拜、爱过 提交于 2020-01-10 05:09:04
问题 I have the code below throwing IndexOutOfBoundsException : List<Character> list = new ArrayList<>(); char c = 'a'; list.add(c); list.remove(c); // gets fixed by passing list.remove((Character)c); I know that this happens because autoboxing is not happening while removal and it happens while adding an element. My question is why? What is so special in adding that autoboxing from char to Character is possible while in the remove method it's not? 回答1: Java will consider boxing a primitive type

Possible to disable Java autoboxing?

大城市里の小女人 提交于 2020-01-03 18:51:13
问题 The "Generics (Updated)" Java tutorial at: http://docs.oracle.com/javase/tutorial/java/generics/types.html defines a simple Box class: public class Box { private Object object; public void set(Object object) { this.object = object; } public Object get() { return object; } } and states: Since its methods accept or return an Object, you are free to pass in whatever you want, provided that it is not one of the primitive types. Every primitive I pass to the set method works without compilation

Why not auto-box Java primitive types for Generics?

ぃ、小莉子 提交于 2020-01-01 05:08:07
问题 Java does not allow primitive types to be used in generic data structures. E.g. ArrayList<int> is not allowed. The reason is, primitive types can not be directly converted to Object. However Java 1.5 does support auto-boxing, and wrapper classes work in generic data structures. So why couldn't the compiler auto-box it to ArrayList<Integer>? Are there any other reasons for why this can not work? 回答1: So as far as I understand it, your proposed ArrayList<int> would be identical to ArrayList

Integer value comparison

帅比萌擦擦* 提交于 2019-12-31 11:37:29
问题 I'm a newbie Java coder and I just read a variable of an integer class can be described three different ways in the API. I have the following code: if (count.compareTo(0)) { System.out.println(out_table); count++; } This is inside a loop and just outputs out_table . My goal is to figure out how to see if the value in integer count > 0 . I realize the count.compare(0) is the correct way? or is it count.equals(0) ? I know the count == 0 is incorrect. Is this right? Is there a value comparison

Integer value comparison

ε祈祈猫儿з 提交于 2019-12-31 11:37:22
问题 I'm a newbie Java coder and I just read a variable of an integer class can be described three different ways in the API. I have the following code: if (count.compareTo(0)) { System.out.println(out_table); count++; } This is inside a loop and just outputs out_table . My goal is to figure out how to see if the value in integer count > 0 . I realize the count.compare(0) is the correct way? or is it count.equals(0) ? I know the count == 0 is incorrect. Is this right? Is there a value comparison

Oracle java tutorial - possible error regarding Character autoboxing java comment [duplicate]

一世执手 提交于 2019-12-31 02:34:30
问题 This question already has answers here : Does autoboxing call valueOf()? (4 answers) Closed last year . I'm new to JAVA, currently learning Oracle tutorial generics section. I think there is a mistake there, and I want to make sure I'm not wrong. I'll appreciate your feedback. I saw the following explanation at https://docs.oracle.com/javase/tutorial/java/generics/restrictions.html Pair < Integer, Character > p = new Pair<>(8, 'a'); Note that the Java compiler autoboxes 8 to Integer.valueOf(8