autoboxing

Why java does not autobox int[] to Integer[]

℡╲_俬逩灬. 提交于 2019-11-29 04:13:39
When I do the following, arrayList1 - contains one element and it is an int[] . arrayList2 - not compiling (Error : The constructor ArrayList<Integer>(List<int[]>) is undefined) arrayList3 - contains 7 elements and they are Integer objects Here's the code: int[] intArray = new int[]{2,3,4,5,6,7,8}; ArrayList arrayList1 = new ArrayList(Arrays.asList(intArray)); ArrayList<Integer> arrayList2 = new ArrayList<Integer>(Arrays.asList(intArray)); Integer[] integerArray = new Integer[]{2,3,4,5,6,7,8}; ArrayList<Integer> arrayList3 = new ArrayList<Integer>(Arrays.asList(integerArray)); Question : Why

Why are primitive types such as Int erased to Object in Scala?

馋奶兔 提交于 2019-11-29 03:31:18
In Scala, { x: Option[Int] => x } .getClass .getMethod("apply", classOf[Option[_]]) .getGenericParameterTypes returns Array(scala.Option<java.lang.Object>) . I'd initially been expecting to see instead Array(scala.Option<scala.Int>) , but I see that scala.Int is a value class (extends AnyVal ) 'whose instances are not represented as objects by the underlying host system'. I still don't understand the erasure to Object , though. Couldn't it be the much more useful java.lang.Integer ? Daniel C. Sobral Couldn't it be the much more useful java.lang.Integer ? Yes, and that was even the case, once.

Method overloading with primitives and their wrappers

泪湿孤枕 提交于 2019-11-29 01:29:05
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.println("I am an object."); } } Scenario 2 output: I am an integer. class Test { public static void main

Which is better: letting Java do autoboxing or using valueOf()

元气小坏坏 提交于 2019-11-29 01:20:15
I am just wondering is there any difference in letting java autobox say an integer: Integer myInteger = 3; // This will call Integer.valueOf() or having your code as Integer myInteger = Integer.valueOf(3); Is there any micro optimization on this? I know the second one is more explicit, but it is also more unnecessary typing, is there any difference besides this?. They are equal anyway internally, so use the first variant. Chances are good, that future compiler optimizations may make the first even faster in the future. I'd use the first choice. It's the same thing with less code. Unless I

Java convert Arraylist<Float> to float[]

五迷三道 提交于 2019-11-29 00:55:22
How I can do that? I have an arraylist, with float elements. (Arraylist <Float>) (float[]) Floats_arraylist.toArray() it is not working. cannot cast from Object[] to float[] Loop over it yourself. List<Float> floatList = getItSomehow(); float[] floatArray = new float[floatList.size()]; int i = 0; for (Float f : floatList) { floatArray[i++] = (f != null ? f : Float.NaN); // Or whatever default you want. } The nullcheck is mandatory to avoid NullPointerException because a Float (an object) can be null while a float (a primitive) cannot be null at all. In case you're on Java 8 already and it's no

Java Modifying Elements in a foreach

对着背影说爱祢 提交于 2019-11-28 13:47:04
I'm learning Java on my own; and therefore the code below has no function other than for learning/testing. Essentially I'm trying to modify the elements of an Integer array (namely, halving them) whilst in a foreach loop. I should note that I'm not re-ordering, adding, or deleting elements; simply changing their values. Here is my code: Logger.describe("Now copying half of that array in to a new array, and halving each element"); Integer[] copyArray = new Integer[DEFAULT_SAMPLE_SIZE / 2]; System.arraycopy(intArray, 0, copyArray, 0, DEFAULT_SAMPLE_SIZE / 2); for (Integer x : copyArray) x /= 2;

What does it mean to say a type is “boxed”?

て烟熏妆下的殇ゞ 提交于 2019-11-28 11:52:22
I have heard of types being referred to as "boxed" in some languages. In Java, I have heard of "autoboxing". What is this? Is it having wrapper classes for a type? How would my code change if I'm working with boxed or unboxed types? Some data types are considered "primitive", meaning they are not treated like an object and don't have the properties of an object. On most platforms, integers and characters are examples of types that are primitive but can be boxed. Boxing means wrapping them in an object so they have the behavior of an object. The exact meaning and behavior depends on the

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

ε祈祈猫儿з 提交于 2019-11-28 11:06:44
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 know that 250 will fit into a long ? axtavt Normally, you cannot apply multiple (implicit) conversions

Performance impact of autoboxing

久未见 提交于 2019-11-28 09:12:53
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 this code: int[] anArray = {3, 4, 2}; requireInRange(3, anArray); Will the compiler actually insert

Kotlin boxed Int are not the same

元气小坏坏 提交于 2019-11-28 08:37:25
问题 Please help me understand this piece of code in the kotlin docs:- val a: Int = 10000 print(a === a) // Prints 'true' val boxedA: Int? = a val anotherBoxedA: Int? = a print(boxedA === anotherBoxedA) // !!!Prints 'false'!!! Now, I understand that first int a = 10000 then in the next line it is comparing it using === . Now the question is why when it assigned boxedA=a , it checked if it is null using int? . Can it just be written like this:- val boxedA: Int=a Please if I'm understanding it the